广州做外贸网站的公司简介看车二手车网站源码
web/
2025/9/27 1:59:05/
文章来源:
广州做外贸网站的公司简介,看车二手车网站源码,内蒙古建设住房与城乡厅官方网站,鞋材 东莞网站建设介绍 定义一个工厂类#xff0c;它可以根据参数的不同返回不同类的实例#xff0c;被创建的实例通常都具有相同的父类。因为在简单工厂模式中用于创建实例的方法是静态方法#xff0c;因此简单工厂模式又被称为静态工厂方法模式#xff0c;属于类创建型模式
实现 class Pr…介绍 定义一个工厂类它可以根据参数的不同返回不同类的实例被创建的实例通常都具有相同的父类。因为在简单工厂模式中用于创建实例的方法是静态方法因此简单工厂模式又被称为静态工厂方法模式属于类创建型模式
实现 class Product {
public:void methodSame() { // 公共方法的实现std::cout methodSame std::endl;}virtual void methodDiff() 0; // 抽象方法的声明由具体产品实现
};class ConcreteProductA : public Product {
public:void methodDiff() override {std::cout ConcreteProductA std::endl;}
};class ConcreteProductB : public Product {
public:void methodDiff() override {std::cout ConcreteProductB std::endl;}
};class Factory {
public:static Product *getProduct(std::string arg) {Product *product NULL;if (arg A) {product new ConcreteProductA();} else if (arg B) {product new ConcreteProductB();}return product;}
};int main() {Product *A Factory::getProduct(A);A-methodSame();A-methodDiff();Product *B Factory::getProduct(B);B-methodSame();B-methodDiff();return 0;
}
简单工厂模式的简化 将Factory合并到父类Product中此时必须分别在头文件和源代码文件中分开编写不然编译报错
class Product {
public:void methodSame();virtual void methodDiff() 0; // 抽象方法的声明由具体产品实现static Product *getProduct(std::string arg);
};class ConcreteProductA : public Product {
public:void methodDiff() override;
};class ConcreteProductB : public Product {
public:void methodDiff() override;
};
#include myclass.hvoid Product::methodSame() { // 公共方法的实现std::cout methodSame std::endl;
}Product* Product::getProduct(std::string arg) {Product *product NULL;if (arg A) {product new ConcreteProductA();} else if (arg B) {product new ConcreteProductB();}return product;
}void ConcreteProductA::methodDiff() {std::cout ConcreteProductA std::endl;
}void ConcreteProductB::methodDiff() {std::cout ConcreteProductB std::endl;
}
总结 优点 1. 工厂类包含必要的判断逻辑可以决定什么时候创建哪一个产品类的实例。 2. 客户端无须知道所创建的具体产品类的类名只需要知道对应的参数即可。 3. 通过引入配置文件可以在不修改任何客户端代码的情况下更换和增加新的具体产品类在一定程度上提高了系统的灵活性。 缺点 1. 由于工厂类集中了所有产品的创建逻辑职责过重一旦不能正常工作整个系统都要受到影响。 2. 使用简单工厂模式势必会增加系统中类的个数引入了新的工厂类增加了系统的复杂度和理解难度。 3. 系统扩展困难。一旦添加新产品就不得不修改工厂逻辑在产品类型较多时有可能造成工厂逻辑过于复杂不利于系统的扩展和维护。 4. 简单工厂模式由于使用了静态工厂方法造成工厂角色无法形成基于继承的等级结构。
练习 使用简单工厂模式设计一个可以创建不同几何形状如圆形、方形和三角形等的绘图工具每个几何图形都具有绘制draw和擦除erase两个方法要求在绘制不支持的几何图形时提示一个UnSupportedShapeException。
myclass.h
//
// Created by yuwp on 2024/1/12.
//#ifndef DESIGNPATTERNS_CLASS_H
#define DESIGNPATTERNS_MYCLASS_H#include iostreamclass Graphical {
public:virtual void draw() 0;virtual void erase() 0;static Graphical *getGraphical(std::string arg);
};class Circle : public Graphical {
public:void draw() override;void erase() override;
};class Square : public Graphical {
public:void draw() override;void erase() override;
};class Triangle : public Graphical {
public:void draw() override;void erase() override;
};class UnSupportedShapeException : public std::exception {
public:UnSupportedShapeException() {};~UnSupportedShapeException() {};const char* what() const _GLIBCXX_TXN_SAFE_DYN _GLIBCXX_USE_NOEXCEPT override;
};#endif //DESIGNPATTERNS_CLASS_Hmyclass.cpp
//
// Created by yuwp on 2024/1/12.
//#include myclass.hGraphical* Graphical::getGraphical(std::string arg) {if (arg circle) {return new Circle();} else if (arg square) {return new Square();} else if (arg triangle) {return new Triangle();} else {throw UnSupportedShapeException();}
}void Circle::draw() {std::cout Draw circle std::endl;
}void Circle::erase() {std::cout Erase circle std::endl;
}void Square::draw() {std::cout Draw square std::endl;
}void Square::erase() {std::cout Erase square std::endl;
}void Triangle::draw() {std::cout Draw triangle std::endl;
}void Triangle::erase() {std::cout Erase triangle std::endl;
}const char* UnSupportedShapeException::what() const _GLIBCXX_TXN_SAFE_DYN _GLIBCXX_USE_NOEXCEPT {return Unsupported shape;
}
main.cpp
#include iostream
#include mutex
#include myclass.hint main() {Graphical *graph;try {graph Graphical::getGraphical(circle);graph-draw();graph-erase();graph Graphical::getGraphical(square);graph-draw();graph-erase();graph Graphical::getGraphical(triangle);graph-draw();graph-erase();graph Graphical::getGraphical(unknown);graph-draw();graph-erase();} catch (const std::exception e) {std::cout e.what() std::endl;}return 0;
}
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/web/82480.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!