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