Objective
Continue to Factory mode Premium Edition, abstract Factory mode. The abstract factory model is actually extended from the factory approach model. In the actual life of a factory product can not be single, it is certainly a variety of products.
Introduction-Abstract Factory mode
Definition: (from Baidu Encyclopedia ~) provides an interface for creating a set of related or interdependent objects without specifying their specific classes.
Realize
Continue with an example of a Amoy shoe factory in the previous article. Now the shoe factory business is bigger even opened the branch, the boss now decided to expand Business, buy clothes ... Buy t-shirt in summer, buy cotton coat in winter, see below to realize:
1. Clothing
// <summary> ///Clothing base class/// </summary> Public Abstract classClothes { Public Abstract stringName {Get; } } /// <summary> ///T -shirts/// </summary> Public classtshirt:clothes { Public Override stringName {Get { return "T -shirts"; } } } /// <summary> ///Jackets/// </summary> Public classjacket:clothes { Public Override stringName {Get { return "Jackets"; } } }
2. Shoes
/// <summary> ///Shoes base class/// </summary> Public Abstract classShoes { Public Abstract stringName {Get; } } /// <summary> ///Sandals/// </summary> Public classSandal:shoes { Public Override stringName {Get { return "Sandals"; } } } /// <summary> ///Shoes/// </summary> Public classCottonpaddedshoes:shoes { Public Override stringName {Get { return "Shoes"; } } }
3. Factory
/// <summary> ///Abstract Factory class/// </summary> Public Abstract classFactory {/// <summary> ///production of shoes/// </summary> /// <returns></returns> Public AbstractShoes createshoes (); /// <summary> ///Build Clothes/// </summary> /// <returns></returns> Public AbstractClothes createclothes (); } /// <summary> ///Summer Factory class/// </summary> Public classSummerfactory:factory { Public OverrideClothes createclothes () {return Newtshirt (); } Public OverrideShoes createshoes () {return Newsandal (); } } /// <summary> ///Winter Factory class/// </summary> Public classWinterfactory:factory { Public OverrideClothes createclothes () {return NewJacket (); } Public OverrideShoes createshoes () {return Newcottonpaddedshoes (); } }
Call:
classProgram {Static voidMain (string[] args) { Console. WriteLine ("Summer Factory production ..."); //Summer Factory FactoryFactory =New summerfactory(); //make shoes, clothes ShoesShoes =Factory. Createshoes (); Clothes Cloth=Factory. Createclothes (); //look at the production is Console. WriteLine ("production: {0},{1}", shoes. Name, cloth. Name); Console. WriteLine ("Winter Factory production ..."); //Winter Factory FactoryFactory1 =New winterfactory(); //Making Shoes ShoesShoes1 =Factory1. Createshoes (); Clothes cloth1=Factory1. Createclothes (); //look at the production is Console. WriteLine ("production: {0},{1}", Shoes1. Name, Cloth1. Name); Console. Read (); } }
Results:
C # Design Pattern Consolidation-Abstract Factory mode