Beauty of design patterns: Factory Method)

Source: Internet
Author: User

Virtual Constructor (Virtual Constructor) is intended to define an interface for creating the target object, so that the subclass decides which target class to instantiate. Factory Method delays the instantiation of a class to its subclass. Structure participant Product defines the Interface of the object created by the factory method ). ConcreteProduct implements the Product interface. Creator declares the factory method, which returns an object of the Product type. Creator can also define the default implementation of a factory method, which returns a default ConcreteProduct object. You can call the factory method to create a Product object. ConcreteCreator redefines (Override) factory methods to create a Product object. The applicability can use the Factory Method mode in the following cases: when a class does not know the class of the object it must create. When a class wants its subclass to specify the object it creates. When a class delegates the responsibility of creating an object to one of multiple help sub-classes, and you want to localize the information of which help sub-classes are proxies. Disadvantage customers may have to create a subclass of Creator just to create a specific ConcreteProduct object. The effect is that the subclass provides a hook connection parallel class level related mode Abstract Factory is often implemented using the Factory method. Factory Method is usually called in Template Method. Prototype does not need to create a subclass of Creator. However, they usually require an Initialize operation for the Product class. Creator uses Initialize to Initialize objects, and the Factory Method does not need such operations. It is a good habit to use naming conventions. It clearly shows that you are using the factory method. (Convention over Configuration) for example, it always declares the abstract operations defined as factory methods as CreateProduct (). Implementation Method (1): The Creator class is an abstract class and does not provide the Implementation of the factory method it declares. Subclass is required to define the implementation, because there is no reasonable default implementation. It avoids the issue of having to instantiate unpredictable classes. Copy code 1 namespace FactoryMethodPattern. implementation1 2 {3 public abstract class extends actorinterfaceofcreator 4 {5 public abstract extends actorinterfaceofproduct CreateProduct (); 6} 7 8 public abstract class extends actorinterfaceofproduct 9 {10} 11 12 public class ConcreteCreator: export actorinterfaceofcreator13 {14 public override into actorinterfaceofproduct CreateProduct () 15 {16 return new Concr EteProduct (); 17} 18} 19 20 public class ConcreteProduct: AbstractOrInterfaceOfProduct21 {22} 23 24 public partial class Test25 {26 public void Case1 () 27 {28 response actorinterfaceofcreator creator = new ConcreteCreator (); 29 Response actorinterfaceofproduct product = creator. createProduct (); 30} 31} 32} implementation method (2): The Creator class is a specific class and provides a default implementation for the factory method. The specific Creator mainly uses the factory method for flexibility. It follows the principle that "create objects with an independent operation so that subclasses can redefine their creation methods ". This criterion ensures that the subclass designer can change the class of the object instantiated by the parent class when necessary. 1 namespace FactoryMethodPattern. implementation2 2 {3 public class ConcreteCreator 4 {5 public virtual login actorinterfaceofproduct CreateProduct () 6 {7 return new ConcreteProduct (); 8} 9} 10 11 public abstract class AbstractOrInterfaceOfProduct12 {13} 14 15 public class ConcreteProduct: AbstractOrInterfaceOfProduct16 {17} 18 19 public partial class Test20 {21 public void Case2 () 22 {23 concret TeCreator creator = new ConcreteCreator (); 24 AbstractOrInterfaceOfProduct product = creator. CreateProduct (); 25} 26} 27} implementation method (3): Parameterized Factory method. You can create multiple products by using Parameterized Factory methods. The factory method uses a parameter that identifies the object type to be created. 1 namespace FactoryMethodPattern. implementation3 2 {3 public enum ProductCategory 4 {5 GoodProduct, 6 BadProduct, 7} 8 9 public class ConcreteCreator10 {11 public virtual login actorinterfaceofproduct CreateProduct (ProductCategory category) 12 {13 switch (category) 14 {15 case ProductCategory. goodProduct: 16 return new ConcreteGoodProduct (); 17 case ProductCategory. badProduct: 18 return new ConcreteB AdProduct (); 19 default: 20 throw new NotSupportedException (); 21} 22} 23} 24 25 public abstract class extends actorinterfaceofproduct26 {27} 28 29 public class ConcreteGoodProduct: abstractOrInterfaceOfProduct30 {31} 32 33 public class ConcreteBadProduct: AbstractOrInterfaceOfProduct34 {35} 36 37 public partial class Test38 {39 public void Case3 () 40 {41 ConcreteCreator creator = new ConcreteCreator (); 42 define actorinterfaceofproduct product = creator. CreateProduct (ProductCategory. GoodProduct); 43} 44} 45} implementation method (4): Use a template to avoid creating subclass. Use the generic implementation factory method in C. 1 namespace FactoryMethodPattern. implementation4 2 {3 public abstract class Objective 4 {5 public abstract AbstractOrInterfaceOfProduct CreateProduct (); 6} 7 8 public class GenericConcreteCreator <T>: AbstractOrInterfaceOfCreator 9 where T: AbstractOrInterfaceOfProduct, new () 10 {11 public Transport actorinterfaceofproduct CreateProduct () 12 {13 return new T (); 14} 15} 16 17 public abstract class extends actorinterfaceofproduct18 {19} 20 21 public class ConcreteGoodProduct: abstractOrInterfaceOfProduct22 {23} 24 25 public class ConcreteBadProduct: AbstractOrInterfaceOfProduct26 {27} 28 29 public partial class Test30 {31 public void Case3 () 32 {33 response creator1 = new GenericConcreteCreator <ConcreteGoodProduct> (); 34 response creator2 = new GenericConcreteCreator <ConcreteBadProduct> (); 35 response actorinterfaceofproduct product1 = creator1.CreateProduct (); 36. AbstractOrInterfaceOfProduct product2 = creator2.CreateProduct (); 37} 38} 39}

Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.