C++ 设计模式:工厂模式

       工厂模式(Factory Pattern)是最常用的设计模式之一,属于创建型模式,他提供了一种创建对象的最佳方式。工厂模式,我们在创建对象时,不会暴露创建逻辑,并且是通过调用统一的接口来指向新创建的对象。

何时使用:我们计划在不同条件下创建不同的对象。

 代码示例:

// 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;
}

程序运行结果:

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

发布了257 篇原创文章 · 获赞 22 · 访问量 9万+

猜你喜欢

转载自blog.csdn.net/qq_24127015/article/details/105247193