(4 pieces of news) The factory also needs to take the skull when moving bricks! Jungle learn abstract Factory pattern _ Von Jungle’s personal blog -CSDN blog

Abstract factory schema structure

The abstract factory pattern structure is similar to the factory method pattern structure, except that a specific factory can produce many similar and related products:

  • AbstractFactory: A base class for all factory classes that produce concrete products, providing common methods for the factory class;
  • Concrete Factories: Produce concrete products
  • AbstractProduct: A base class for all products that provides common methods for the product class
  • Concrete products: ConcreteProduct classes

The abstract factory pattern UML class diagram looks like this:

Combining abstract factory pattern definition and UML, we can see that concrete ConcreteFactory_A can produce two products, namely, ConcreteProduct_A_1 and ConcreteProduct_A_2, and the same for another concrete factory ConcreteFactory_B. When used by the client, you need to declare an AbstractFactory, AbstractFactory, and two abstract products, AbstractProduct.

3. Abstract factory pattern code instances

Consider the following scenario:

Jungle wants to play outdoors and he can choose to play basketball or football. But this time Jungle doesn’t want to get his shirt dirty, so Jungle needs to wear his jersey. He’ll wear his basketball shirt for basketball and his football shirt for football. The basketball storage room can provide basketball and basketball jerseys, and the football storage room can provide football and football jerseys. Jungle can just go to a storage room depending on his mood, put on his jersey, grab his ball and have fun.

The corresponding UML example diagram is shown below:

3.1. Define product classes

3.1.1. Product Category: Ball

  • Abstract product class *AbstractBall, the base class of the ball class, defines the abstract method Play
Abstract product class AbstractBall
class AbstractBall
{
public:
	AbstractBall() {}// Abstract method:
	void play(a){};
};
Copy the code
  • The specific product class is Basketball and Football respectively, and the specific implementation method is Play
// The specific product class is Basketball
class Basketball :public AbstractBall
{
public:
	Basketball() {play(a); }// How to implement it
	void play(a){
		printf("Jungle play Basketball\n\n"); }};// Specific product class Football
class Football :public AbstractBall
{
public:
	Football() {play(a); }// How to implement it
	void play(a){
		printf("Jungle play Football\n\n"); }};Copy the code

3.1.2. Product Shirt

  • Abstract product class *AbstractShirt: *A base class for the jersey class that defines the abstract method wearShirt
Abstract product class AbstractShirt
class AbstractShirt
{
public:
	AbstractShirt() {}// Abstract method:
	void wearShirt(a){};
};
Copy the code
  • The specific product category is BasketballShirt and FootballShirt, and the specific implementation method is wearShirt
// Specific product class BasketballShirt
class BasketballShirt :public AbstractShirt
{
public:
	BasketballShirt() {wearShirt(a); }// How to implement it
	void wearShirt(a){
		printf("Jungle wear Basketball Shirt\n\n"); }};// FootballShirt
class FootballShirt :public AbstractShirt
{
public:
	FootballShirt() {wearShirt(a); }// How to implement it
	void wearShirt(a){
		printf("Jungle wear Football Shirt\n\n"); }};Copy the code

3.2. Define the factory class

  • Defining an Abstract factoryAbstractFactory, declare two methods getBall and getShirt
// Abstract factory class
class AbstractFactory
{
public:
	virtual AbstractBall *getBall(a) = 0;
	virtual AbstractShirt *getShirt(a) = 0;
};
Copy the code
  • Define BasketballFactory and FootballFactory, and re-implement two methods getBall and getShirt
// BasketballFactory class
class BasketballFactory :public AbstractFactory
{
public:
	BasketballFactory() {printf("BasketballFactory\n");
	}
	AbstractBall *getBall(a){
		printf("Jungle get basketball\n");
		return new Basketball(a); }AbstractShirt *getShirt(a){
		printf("Jungle get basketball shirt\n");
		return new BasketballShirt();
	}
};
 
// BasketballFactory class
class FootballFactory :public AbstractFactory
{
public:
	FootballFactory() {printf("FootballFactory\n");
	}
	AbstractBall *getBall(a){
		printf("Jungle get football\n");
		return new Football(a); }AbstractShirt *getShirt(a){
		printf("Jungle get football shirt\n");
		return new FootballShirt();
	}
};
Copy the code

3.3. Examples of client usage

#include <iostream>
#include "AbstractFactory.h"
 
int main(a)
{
	printf("Abstract Factory pattern \n");
	
	// Define factory and product objects
	AbstractFactory *fac = NULL;
	AbstractBall *ball = NULL;
	AbstractShirt *shirt = NULL;
 
	fac = new BasketballFactory(a); ball = fac->getBall(a); shirt = fac->getShirt(a); fac =new FootballFactory(a); ball = fac->getBall(a); shirt = fac->getShirt(a);system("pause");
	return 0;
}
Copy the code

Effect of 3.4.