首页 > 其他分享 >设计模式-工厂模式/抽象工厂模式

设计模式-工厂模式/抽象工厂模式

时间:2023-02-07 12:36:09浏览次数:37  
标签:draw 设计模式 模式 工厂 Shape include public

工厂模式

#include <iostream>
#include <string>
using namespace std;
class Shape {

public:
	virtual void draw() = 0;
};

class Rectangle : public Shape {
public:
	void draw() {
		std::cout << "Rectangle draw" << std::endl;
	}
};
class Square : public Shape
{
public:
	void draw() {
		std::cout << "Square draw()" << std::endl;
	}
};
class Circle : public Shape {
public:
	void draw() {
		std::cout << "Circle draw()" << std::endl;
	}
};

class ShapeFactory {
public:
	Shape* getShape(string shapetype) {
		if (shapetype == "") return NULL;
		if (shapetype == "Circle" || shapetype == "CIRCLE" || shapetype == "circle")
			return new Circle;
		if (shapetype == "Rectangle") return new Rectangle;
		if (shapetype == "Square") return new Square;
		return NULL;
	}
	void facoryWork() {
		cout << "工厂模式:" << endl;
		Shape* shape1 = this->getShape("circle");
		Shape* shape2 = this->getShape("Rectangle");
		Shape* shape3 = this->getShape("Square");
		shape1->draw();
		shape2->draw();
		shape3->draw();
		return;
	}
};

抽象工厂模式

#include <iostream>
#include <string>
using namespace std;
//形状 虚函数  椭圆 圆形 长方形
//颜色 虚函数  绿色 红色
//工厂 虚函数 形状工厂 颜色工厂
// 新建 一个具体工厂(颜色工厂)-》画 绿色
class Shape {

public:
	virtual void draw() = 0;
};

class Rectangle : public Shape {
public:
	void draw() {
		std::cout << "Rectangle draw" << std::endl;
	}
};
class Square : public Shape
{
public:
	void draw() {
		std::cout << "Square draw()" << std::endl;
	}
};
class Circle : public Shape {
public:
	void draw() {
		std::cout << "Circle draw()" << std::endl;
	}
};


class Color { public: virtual void fill() = 0; };
class Red : public Color { public: void fill() { cout << "Red fill" << endl; } };
class Green : public Color { public: void fill() { cout << "Green fill" << endl; } };

class AbstructFactory{
public:
	virtual Color* getColor(string color) = 0;
	virtual Shape* getShape(string shape) = 0;
};


class ShapeFactory : public AbstructFactory {
public:
	Shape* getShape(string shapetype) {
		if (shapetype == "") return NULL;
		if (shapetype == "Circle" || shapetype == "CIRCLE" || shapetype == "circle")
			return new Circle;
		if (shapetype == "Rectangle") return new Rectangle;
		if (shapetype == "Square") return new Square;
		return NULL;
	}
	Color* getColor(string color) {
		return NULL;
	}
        
};
class ColorFactory : public AbstructFactory {
public:
	Shape * getShape(string shape) {
		return NULL;
	}
	Color* getColor(string colorType) {
		if (colorType == "") return NULL;
		if (colorType == "Green") return new Green;
		if (colorType == "Red") return new Red;
		return NULL;
	}
};

class FactoryProducer{
public:
	AbstructFactory* getFactory(string factoryType) {
		if (factoryType == "Shape") return new ShapeFactory;
		if (factoryType == "Color") return new ColorFactory;
	}
	void abstructFactoryWork() {
		cout << "抽象工厂模式" << endl;
		AbstructFactory* shapeFacory = this->getFactory("Shape");
		Shape *shape1 = shapeFacory->getShape("circle");
		Shape *shape2 = shapeFacory->getShape("Rectangle");
		Shape *shape3 = shapeFacory->getShape("Square");
		shape1->draw();
		shape2->draw();
		shape3->draw();

		AbstructFactory * colorFactory = this->getFactory("Color");
		Color *color1 = colorFactory->getColor("Red");
		Color *color2 = colorFactory->getColor("Green");
		color1->fill();
		color2->fill();
		
	}
};

*main function

#pragma once
#include <iostream>
//#include "工厂模式.cpp"
#include "抽象工厂模式.cpp"
//#include "单例模式.cpp"
#include "builderModle.h"
using namespace std;

//设计模式
int main() {

	//1 工厂模式
	//ShapeFactory* shape = new ShapeFactory;
	//shape->facoryWork();
	//2 【抽象工厂模式
	FactoryProducer *abstructFactory = new FactoryProducer;
	abstructFactory->abstructFactoryWork();

	//3单例模式
	//SimpleSingleObject *obj1 = SimpleSingleObject::getInstance();
	//SimpleSingleObject *obj2 = SimpleSingleObject::getInstance();

	//4建造者模式
	/*Builder *xiaomiEngerneer = new xiaomiBuilder();
	Director *director = new Director(xiaomiEngerneer);
	//director->getbuilder(xiaomiEngerneer);
	director->create(xiaomiEngerneer);
	Phone *m_phone = xiaomiEngerneer->getPhone();
	m_phone->displayPhone();*/

	cout << "hello" << endl;

	return 0;
}

标签:draw,设计模式,模式,工厂,Shape,include,public
From: https://www.cnblogs.com/bell123/p/17097974.html

相关文章

  • 【Appium_python】启动app,出现多次打开关闭导致失败问题,driver用单例模式(_new_)进行解
    运用多设备,启动app多次出现打开又关闭问题,查看后是多次对driver进行实例化,就用单例的模式进行解决。单例模式(SingletonPattern)目的就是保证一个类仅有一个实例,每一次执行......
  • 工厂模式-go语言实现
    一、理论知识工厂模式的作用就是用来创建对象,细分为三种:简单工厂、工厂方法、抽象工厂。1.1应用场景工厂模式一般用于对于不同的场景,需要创建不同的对象,但是这些对象实......
  • 单例模式
      一个类永远只能创建一个对象,例如任务管理器我们只要一个就可以解决问题了,这样可以节省内存空间。单例的实现方式很多:比如饿汉单例模式和懒汉单例模式....................
  • 搞懂设计模式——代理模式 + 原理分析
    作者:京东零售秦浩然引子举个栗子,众所周知,我们是可以在京东上购买机票的。但机票是航司提供的,我们本质上是代理销售而已。那为什么航司要让我们代理销售呢?我们又是如帮他做......
  • java合成模式之神奇的树结构
    目录什么是合成模式安全式合成模式抽象构件(Component)角色树叶构件(Leaf)角色树枝构件(Composite)角色使用透明式合成模式抽象构件(Component)角色树叶构件(......
  • 模板模式
    /*模板模式:解决某类事情的步骤有些是固定的,有些是会发生变化的,那么这时候我们可以为这类事情提供一个模板代码,从而提高效率。需求;编写一个计算程序运行时间的模板。模板......
  • 理解vuex -- vue的状态管理模式
    [b]vuex是什么?[/b]先引用vuex官网的话:[quote]Vuex是一个专为Vue.js应用程序开发的状态管理模式。它采用集中式存储管理应用的所有组件的状态,并以......
  • 设计模式-策略模式升级(服务定位器模式)
    设计模式-策略模式(服务定位器模式)前言:正文开始前先抛出一个问题,项目开发中如果让你设计支付模块,目前有支付宝、微信、各大银行的部分,你会如何设计支付这块的代码?在调用的......
  • vim的visual模式和列编辑
    有三种方式进入visual模式:1>在普通模式下输入v(小写),底部提示信息为VISUAL,编辑粒度为字符  通过方向键或者HJKL调整选择的字符范围。输入d,删除选中字符;输入y,复制当......
  • 互联网医院app开发|互联网医院在线医疗新模式
     在互联网飞速发展的今天,互联网医院app已经成为当今医疗行业服务用户的一种必备模式,很多医院也已经启用来互联网医院,不仅方便了患者,也方便了医生。那么互联网医院app开发......