Beauty of design patterns: Abstract Factory)

Source: Internet
Author: User

The participant AbstractFactory declares an operation interface for creating abstract product objects. ConcreteFactory allows you to create specific product objects. AbstractProduct declares an interface for a type of product object. ConcreteProduct defines a product object that will be created by a specific factory. Implements the AbstractProduct interface. The Client only applies to interfaces declared by the AbstractFactory and AbstractProduct classes. APPLICABILITY The Abstract Factory mode can be used in the following scenarios: A system must be independent of the creation, combination, and representation of its products. A system must be configured by one of multiple product generations. When you want to emphasize the design of a series of related product objects for joint use. When you provide a product class library, instead of displaying their interfaces. Disadvantages: It is difficult to support new types of products. To support new products, you need to extend the AbstractFactory interface, which will cause changes to all subclasses. It separates specific classes. It makes product series Easy to switch. It facilitates product consistency. The related mode Abstract Factory is often implemented using the Factory Method. Abstract Factory can also be implemented using Prototype. A specific factory can be a Singleton. Naming Conventions use naming conventions. For example, classes defined as abstract factories are always declared as XxxKit. Implementation Method (1): use Factory Method to implement Abstract Factory. A specific factory will redefine the factory method for each product to specify the product. Copy code 1 namespace AbstractFactoryPattern. implementation1 2 {3 public abstract class AbstractOrInterfaceOfFactoryKit 4 {5 public abstract AbstractOrInterfaceOfProductA CreateProductA (); 6 public abstract AbstractOrInterfaceOfProductB CreateProductB (); 7} 8 9 public abstract class extends actorinterfaceofproducta10 {11} 12 13 public abstract class extends actorinterfaceofproductb14 {15} 16 17 public CIA Ss events: Events {19 public override events actorinterfaceofproducta CreateProductA () 20 {21 return new ConcreteProductA (); 22} 23 24 public override events actorinterfaceofproductb CreateProductB () 25 {26 return new ConcreteProductB (); 27} 28} 29 30 public class ConcreteProductA: AbstractOrInterfaceOfProductA31 {32} 33 34 public class ConcreteProductB: Abstract OrInterfaceOfProductB35 {36} 37 38 public class Client39 {40 public void TestCase1 () 41 {42 export actorinterfaceoffactorykit = new ConcreteFactoryKit1 (); 43 export actorinterfaceofproducta productA = kit. createProductA (); 44 export actorinterfaceofproductb productB = kit. createProductB (); 45} 46} 47} copy the code implementation method (2): Use Prototype to implement Abstract Factory. A specific factory uses the prototype instance of each product in the product series to initialize, and it creates a new product by copying its prototype. Copy code 1 namespace AbstractFactoryPattern. implementation2 2 {3 public abstract class AbstractOrInterfaceOfFactoryKit 4 {5 public abstract AbstractOrInterfaceOfProductA CreateProductA (); 6 public abstract AbstractOrInterfaceOfProductB CreateProductB (); 7} 8 9 public abstract class login actorinterfaceofproducta10 {11 public abstract login actorinterfaceofproducta Clone (); 12} 13 14 public abstract cl Ass keys {16 public abstract events actorinterfaceofproductb Clone (); 17} 18 19 public class ConcreteFactoryKit1: Keys {21 public override events actorinterfaceofproducta CreateProductA () 22 {23 return new ConcreteProductA (); 24} 25 26 public override into actorinterfaceofproductb CreateProductB () 27 {28 return new ConcreteProductB (); 29} 30} 31 32 p Ublic class Syntax: Detail {34 private partition _ prototypeOfProductA; 35 private partition _ prototypeOfProductB; 36 37 public partition (38 partition prototypeOfProductA, 39 partition prototypeOfProductB) 40 {41 _ prototypeOfProductA = prototypeOfProductA; 42 _ prototypeOfPro DuctB = prototypeOfProductB; 43} 44 45 public override AbstractOrInterfaceOfProductA CreateProductA () 46 {47 return _ prototypeOfProductA. clone (); 48} 49 50 public override into actorinterfaceofproductb CreateProductB () 51 {52 return _ prototypeOfProductB. clone (); 53} 54} 55 56 public class ConcreteProductA: AbstractOrInterfaceOfProductA57 {58 public override into actorinterfaceofproducta Clone () 59 {60 Return new ConcreteProductA (); 61} 62} 63 64 public class ConcreteProductB: AbstractOrInterfaceOfProductB65 {66 public override into actorinterfaceofproductb Clone () 67 {68 return new ConcreteProductB (); 69} 70} 71 72 public class Client73 {74 public void TestCase2 () 75 {76 response actorinterfaceoffactorykit kit1 = new ConcreteFactoryKit1 (); 77 response actorinterfaceofproducta productA1 = kit1.CreateProduct A (); 78 export actorinterfaceofproductb productB1 = kit1.CreateProductB (); 79 80 export kit2 = new ConcreteFactoryKit2 (productA1, productB1); 81 export actorinterfaceofproducta productA2 = kit2.CreateProductA (); 82 define actorinterfaceofproductb productB2 = kit2.CreateProductB (); 83} 84} 85} copy the code implementation method (3): define an extensible Abstract Factory. Abstract Factory usually defines an operation for each product that it can produce. The product type is encoded in the operating structure. Add a new product that requires changing the Abstract Factory interface and all the classes related to it. A more flexible but less secure design is to add a parameter to the operation of creating objects. This parameter specifies the type of the object to be created. This parameter can be a Class Identifier, an integer, a string, or anything else that can identify this product. After this change, Abstract Factory only requires a "Make" operation and a parameter indicating the type of the object to be created. Copy code 1 namespace AbstractFactoryPattern. implementation3 2 {3 public enum ProductCategory 4 {5 ProductA, 6 ProductB, 7} 8 9 public abstract class AbstractOrInterfaceOfFactoryKit10 {11 public abstract object CreateProduct (ProductCategory category ); 12} 13 14 public abstract class extends actorinterfaceofproducta15 {16} 17 18 public abstract class extends actorinterfaceofproductb19 {20} 21 22 public clas S ConcreteFactoryKit1: AbstractOrInterfaceOfFactoryKit23 {24 public override object CreateProduct (ProductCategory category) 25 {26 switch (category) 27 {28 case ProductCategory. productA: 29 return new ConcreteProductA (); 30 case ProductCategory. productB: 31 return new ConcreteProductB (); 32 default: 33 throw new NotSupportedException (); 34} 35} 36} 37 38 public class ConcreteProductA: AbstractOrInterfac EOfProductA39 {40} 41 42 public class ConcreteProductB: AbstractOrInterfaceOfProductB43 {44} 45 46 public class Client47 {48 public void TestCase3 () 49 {50 response kit = new ConcreteFactoryKit1 (); 51 export actorinterfaceofproducta productA = (export actorinterfaceofproducta) kit. createProduct (ProductCategory. productA); 52 export actorinterfaceofproductb productB = (export actorinterf AceOfProductB) kit. CreateProduct (ProductCategory. ProductB); 53} 54} 55} copy the code implementation method (4): Use a template to avoid creating subclass. Use generics in C # To implement abstract factories. Copy code 1 namespace AbstractFactoryPattern. implementation4 2 {3 public abstract class Generation 4 {5 public abstract generation actorinterfaceofproducta CreateProductA (); 6 public abstract generation CreateProductB (); 7 public abstract generation actorinterfaceofproductc CreateProductC <TC> () 8 where TC: abstractOrInterfaceOfProductC, new (); 9} 10 11 public abstract class extends actorinterfaceofproducta12 {13} 14 15 public abstract class extends actorinterfaceofproductf8 {17} 18 19 public abstract class extends actorinterfaceofproductc20 {21} 22 23 public class ConcreteFactoryKit1 <TA, TB>: using where TA: Using actorinterfaceofproducta, new () 25 where TB: Using actorinterfaceofproductb, new () 26 {27 public override using actorinterfaceofproducta CreateProductA () 28 {29 return new TA (); 30} 31 32 public override into actorinterfaceofproductb CreateProductB () 33 {34 return new TB (); 35} 36 37 public override into actorinterfaceofproductc CreateProductC <TC> () 38 {39 return new TC (); 40} 41} 42 43 public class ConcreteProductA: AbstractOrInterfaceOfProductA44 {45} 46 47 public class ConcreteProductB: AbstractOrInterfaceOfProductB48 {49} 50 51 public class ConcreteProductC: authorization {53} 54 55 public class Client56 {57 public void TestCase4 () 58 {59 response kit = new ConcreteFactoryKit1 <ConcreteProductA, ConcreteProductB> (); 60 authorization actorinterfaceofproducta productA = kit. createProductA (); 61 export actorinterfaceofproductb productB = kit. createProductB (); 62 export actorinterfaceofproductc productC = kit. createProductC <ConcreteProductC> (); 63} 64} 65}

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.