1. Overview:
Defines an interface used to create objects, so that the subclass determines which class to instantiate. The factory method delays the instantiation of a class to the subclass.
2. Role in the Mode
2.1 Abstract Factory: This abstract class (or interface) declares a factory method for creating an object to return an object of the product type.
2.2 concretecreator: redefines the factory method and returns a specific concrete product instance.
2.3 abstract product: defines the object created by the factory method.
2.4 concreteproduct: a product that is inherited from the product abstract class.
3. Interpretation of the Mode
3.1 General class diagram of factory method mode
3.2 code implementation of factory method mode
/// <Summary> /// abstract factory, declare a method for creating a product. // </Summary> public abstract class creator {// <summary> // This method is used to return the product. // </Summary >/// <returns> </returns> public abstract product createproduct ();} /// <summary> /// the creator of product class A in the specific production factory, inherited from the abstract factory creator /// </Summary> public class productacreator: creator {public override product createproduct () {return New concreteproducta () ;}/// <summary> // The creator of the specific production factory and product class B, inherited from the abstract factory creator // </Summary> public class productbcreator: Creator {public productbcreator () {} public override product createproduct () {return New concreteproductb ();}} /// <summary> /// abstract product, define the object created in the factory method /// </Summary> public abstract class product {public product () {} public abstract void opration () ;}/// <summary> // product A, inherited from product /// </Summary> public class concreteproducta: product {public concreteproducta () {} public override void opration () {// This is product A }}/// <summary> // product B, inherited from product // </Summary> public class concreteproductb: Product {public concreteproductb () {} public override void opration () {// This is product B }}
4. Summary
4.1 advantages:
The factory method removes the condition Branch (removing the coupling between the factory class and the branch) and solves the problem that the simple factory is open to modification.
4.2 disadvantages:
When implementing the factory method mode, the client needs to decide which factory to instantiate to build a specific product. The selection and determination still exists, that is, in the factory method mode, the logic judgment of the simple factory is handed over to the client for processing.
For the simple factory mode, you need to modify the factory class to add the function. For the factory method mode, the client is modified.
4.3 Use Cases:
For a product, the caller knows exactly which factory service should be used, instantiate the specific factory, and produce specific products.
The number of sub-classes is not fixed, and new function sub-classes may appear at any time.