C # basic simple factory mode and Singleton Mode,
Design pattern: refers to the code Design experience that most people use repeatedly.
Role: code reuse, easy to understand, and ensure code reliability.
I. Simple factory Model
Usage: Create a factory class (naming convention ends with Factiory) and a static method with parameters (return different subclass objects according to different parameters.
Public class FunctionFactory
{
Public static FatherClass Function (string str)
{
FatherClass fc = null;
...
Return fc = new ChildClass ();
}
}
Main ()
{
Fc. Subclass method (); // rewrite Method
}
Ii. Singleton Mode
By creating a private constructor, you cannot instantiate an object (not new). By creating an instantiated object using a static method, you can only have one instantiated object (single instance) in a class ).
Namespace Singleton Mode
{
Public class Function
{
Private Function (){}// Define the private constructor
Private static Function f; // defines the private static variable of the data type.
Public static Function createtext ()
{
If (f = null)
{
F = new text (); // if f is null, the private constructor is called.
}
Return f; // return f to the caller
}
}
}