当前位置:网站首页>Detailed explanation of three factory modes
Detailed explanation of three factory modes
2022-07-25 03:16:00 【An Ruoxi~】
1、 Factory method model
1.1、 Simple factory model
Definition : Defines a class for creating objects , This class encapsulates the behavior of the instantiated object .
give an example :( Let's take an example of a fruit factory )
The fruit factory produces three types of fruits :apple,pear,banana. Through factory class (Factory) Instantiate these three types of objects . The class diagram is as follows :
#include<iostream>
using namespace std;
class Produce;// Declare product classes
class Factory// Factory
{
public:
Factory() {}
~Factory() {}
virtual Produce* create_produce(string class_name, string name) = 0;
// The factory produces products , The pure virtual function here is to make the sub factory produce products
// What is returned is a product class pointer , This represents a product
// there name Parameter is to return the desired product qwq
};
class Produce
{
public:
Produce() {}
~Produce() {}
virtual void show_myname() = 0;// The pure virtual function defined here is for the derived class to implement this function , Classes with pure virtual functions are abstract classes
};
// Now I'm going to start creating specific products
class Apple :public Produce// This is a specific Apple
{
private:
string name;
public:
Apple(string new_name = "")
{
this->name = new_name;
}
virtual void show_myname()// Override the parent function
{
cout << " My name is :" << name << endl;
}
};
class Pear :public Produce// This is a specific pear
{
private:
string name;
public:
Pear(string new_name = "")
{
this->name = new_name;
}
virtual void show_myname()// Override the parent function
{
cout << " My name is :" << name << endl;
}
};
class Badfruits :public Produce// This is a specific kind of bad fruit
{
private:
string name;
public:
Badfruits() {}
virtual void show_myname()// Override the parent function
{
cout << " This product has expired !!" << endl;
}
};
// Now let's start to create a specific factory class
class Factory_fruits :public Factory// This is a fruit factory
{
public:
Factory_fruits() {}
~Factory_fruits() {}
virtual Produce* create_produce(string class_name, string name)
{
if (class_name == "apple")
{
Produce* my_produce = new Apple(name);// establish name Of apple product
return my_produce;
}
else if (class_name == "pear")
{
Produce* my_produce = new Pear(name);// establish name Of pear product
return my_produce;
}
else
{
Produce* my_produce = new Badfruits();// establish name Of pear product
return my_produce;
}
}
};
// The initial construction work has been completed , To sum up , We built two abstract classes : Factory , Product class ;
// Product derived classes : Apple , Pears , Bad fruits
// Factory derived classes : Fruit factories .
// Now we are going to use these things
int main()
{
Factory* my_factory_fruits = new Factory_fruits();// Create an abstract factory object " upgrade " For the fruit factory
// there " upgrade " It's my understanding , In fact, it's type conversion
Produce* my_produce_apple = my_factory_fruits->create_produce("apple", " Red Fuji ");
// Create abstract product objects ," upgrade " Processed for fruit factories apple, Red Fuji
Produce* my_produce_pear = my_factory_fruits->create_produce("pear", " sweet stewed snow pear ");
// Create abstract product objects ," upgrade " Processed for fruit factories pear, sweet stewed snow pear
Produce* my_produce_banana = my_factory_fruits->create_produce("banana", " big banana ");
// Create abstract product objects ," upgrade " Processed for fruit factories banana, big banana , But the factory cannot produce banana, So we can only produce badfruit Bad fruit
my_produce_apple->show_myname();
// The product displays its name
my_produce_pear->show_myname();
// The product displays its name
my_produce_banana->show_myname();
// The product displays its name
// The following is destroying memory
delete my_factory_fruits;
my_factory_fruits = nullptr;
delete my_produce_apple;
my_produce_apple = nullptr;
delete my_produce_pear;
my_produce_pear = nullptr;
delete my_produce_banana;
my_produce_banana = nullptr;
return 0;
}
This simple factory model has a big flaw , As the name suggests, factory mode , It's not that simple >_<. He is not like a simple factory, a fruit factory can produce all kinds of fruits , Instead, a factory produces a kind of fruit . For example, apple factory produces apples , Pear factory produces pears . What are the benefits of this separation ???
I read many blogs , They all mentioned to open up - Closed principle , In my own words , If you want to add new banana products . Using simple factory mode requires adding a banana , Then add branches in the fruit factory . In the factory mode, you need to add a banana factory and a banana factory ...
But our teacher often teaches us , Don't change the source code !!! If you use the simple factory mode, you must change the conditional branch in the source code , This will be scolded to death by the teacher qwq. Therefore, using the factory mode does not need to change the source code , Just add code .
1.2、 Factory mode
#include<iostream>
using namespace std;
class Produce;// Declare product classes
class Factory// Factory
{
public:
Factory() {}
~Factory() {}
virtual Produce* create_produce(string name) = 0;
// The factory produces products , The pure virtual function here is to make the sub factory produce products
// What is returned is a product class pointer , This represents a product
// there name Parameter is to return the desired product qwq
};
class Produce
{
public:
Produce() {}
~Produce() {}
virtual void show_myname() = 0;// The pure virtual function defined here is for the derived class to implement this function , Classes with pure virtual functions are abstract classes
};
// Now I'm going to start creating specific products
class Apple :public Produce// This is a specific Apple
{
private:
string name;
public:
Apple(string new_name = "")
{
this->name = new_name;
}
virtual void show_myname()// Override the parent function
{
cout << " My name is :" << name << endl;
}
};
class Pear :public Produce// This is a specific pear
{
private:
string name;
public:
Pear(string new_name = "")
{
this->name = new_name;
}
virtual void show_myname()// Override the parent function
{
cout << " My name is :" << name << endl;
}
};
class Banana :public Produce// This is a specific banana
{
private:
string name;
public:
Banana(string new_name = "")
{
this->name = new_name;
}
virtual void show_myname()// Override the parent function
{
cout << " My name is :" << name << endl;
}
};
// Now let's start to create a specific factory class
class Factory_apple :public Factory// This is a fruit factory
{
public:
Factory_apple() {
}
~Factory_apple() {
}
virtual Produce* create_produce(string name)
{
Produce* my_produce = new Apple(name);// establish name Of apple product
return my_produce;
}
};
class Factory_pear :public Factory// This is a fruit factory
{
public:
Factory_pear() {
}
~Factory_pear() {
}
virtual Produce* create_produce(string name)
{
Produce* my_produce = new Pear(name);// establish name Of pear product
return my_produce;
}
};
class Factory_banana :public Factory// This is banana
{
public:
Factory_banana() {
}
~Factory_banana() {
}
virtual Produce* create_produce(string name)
{
Produce* my_produce = new Banana(name);// establish name Of badfruits product
return my_produce;
}
};
// The initial construction work has been completed , To sum up , We built two abstract classes : Factory , Product class ;
// Product derived classes : Apple , Pears , Bad fruits
// Factory derived classes : Apple factory class . Pear factory . Bad fruit factory
// Now we are going to use these things
int main()
{
Factory* my_factory_apple = new Factory_apple();// Create an abstract factory object " upgrade " For Apple factory
// there " upgrade " It's my understanding , In fact, it's type conversion
Factory* my_factory_pear = new Factory_pear();// Create an abstract factory object " upgrade " For pear factory
Factory* my_factory_banana = new Factory_banana();// Create an abstract factory object " upgrade " For banana factory
Produce* my_produce_apple = my_factory_apple->create_produce(" Red Fuji ");
// Create abstract product objects ," upgrade " Processed for Apple factory apple, Red Fuji
Produce* my_produce_pear = my_factory_pear->create_produce(" sweet stewed snow pear ");
// Create abstract product objects ," upgrade " Processed for pear factory pear, sweet stewed snow pear
Produce* my_produce_banana = my_factory_banana->create_produce(" big banana ");
// Create abstract product objects ," upgrade " Processed for banana factory banana
my_produce_apple->show_myname();
// The product displays its name
my_produce_pear->show_myname();
// The product displays its name
my_produce_banana->show_myname();
// The product displays its name
// The following is destroying memory
delete my_factory_apple;
my_factory_apple = nullptr;
delete my_factory_pear;
my_factory_pear = nullptr;
delete my_factory_banana;
my_factory_banana = nullptr;
delete my_produce_apple;
my_produce_apple = nullptr;
delete my_produce_pear;
my_produce_pear = nullptr;
delete my_produce_banana;
my_produce_banana = nullptr;
return 0;
}
2、 Abstract factory pattern
As the name suggests, this pattern is a little more abstract than the ordinary factory pattern .
According to my personal understanding , Simple factory model , Factory mode , The main difference between abstract patterns and factory classification .
Simple factory model : One product, one factory , One factory for ten products . summary : The factory can produce all products
Factory mode : One product, one factory , Ten products, ten factories . summary : The factory can only produce one product
Abstract factory pattern :N A product M Factories . summary : Factories can produce goods flexibly .
Or according to the example just now , I want to sell apples now , Apple Juice , pears , Pear juice .
The simple factory model is 1 Four factories sell four products .
The factory model is to create four factories and sell four products .
The abstract factory pattern is different : I can create two factories . One that specializes in selling apples , A special pear seller , In addition to this classification , Other classifications can also be made , Very flexible !
Definition : Defines an interface for creating related or dependent object families , There is no need to specify a specific class .
give an example :( We still take the example of a fruit factory )
#include<iostream>
using namespace std;
class Produce_fruits;// Declare fruit products
class Produce_fruits_juice;// Declare fruit juice products
class Factory// Factory
{
public:
Factory() {}
~Factory() {}
virtual Produce_fruits* create_produce(string name) = 0;
// The factory produces products , The pure virtual function here is to make the sub factory produce products
// What is returned is a product class pointer , This represents a product
// there name Parameter is to return the desired product qwq
virtual Produce_fruits_juice* create_produce_juice(string name) = 0;
// The factory produces products , The pure virtual function here is to make the sub factory produce products
// What is returned is a product class pointer , This represents a product
// there name Parameter is to return the desired product qwq
};
class Produce_fruits
{
public:
Produce_fruits() {}
~Produce_fruits() {}
virtual void show_myname() = 0;// The pure virtual function defined here is for the derived class to implement this function , Classes with pure virtual functions are abstract classes
};
class Produce_fruits_juice
{
public:
Produce_fruits_juice() {}
~Produce_fruits_juice() {}
virtual void show_myname() = 0;// The pure virtual function defined here is for the derived class to implement this function , Classes with pure virtual functions are abstract classes
};
// Now I'm going to start creating specific products
class Apple :public Produce_fruits// This is a specific Apple
{
private:
string name;
public:
Apple(string new_name = "")
{
this->name = new_name;
}
virtual void show_myname()// Override the parent function
{
cout << " My name is :" << name << "( Fruits )" << endl;
}
};
class Pear :public Produce_fruits// This is a specific pear
{
private:
string name;
public:
Pear(string new_name = "")
{
this->name = new_name;
}
virtual void show_myname()// Override the parent function
{
cout << " My name is :" << name << "( Fruits )" << endl;
}
};
class Produce_apple_juice :public Produce_fruits_juice// This is a specific Apple
{
private:
string name;
public:
Produce_apple_juice(string new_name = "")
{
this->name = new_name;
}
virtual void show_myname()// Override the parent function
{
cout << " My name is :" << name << "( Fruit juice )" << endl;
}
};
class Produce_pear_juice :public Produce_fruits_juice// This is a specific pear
{
private:
string name;
public:
Produce_pear_juice(string new_name = "")
{
this->name = new_name;
}
virtual void show_myname()// Override the parent function
{
cout << " My name is :" << name << "( Fruit juice )" << endl;
}
};
// Now let's start to create a specific factory class
class Factory_apple :public Factory// This is an apple factory class
{
public:
Factory_apple() {}
~Factory_apple() {}
virtual Produce_fruits* create_produce(string name)
{
Produce_fruits* my_produce = new Apple(name);// establish name Apple products
return my_produce;
}
virtual Produce_fruits_juice* create_produce_juice(string name)
{
Produce_fruits_juice* my_produce = new Produce_apple_juice(name);// establish name Apple juice products
return my_produce;
}
};
class Factory_pear :public Factory// This is a pear factory
{
public:
Factory_pear() {}
~Factory_pear() {}
virtual Produce_fruits* create_produce(string name)
{
Produce_fruits* my_produce = new Pear(name);// establish name Pear products
return my_produce;
}
virtual Produce_fruits_juice* create_produce_juice(string name)
{
Produce_fruits_juice* my_produce = new Produce_pear_juice(name);// establish name Pear juice products
return my_produce;
}
};
// The initial construction work has been completed , To sum up , We built two abstract classes : Factory , Fruit products , Fruit juice products ;
// Product derived classes : Apple , Pears , Apple juice , Pear juice ,
// Factory derived classes : Apple factory class , Pear factory .
// Now we are going to use these things
int main()
{
Factory* my_factory_apple = new Factory_apple();// Create an abstract factory object " upgrade " For Apple factory
// there " upgrade " It's my understanding , In fact, it's type conversion
Factory* my_factory_pear = new Factory_pear();// Create an abstract factory object " upgrade " For pear factory
// there " upgrade " It's my understanding , In fact, it's type conversion
Produce_fruits* my_produce_apple = my_factory_apple->create_produce(" Red Fuji ");
// Create abstract product objects ," upgrade " Red Fuji processed for Apple factory
Produce_fruits* my_produce_pear = my_factory_pear->create_produce(" Yali Pear ");
// Create abstract product objects ," upgrade " Yali Pear processed for pear factory
Produce_fruits_juice* my_produce_apple_juice = my_factory_apple->create_produce_juice(" Red apple juice ");
// Create abstract product objects ," upgrade " Red apple juice processed for Apple factory
Produce_fruits_juice* my_produce_pear_juice = my_factory_pear->create_produce_juice(" Rock sugar Sydney juice ");
// Create abstract product objects ," upgrade " Crystal sugar Sydney juice processed for pear factory
my_produce_apple->show_myname();
// The product displays its name
my_produce_pear->show_myname();
// The product displays its name
my_produce_apple_juice->show_myname();
// The product displays its name
my_produce_pear_juice->show_myname();
// The product displays its name
// The following is destroying memory
delete my_factory_apple;
my_factory_apple = nullptr;
delete my_factory_pear;
my_factory_pear = nullptr;
delete my_produce_apple;
my_produce_apple = nullptr;
delete my_produce_pear;
my_produce_pear = nullptr;
delete my_produce_apple_juice;
my_produce_apple_juice = nullptr;
delete my_produce_pear_juice;
my_produce_pear_juice = nullptr;
return 0;
}
3、 Summarize the applicable occasions and choices of factory mode
A large number of products need to be created , And these products have a common interface .
Simple factory : Used to produce any product in the same hierarchy .( We don't support expanding and adding products )
Factory method : Used to produce fixed products in the same hierarchy .( Support to expand and increase products )
Abstract factory : All the products used to produce different product families .( Support to expand and increase products ; Support to add product families )
Simple factory applications : Only London factory ( There is only one level ), And this factory only produces three types of pizza:chesse,pepper,greak( Fixed products ).
Applicable occasions of factory method : Now it's not just London factories , New York factory was also added ( It's still the same hierarchy , But it supports the expansion of products ), The two factories still produce only three types of pizza:chesse,pepper,greak( Fixed products ).
Application of abstract factory : Not just New York factories ( It's still the same hierarchy , But it supports the expansion of products ), The two factories have also added a new type of pizza:chinese pizza( Add product family ).
So an abstract factory is like a factory , The factory method is like a product line of a factory . therefore , We can create factories with abstract factory patterns , And using the factory method to create a production line . such as , We can use the abstract factory pattern to create factories in London and New York , Use the factory method to achieve cheese pizza and greak pizza The production of . The class diagram is as follows :
Summarize the three modes :
The simple factory pattern is to create a class that instantiates an object , Instantiate multiple objects in this class . The factory method pattern defines an abstract method for creating objects , It's up to the subclass to decide which class to instantiate . The advantage of this is that there are new types of objects that need to be instantiated. Just add subclasses . The abstract factory pattern defines an interface for creating object families , There is no need to specify a specific class . The abstract factory also gives the instantiation of objects to subclasses , That is, support the expansion of . At the same time, it provides the client interface , It avoids the direct operation of subclass factories by users .
##### characteristic :
Simple factory model : A factory N A product
Factory mode :N Factories N A product
Abstract factory pattern :N Factories M A product
##### shortcoming ( relative ):
Simple factory model : Against the principle of openness and closure ( Change the source code ), More products will be more miscellaneous
Factory mode : Too many products will be very miscellaneous , A little bit of code , More products will be more miscellaneous
Abstract factory pattern : Against the principle of openness and closure ( Change the source code ), A lot of code , More products will be very miscellaneous
##### advantage ( relative ):
Simple factory model : Small amount of code , Easy to add new products
Factory mode : It does not violate the open and closed principle ( Do not change the source code ), Easy to add new products
Abstract factory pattern : The model is flexible , Call convenient
边栏推荐
- kettle_ Configure database connection_ report errors
- A. Subtle Substring Subtraction
- DOM node type
- Bgy development small example
- Can bus baud rate setting of stm32cubemx
- Hal library serial port for note taking
- Use go language to import Doris data through stream load
- Method of adding kernel in Jupiter notebook
- Learning Record V
- Solve ''_ Xsrf 'argument missing from post
猜你喜欢

Use and introduction of vim file editor

Openlayers draw circles and ellipses

Question D: pruning shrubs

Select sort / cardinality sort

Resolved (the latest version of selenium reported an error) attributeerror: module 'selenium webdriver‘ has no attribute ‘PhantomJS‘

04 -- two ways of writing el and data

Learning record 10

Arduino IDE for raspberry PI Pico development firmware localization installation tutorial

Consistent hash, virtual node, bloom filter

List title of force buckle summary
随机推荐
Solve the error: could not find 'xxxtest‘
Map set learning
Matplotlib tutorial (I) [getting to know Matplotlib first]
Resolved (the latest version of selenium reported an error) attributeerror: module 'selenium webdriver‘ has no attribute ‘PhantomJS‘
[brother hero July training] day 19: binary tree
[jailhouse article] scheduling policies and system software architectures for mixed criticality
Page performance: how to optimize pages systematically?
List title of force buckle summary
JS written test question -- deep copy of object
Take a database statement note: when the field is empty, assign the default value to the result
FLASH read / write problem of stm32cubemx
Stm32cubemx quadrature encoder
B. Making Towers
Eslint error
Hyperchain hyperblockchain Shi Xingguo was interviewed by 36 krypton: the amount of customer cooperation consulting is doubling
mysql_ Account authorization permission recycling, account locking and unlocking, account creation and deletion
Easyexcel sets the style of the last row [which can be expanded to each row]
Web -- JDBC tool class writing
JS written test question -- prototype, new, this comprehensive question
NVM installation and use