In the abstract factory model, I will introduce an example on the Internet.
With the passage of time, more and more places have passed by, and more friends are there. You found that the food was originally divided into many cuisines, such as Lu food, Cantonese food, Hunan food, etc.
It is difficult for different friends to use different cuisines. Your chef is Lu cuisine. What should I do? Guangdong friends are not used to it. Now let's go back to the simple factory model.
Braised Pork is further inherited to generate braised pork in Lu cuisine, braised pork in Cantonese cuisine, and braised pork in Hunan cuisine. The steamed fish is inherited to Lu cuisine steamed fish, Cantonese food steamed fish, and Hunan food steamed fish.
Push. We also want to modify this class to prevent it from returning to the food base class. Instead, we will return to the braised pork and steamed fish layers and abstract these methods as the base class of the cuisine factory.
This class inherits from the Lu Food Factory, Guangdong food factory, Hunan Food Factory, and so on. Then, these factories create specific dishes. Haha if you entertain Guangdong friends, you will use Guangdong food factory to return
A table of braised pork and steamed fish from Cantonese cuisine is returned. Your Guangdong friends will definitely have a very satisfying appetite. Well, you have implemented the abstract factory model. Schema
The pattern is also changed.
Now we can see how perfect it is to hire a new cook to create a new dish, but it is very difficult if you want to add a new dish.
Implementation Code:
// Product base
# Include <iostream>
Class foodproduct
{
Public:
Foodproduct (void );
~ Foodproduct (void );
Virtual void delicious ();
};
// Braised fish
Class braisingfish: Public foodproduct
{
Public:
Braisingfish (void );
~ Braisingfish (void );
Virtual void delicious ();
};
// Steamed fish
Class steamedfish: Public foodproduct
{
Public:
Steamedfish (void );
~ Steamedfish (void );
Virtual void delicious ();
};
// Sichuan-style steamed fish
Class chuanbraisingfish: Public braisingfish
{
Public:
Chuanbraisingfish (void );
~ Chuanbraisingfish (void );
Virtual void delicious ();
};
// Lu-style steamed fish
Class lubraisingfish: Public braisingfish
{
Public:
Lubraisingfish (void );
~ Lubraisingfish (void );
Virtual void delicious ();
};
// The method of Sichuan (Lu) braised fish is the same as that of steamed fish.
// Implement the cooking factory below
// Total cooking Factory
Class cookfactory
{
Public:
Cookfactory (void );
~ Cookfactory (void );
Virtual braisingfish * cookbraisingfish ();
Virtual steamedfish * cooksteamedfish ();
};
// Lu Cai Factory
Class lucookfactory: Public cookfactory
{
Public:
Lucookfactory (void );
~ Lucookfactory (void );
Virtual braisingfish * cookbraisingfish ();
Virtual steamedfish * cooksteamedfish ();
};
// Implementation of Lu Cai Factory
Braisingfish * lucookfactory: cookbraisingfish ()
{
STD: cout <"the Lu braisingfish is cooked! "<STD: Endl;
Return new lubraisingfish ();
}
Steamedfish * lucookfactory: cooksteamedfish ()
{
STD: cout <"the Lu stramedfish is cooked! "<STD: Endl;
Return new lusteamedfish ();
}
// Sichuan food factory
Class chuancookfactory: Public cookfactory
{
Public:
Chuancookfactory (void );
~ Chuancookfactory (void );
Virtual braisingfish * cookbraisingfish ();
Virtual steamedfish * cooksteamedfish ();
};
// Implementation of Sichuan food factory
Braisingfish * chuancookfactory: cookbraisingfish ()
{
STD: cout <"The Chuan braisingfish is cooked! "<STD: Endl;
Return new chuanbraisingfish ();
}
Steamedfish * chuancookfactory: cooksteamedfish ()
{
STD: cout <"The Chuan steamedfish is cooked! "<STD: Endl;
Return new chuansteamedfish ();
}
// The following is the implementation of the Client
Int main (void)
{
Cookfactory * cookfac = new lucookfactory ();
Braisingfish * brafish = cookfac-> cookbraisingfish ();
Brafish-> delicious ();
STD :: cout <"************************************ * *****/N ";
Delete brafish;
Steamedfish * stefish = cookfac-> cooksteamedfish ();
Stefish-> delicious ();
STD :: cout <"************************************ * *****/N ";
Delete stefish;
Delete cookfac;
Cookfac = new chuancookfactory ();
Brafish = cookfac-> cookbraisingfish ();
Brafish-> delicious ();
STD :: cout <"************************************ * *****/N ";
Delete brafish;
Stefish = cookfac-> cooksteamedfish ();
Stefish-> delicious ();
Delete stefish;
System ("pause ");
Return 0;
}
So far, the abstract factory model has been completed.
Now we can compare it with the factory model. The interface returned by the abstract factory is no longer the common base class product of product A and product B, but the base class of product A and product B (in the factory model ).
Are specific implementation classes, and the base classes are changed here. At this time, the factory abstraction is similar to the factory method in a simple factory, that is, these feature areas make it different from the factory model to the smoke
Like the factory model, the abstract factory solves the problem of creating a series of products with the same style (LU cuisine or Guangdong cuisine), while the factory method solution creates a series of products with the same features.
(Braised pork and steamed fish are all foods ). Of course, the defects of the simple factory have appeared again in the abstract factory. If I want to add a new product, the abstract interface of the factory will change. Therefore
Abstract factories are not perfect than factory models, but they are different in their respective application fields. In fact, if the abstract factory interface is returned to the common base class of product A and product B
(Parameters returned by the factory mode), you will find that it is not just degraded to the factory mode, wondering how familiar this mode is.
Differences between class mode and Object Mode
". Analysis: first, they do not create objects in the base class, they are all implemented in the subclass, so they all comply with the class mode concept; but the product object created in the factory mode is compiled in
It is determined by the translation period that a fixed factory should be called, while the abstract factory mode dynamically determines the product creation at runtime. Only at runtime can the factory be called, call Factory
Changes with the running environment.
Applicability:
- A system must be independent of the creation, combination, and representation of its products.
- When a system needs to be configured by one of multiple product generations
- When you want to emphasize the design of a series of related product objects for joint use
- When you provide a product class library, instead of displaying their interfaces.