c++设计模式-工厂模式

本文共有4152个字,关键词:c++设计模式

详细的理论知识可以参考菜鸟教程-工厂模式

芒果也是在菜鸟学习的,对应的教程使用的编程语言为java,一边学习一边实现其c++版本。详细的理论知识芒果不在赘述,菜鸟教程已经写的非常棒了,这里只是实现其c++的版本。

实现步骤

  1. 创建一个接口
//【1】创建一个接口
class Shape
{
public:
    virtual void draw() = 0;
};
  1. 创建接口的实现类
//【2】创建接口的实现类
class Rectangle : public Shape
{
public:
    void draw() override
    {
        std::cout << "Inside Rectangle::draw() method." << std::endl;
    }
};

class Square : public Shape
{
public: 
    void draw() override
    {
        std::cout << "Inside Square::draw() method." << std::endl;
    }
};

class Circle : public Shape
{
public:
    void draw() override
    {
        std::cout << "Inside Circle::draw() method." << std::endl;
    }
};
  1. 创建一个工厂,生成基于给定信息的实体类对象

//【3】创建一个工厂,生成基于给定信息的实体类对象
class ShapeFactory
{
    //使用getShape方法获取形状类型的对象
public:
    Shape* getShape(std::string shape_type)
    {
        if (shape_type.empty())
        {
            return nullptr;
        }
        else if (shape_type == "CIRClE")
        {
            return new Circle();
        }
        else if(shape_type == "RECTANGLE")
        {
            return new Rectangle();
        }
        else if(shape_type == "SQUARE")
        {
            return new Square();
        }
        else
        {
            return nullptr;
        }
    }
};
  1. 使用该工厂,通过传递类型信息来获取实体类的对象。
int main()
{
    //使用该工厂,通过传递类型信息来获取实体类的对象
    ShapeFactory* shape_factory = new ShapeFactory();

    //获取Circle的对象,并调用它的draw方法
    Shape* circle_shape = shape_factory->getShape("CIRClE");
    circle_shape->draw();

    //获取Rectangle对象,并调用它的draw方法
    Shape* rect_shape = shape_factory->getShape("RECTANGLE");
    rect_shape->draw();

    //获取Square对象,并调用它的draw方法
    Shape* square_shape = shape_factory->getShape("SQUARE");
    square_shape->draw();

    return 0;
}
  1. 执行程序,输出结果

    Inside Circle::draw() method.
    Inside Rectangle::draw() method.
    Inside Square::draw() method.

完整代码

// FactoryPattern.cpp : This file contains the 'main' function. Program execution begins and ends there.
//

#include "pch.h"
#include <iostream>
#include<cstring>

//【1】创建一个接口
class Shape
{
public:
    virtual void draw() = 0;
};

//【2】创建接口的实现类
class Rectangle : public Shape
{
public:
    void draw() override
    {
        std::cout << "Inside Rectangle::draw() method." << std::endl;
    }
};

class Square : public Shape
{
public: 
    void draw() override
    {
        std::cout << "Inside Square::draw() method." << std::endl;
    }
};

class Circle : public Shape
{
public:
    void draw() override
    {
        std::cout << "Inside Circle::draw() method." << std::endl;
    }
};

//【3】创建一个工厂,生成基于给定信息的实体类对象
class ShapeFactory
{
    //使用getShape方法获取形状类型的对象
public:
    Shape* getShape(std::string shape_type)
    {
        if (shape_type.empty())
        {
            return nullptr;
        }
        else if (shape_type == "CIRClE")
        {
            return new Circle();
        }
        else if(shape_type == "RECTANGLE")
        {
            return new Rectangle();
        }
        else if(shape_type == "SQUARE")
        {
            return new Square();
        }
        else
        {
            return nullptr;
        }
    }
};
int main()
{
    //使用该工厂,通过传递类型信息来获取实体类的对象
    ShapeFactory* shape_factory = new ShapeFactory();

    //获取Circle的对象,并调用它的draw方法
    Shape* circle_shape = shape_factory->getShape("CIRClE");
    circle_shape->draw();

    //获取Rectangle对象,并调用它的draw方法
    Shape* rect_shape = shape_factory->getShape("RECTANGLE");
    rect_shape->draw();

    //获取Square对象,并调用它的draw方法
    Shape* square_shape = shape_factory->getShape("SQUARE");
    square_shape->draw();

    return 0;
}

// Run program: Ctrl + F5 or Debug > Start Without Debugging menu
// Debug program: F5 or Debug > Start Debugging menu

// Tips for Getting Started: 
//   1. Use the Solution Explorer window to add/manage files
//   2. Use the Team Explorer window to connect to source control
//   3. Use the Output window to see build output and other messages
//   4. Use the Error List window to view errors
//   5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project
//   6. In the future, to open this project again, go to File > Open > Project and select the .sln file
版权声明:本文为作者原创,如需转载须联系作者本人同意,未经作者本人同意不得擅自转载。
 windows编译opencv4.0 没有了 
添加新评论
暂无评论