工厂模式
#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