C # Design Pattern Consolidation-Abstract Factory mode

Source: Internet
Author: User

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

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.