c # singleton design pattern and simple factory

Review what these two design patterns

Singleton

What is the Singleton Design Pattern?

Singleton, it is a common software design patterns. In its core structure contains only the special class called singleton. Guarantee single mode system embodiment, a class of the application class model only one instance. I.e. only an object instance of a class.
Concrete realization
to understand the difference between static non-static
static member: class static member variables and is associated with the class as "total" Some variable (a common expression), he does not rely on the existence of a specific object, visit when accessed by the class name .https add operator plus the variable name: //www.jianshu.com/p/e1fee3558cb6
need:

(1) the constructor privatization, it can not be outside the class by the new keyword class is instantiated objects.

(2) generating a unique instance of an object class in the inside, and packaged as private static type.

(3) define a static method returns the unique object.
Specific implementation https://www.cnblogs.com/binaway/p/8889184.html

Simple Factory

Process basically simple factory pattern
specific product categories: the relevant code variety of different products packaged objects need to be created to the specific product class in
the abstract class product: the product class specific common code product packaged in an abstract class abstract and after extraction the
factory classes: a factory class is used to create a variety of products, provides factory methods to create a product in the factory class, the method may be different depending on the parameters passed to create specific products target
client: just call the factory class factory method and passing the corresponding parameter to obtain a target product

Product class abstract
{
// Methods All products common traffic class
public void MethodSame ()
{
// public methods implemented
}

// declare abstract business methods
public abstract void MethodDiff ();
}

Typical specific product class code:
class ConcreteProductA: Product
{
// business method implemented
public void MethodDiff the override ()
{
implement the business methods //
}
}

Factory's class
{
// static factory method
public static Product GetProduct (String Arg)
{
Product Product = null;
IF (arg.Equals ( "A"))
{
Product ConcreteProductA new new = ();
// initialize disposed Product
}
the else IF ( arg.Equals ( "B"))
{
Product ConcreteProductB new new = ();
// initialize disposed Product
}
return Product;
}
}

A typical client code:
class Program
{
static void the Main (String [] args)
{
Product Product;
Product Factory.GetProduct = ( "A"); // create a factory class object through the product
product.MethodSame ();
product.MethodDiff ();
}
}

发布了43 篇原创文章 · 获赞 8 · 访问量 3921

Guess you like

Origin blog.csdn.net/MaYang_/article/details/100760606