. The design pattern in net two: Single piece mode
Source: Internet
Author: User
Designing a single piece Mode (Singleton) is a very simple pattern, which is the first one I understand and can use. Sometimes the complexity of the pattern is not in itself, but because of the purpose of his application. The initial time to face a pattern is often full of confusion, a simple call why to make it so complicated? Why build so many classes, just to open a file.
Generally speaking, learning a pattern is a process of acceptance, recognition and understanding. Acceptance: Understand the structure of the pattern, understand the meaning of the case; recognition: the role and feasibility of the model in practical engineering; Understand: Apply the pattern to the development process.
The purpose of the model is to reduce the coupling between the modules in time and space, so as to improve the reusability of the module and reduce the probability of error.
Single-piece mode
The singleton pattern is a simpler pattern, and the following code can create an example of a singlton pattern, which is a class that writes system logs.
public class LogWriter
{
Declare a static variable, the type is the class itself
private static LogWriter _instance = null;
Privatize the class constructor so that the class cannot be created by the outside world
Private LogWriter ()
{
}
Provides a static method to create an instance of a class
public static LogWriter getinstance ()
{
if (_instance = null)
{
_instance = new LogWriter ();
}
return _instance;
}
The following implements the other features of the class
//....
}
The caller may not get the singleton instance in the form of new, and the invocation example is as follows:
LogWriter log = Logwriter.getinstance ();
Log. Debug ("Hello World");
Practical application
It is easy to imagine that a single piece pattern applies to situations where an object is unique or has a fixed number of objects throughout the system. such as database connection, configuration file, etc. ...
A pattern is an agreed term between programmers, and language can be the basis of thought. With such a language, some complex concepts become easier to communicate. For example, if a designer simply says that a class is a single piece of mode and is a singleton, the programmer should at least be able to understand the invocation of the class at a later time without further elaboration.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service