当前位置: 首页 > news >正文

上海html5网站制作北京建设工程继续教育网站

上海html5网站制作,北京建设工程继续教育网站,珠海微网站进入,网络营销方案3000字原型模式(Prototype Pattern) 原型模式是一种创建型设计模式,它允许你通过复制现有对象来创建新的对象,而不是通过类实例化来创建对象。这种模式在开发时需要大量类似对象的情况下非常有用。原型模式的核心是一个具有克隆方法的接…

原型模式(Prototype Pattern)

原型模式是一种创建型设计模式,它允许你通过复制现有对象来创建新的对象,而不是通过类实例化来创建对象。这种模式在开发时需要大量类似对象的情况下非常有用。原型模式的核心是一个具有克隆方法的接口,通过实现该接口,不同的对象可以被复制。

实际应用

在C++中,原型模式通常使用一个基类(原型接口)和具体实现类来实现。基类包含一个纯虚拟的克隆方法,每个具体类实现该方法以返回自己的克隆。

克隆形状对象

假设我们有一个`Shape`基类和一些具体的形状类,如`Circle`和`Rectangle`。

#include <iostream>
#include <memory>
#include <unordered_map>// Shape基类,包含一个纯虚拟的克隆方法
class Shape {
public:virtual ~Shape() = default;virtual std::unique_ptr<Shape> clone() const = 0;virtual void draw() const = 0;
};// Circle类,继承自Shape并实现克隆方法
class Circle : public Shape {
private:int radius;
public:Circle(int r) : radius(r) {}std::unique_ptr<Shape> clone() const override {return std::make_unique<Circle>(*this);}void draw() const override {std::cout << "Drawing a Circle with radius " << radius << "\n";}
};// Rectangle类,继承自Shape并实现克隆方法
class Rectangle : public Shape {
private:int width, height;
public:Rectangle(int w, int h) : width(w), height(h) {}std::unique_ptr<Shape> clone() const override {return std::make_unique<Rectangle>(*this);}void draw() const override {std::cout << "Drawing a Rectangle with width " << width << " and height " << height << "\n";}
};int main() {// 创建原型对象std::unordered_map<std::string, std::unique_ptr<Shape>> prototypes;prototypes["circle"] = std::make_unique<Circle>(10);prototypes["rectangle"] = std::make_unique<Rectangle>(20, 30);// 克隆对象std::unique_ptr<Shape> shape1 = prototypes["circle"]->clone();std::unique_ptr<Shape> shape2 = prototypes["rectangle"]->clone();// 绘制克隆的对象shape1->draw();shape2->draw();return 0;
}
克隆计算机对象

假设我们有一个`Computer`基类和一些具体的计算机类,如`Laptop`和`Desktop`。

#include <iostream>
#include <memory>
#include <unordered_map>// Computer基类,包含一个纯虚拟的克隆方法
class Computer {
public:virtual ~Computer() = default;virtual std::unique_ptr<Computer> clone() const = 0;virtual void display() const = 0;
};// Laptop类,继承自Computer并实现克隆方法
class Laptop : public Computer {
private:int batteryLife;
public:Laptop(int battery) : batteryLife(battery) {}std::unique_ptr<Computer> clone() const override {return std::make_unique<Laptop>(*this);}void display() const override {std::cout << "Displaying a Laptop with battery life " << batteryLife << " hours\n";}
};// Desktop类,继承自Computer并实现克隆方法
class Desktop : public Computer {
private:bool hasMonitor;
public:Desktop(bool monitor) : hasMonitor(monitor) {}std::unique_ptr<Computer> clone() const override {return std::make_unique<Desktop>(*this);}void display() const override {std::cout << "Displaying a Desktop " << (hasMonitor ? "with" : "without") << " monitor\n";}
};int main() {// 创建原型对象std::unordered_map<std::string, std::unique_ptr<Computer>> prototypes;prototypes["laptop"] = std::make_unique<Laptop>(8);prototypes["desktop"] = std::make_unique<Desktop>(true);// 克隆对象std::unique_ptr<Computer> comp1 = prototypes["laptop"]->clone();std::unique_ptr<Computer> comp2 = prototypes["desktop"]->clone();// 显示克隆的对象comp1->display();comp2->display();return 0;
}
克隆文档对象

假设我们有一个`Document`基类和一些具体的文档类,如`WordDocument`和`PDFDocument`。

#include <iostream>
#include <memory>
#include <unordered_map>// Document基类,包含一个纯虚拟的克隆方法
class Document {
public:virtual ~Document() = default;virtual std::unique_ptr<Document> clone() const = 0;virtual void print() const = 0;
};// WordDocument类,继承自Document并实现克隆方法
class WordDocument : public Document {
private:int wordCount;
public:WordDocument(int words) : wordCount(words) {}std::unique_ptr<Document> clone() const override {return std::make_unique<WordDocument>(*this);}void print() const override {std::cout << "Printing a Word Document with word count " << wordCount << "\n";}
};// PDFDocument类,继承自Document并实现克隆方法
class PDFDocument : public Document {
private:int pageCount;
public:PDFDocument(int pages) : pageCount(pages) {}std::unique_ptr<Document> clone() const override {return std::make_unique<PDFDocument>(*this);}void print() const override {std::cout << "Printing a PDF Document with page count " << pageCount << "\n";}
};int main() {// 创建原型对象std::unordered_map<std::string, std::unique_ptr<Document>> prototypes;prototypes["word"] = std::make_unique<WordDocument>(5000);prototypes["pdf"] = std::make_unique<PDFDocument>(100);// 克隆对象std::unique_ptr<Document> doc1 = prototypes["word"]->clone();std::unique_ptr<Document> doc2 = prototypes["pdf"]->clone();// 打印克隆的对象doc1->print();doc2->print();return 0;
}

总结

每个原型类实现自己的克隆方法,从而确保了对象的正确复制。

http://www.yayakq.cn/news/605588/

相关文章:

  • 农产品的网站建设方案书范文黑龙江新闻法治频道节目回放
  • dw建设网站教案wordpress可以自己做主题
  • 做网站推广公司网站页面布局优化
  • 怎么利用云盘建设网站泉州制作网站软件
  • 商务网站建设怎样收费电子商务学网站建设好吗
  • 视频建设网站首页sem竞价外包公司
  • 一个网站的建设流程wordpress访问权限插件
  • 广东专注网站建设怎么样蚌埠seo外包
  • 怎么下载随申办app公司网站关键词优化怎么做
  • 神农架网站建设工作态度和责任心句子
  • 优惠券网站要怎么做企业网站建设 百度文库
  • 办网站需流程怎么做网页设计的页面
  • 做京东网站的摘要wordpress动态文章页模板下载
  • 温州建设网站公司哪家好深圳关键词seo
  • python做电商网站做网站竞价是什么意思
  • 天津工程建设信息网站万维网络(临沂网站建设)
  • 网站开发工程师招聘信息网络推广渠道排名
  • 网站自然排名怎么优化网页设计企业网站设计的功能
  • 网站建设经费管理安阳县人口
  • 手机上怎么查看网站设计腾讯云服务器搭建网站
  • 方山建站报价福建建筑人才网档案关联
  • 电商网站开发资金预算咨询公司网站设计
  • 广告页面模板网站公司推广网站怎么做
  • 企业官网建设 创意网站建设大连开发区盛京医院电话
  • 哈尔滨网站营销推广东莞网络推广服务商
  • 网站建设文献西丽网站建设
  • 有哪些做的很漂亮的网站wordpress允许爬取
  • 外国购物网站有哪些平台盘锦做网站公司
  • 门户网站建设审批程序老铁seo外链工具
  • 网站制作公司优势广东企业网络推广教程