Example of an abstract factory:
Design a TV factory in factory method mode:
It is required to provide a sub-factory for TV sets of each brand. For example, the Haier factory is responsible for producing Haier TV sets, and the Hisense factory is responsible for producing Hisense TV sets. If tcl TV sets or skyworth TV sets need to be produced, you only need to add a new TCL factory or skyworth factory. The original factory does not need to be modified, making the entire system more flexible and scalable.
Class diagram:
Note: This type of graph is automatically generated by Visual Studio 2012 based on the source code. The program can be regarded as a client that calls the factory.
The class chart shows that I separately declare a top-level abstract class for TV products, followed by the product abstract classes of Haier and Hisense, and then the specific product classes are implemented by their respective manufacturers. At the same time, the factory category of the product is also composed of a top-level abstract factory class. Two specific Haier factory classes and Hisense factory classes are derived below.
Factory class implementation source code:
Using system; using system. collections. generic; using system. LINQ; using system. text; using system. threading. tasks; namespace abstracttvfactory {// abstract TV abstract class abstvfactory {// Private string brand; Abstract Public abstvproduct buildproduct () ;}// the specific TV brand abstract factory class hairfactory: abstvfactory {// public void getbrand () // {// throw new notimplementedexception (); // console. writeline ("Hair Bran D "); //} public override abstvproduct buildproduct () {// throw new notimplementedexception (); return New hairtv1 () ;}} class hisensefactory: abstvfactory {public override abstvproduct buildproduct () {return New hisensetv1 () ;}} abstract class abstvproduct {Abstract Public void tvproduct ();} // The following is an abstract product TV class abstract class abshairtvproduct: abstvproduct // Haier's product abstract class {abstract override public void Tvproduct ();} abstract class abshisensetvproduct: abstvproduct // The product abstract class of Hisense {abstract override public void tvproduct ();} // The specific TV class hairtv1: abshairtvproduct // Haier TV product 1 {public override void tvproduct () {console. writeline ("this is hair TV product! "+ This. getType (). tostring () ;}} class hairtv2: abshairtvproduct // Haier TV product 2 {public override void tvproduct () {console. writeline ("this is hair TV product! "+ This. getType (). tostring ();} class hisensetv1: abshisensetvproduct {public override void tvproduct () {// throw new notimplementedexception (); console. writeline ("this is Hisense TV product! "+ This. getType (). tostring ();} class hisensetv2: abshisensetvproduct {public override void tvproduct () {// throw new notimplementedexception (); console. writeline ("this is Hisense TV product! "+ This. GetType (). tostring ());}}}
Client running source code:
Using system; using system. collections. generic; using system. LINQ; using system. text; using system. threading. tasks; namespace implements acttvfactory {class program {static void main (string [] ARGs) {abstvfactory hairtvfactory = new hairfactory (); abshairtvproduct hairtv_1 = new hairtv1 (); // Haier TV type 1 hairtv_1 = (abshairtvproduct) hairtvfactory. buildproduct (); hairtv_1.tvproduct (); abshairtvproduct hairtv_2 = new hairtv2 (); // Haier TV type 2 hairtv_2 = (abshairtvproduct) hairtvfactory. buildproduct (); hairtv_2.tvproduct (); // TV abstvfactory hisensetvfactory = new hisensefactory (); abshisensetvproduct hisensetv_1 = new hisensetv1 (); // Hisense TV type 1 hisensetv_1 = (abshisensetvproduct) hisensetvfactory. buildproduct (); hisensetv_1.tvproduct (); abshisensetvproduct hisensetv_2 = new hisensetv2 (); // Hisense TV type 2 hisensetv_2 = (abshisensetvproduct) hisensetvfactory. buildproduct (); hisensetv_2.tvproduct (); console. readkey ();}}}
Run:
Intuitive relationship diagram: