Factory methond Case Study

Source: Internet
Author: User

Case Analysis:

The factory method defines an interface for creating objects, but the subclass determines which class to instantiate. Injection Molding demonstrates this mode. Plastic toys manufacturers process plastic powder and inject the plastic into the desired shape of the mold. The type of toys (cars, people, etc.) is determined by the mold.

Intent:

It is intended to define a factory interface for creating product objects and delay the actual creation work to the subclass. In the factory method mode, the core factory class is no longer responsible for the creation of all products, but rather the creation work is handed over to the Child class. This core class becomes the role of an abstract factory. It is only responsible for providing the interface that must be implemented by a specific factory subclass, without touching the details of product class instantiation.

Structure:

Program code:

# Include <iostream. h>

 

Class cmamoju

{

Public:

    Cmamoju ()

    {

        Cout <"producing toy horse Mold" <Endl;

    }

};

 

Class cchemoju

{

Public:

    Cchemoju ()

    {

        Cout <"producing mold vehicles" <Endl;

    }

};

 

Class cwanjugongchang

{

Public:

    Void zhizaowanju ()

    {

        Makemoju ();

        Zhufen ();

        Meihua ();

    }

Protected:

    Virtual void makemoju () = 0;

   Virtual void zhufen () = 0;

    Virtual void Meihua () = 0;

};

 

Class cwanjumagongchang: Public cwanjugongchang

{

Protected:

    Void makemoju ()

    {

        Cmamoju OBJ;

    }

    Void zhufen ()

    {

        Cout <"inject materials into the mold to form it" <Endl;

    }

    Void Meihua ()

    {

        Cout <"Beautify the product and complete product production" <Endl;

    }

};

 

Class cwanjuchegongchang: Public cwanjugongchang

{

Protected:

    Void makemoju ()

    {

        Cchemoju OBJ;

    }

    Void zhufen ()

    {

        Cout <"inject materials into the mold to form it" <Endl;

    }

    Void Meihua ()

    {

        Cout <"Beautify the product and complete product production" <Endl;

    }

};

 

Int main ()

{

   Cwanjugongchang * OBJ = new cwanjumagongchang;

    OBJ-> zhizaowanju ();

 

    Cout <Endl;

 

    OBJ = new cwanjuchegongchang ();

    OBJ-> zhizaowanju ();

    Return 0;

}

Running result:

Production of toy horse Mold

Inject materials into the mold to form the mold.

Beautify the product and complete product production

 

Mold Production

Inject materials into the mold to form the mold.

Beautify the product and complete product production

Press any key to continue

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.