C ++ design patterns: Factory Pattern

       Factory pattern (Factory Pattern) is one of the most commonly used design patterns, are creational patterns, he offers the best way to create objects. Factory mode, when we create an object, not exposed to create a logical, and by calling a unified interface to point to the newly created object.

When to use: We plan to create different objects under different conditions.

 Code Example:

// Shape.h
#pragma once


class Shape
{
public:
	Shape();
	~Shape();
	virtual void draw() = 0;
};

// Shape.cpp
#include "Shape.h"

Shape::Shape()
{
}


Shape::~Shape()
{
}

// Circle.h
#pragma once
#include "Shape.h"
class Circle :public Shape
{
public:
	Circle();
	~Circle();
	virtual void draw();
};

// Circle.cpp
#include "Circle.h"
#include <iostream>
using namespace std;

Circle::Circle()
{
}


Circle::~Circle()
{
}

void Circle::draw()
{
	cout << "Circle::draw()\n";
}

// Square.h
#pragma once
#include "Shape.h"
class Square :
	public Shape
{
public:
	Square();
	~Square();
	virtual void draw();
};

// Square.cpp
#include "Square.h"
#include <iostream>
using namespace std;


Square::Square()
{
}


Square::~Square()
{
}

void Square::draw()
{
	cout << "Square::draw()\n";
}

// Rectangle.h
#pragma once
#include "Shape.h"
class Rectangle :
	public Shape
{
public:
	Rectangle();
	~Rectangle();
	virtual void draw();
};


// Rectangle.cpp
#include "Rectangle.h"
#include <iostream>
using namespace std;


Rectangle::Rectangle()
{
}


Rectangle::~Rectangle()
{
}

void Rectangle::draw()
{
	cout << "Rectangel::draw()\n";
}

// ShapeFatory.h
#pragma once
enum ShapeType
{
	Circle_Type,
	Square_Type,
	Rectangle_Type
};
class Shape;

class ShapeFatory
{
public:
	ShapeFatory();
	~ShapeFatory();
	Shape *getShape(ShapeType shapeType);
};


// ShapeFatory.cpp
#include "ShapeFatory.h"
#include "Shape.h"
#include "Circle.h"
#include "Rectangle.h"
#include "Square.h"


ShapeFatory::ShapeFatory()
{
}


ShapeFatory::~ShapeFatory()
{
}

Shape * ShapeFatory::getShape(ShapeType shapeType)
{
	Shape *pShape = nullptr;
	switch (shapeType)
	{
	case Circle_Type:
		pShape =  new Circle();
		break;
	case Square_Type:
		pShape = new Square();
		break;
	case Rectangle_Type:
		pShape = new Rectangle();
		break;
	default:
		break;
	}
	return pShape;
}#include "ShapeFatory.h"
#include "Shape.h"
#include "Circle.h"
#include "Rectangle.h"
#include "Square.h"


ShapeFatory::ShapeFatory()
{
}


ShapeFatory::~ShapeFatory()
{
}

Shape * ShapeFatory::getShape(ShapeType shapeType)
{
	Shape *pShape = nullptr;
	switch (shapeType)
	{
	case Circle_Type:
		pShape =  new Circle();
		break;
	case Square_Type:
		pShape = new Square();
		break;
	case Rectangle_Type:
		pShape = new Rectangle();
		break;
	default:
		break;
	}
	return pShape;
}


//  main.cpp
#include <iostream>

#include "Circle.h"
#include "Rectangle.h"
#include "Square.h"
#include "ShapeFatory.h"
#include <memory>
using namespace std;

void main()
{
	ShapeFatory * pShapeFactory = new ShapeFatory;// 创建工厂类对象
	
	Shape *p1 = pShapeFactory->getShape(Circle_Type);// 调用统一接口创建Circle类
	p1->draw();// 实际调用circle类的画图方法

	Shape *p2 = pShapeFactory->getShape(Square_Type);
	p2->draw();

	Shape *p3 = pShapeFactory->getShape(Rectangle_Type);
	p3->draw();

	delete p1;
	delete p2;
	delete p3;
	delete pShapeFactory;
	
	int a;
	cin >> a;
}

Result of the program:

Circle::draw()
Square::draw()
Rectangel::draw()

Published 257 original articles · won praise 22 · views 90000 +

Guess you like

Origin blog.csdn.net/qq_24127015/article/details/105247193