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

外语网站建设百度指数查询网

外语网站建设,百度指数查询网,台州建设工程网站,网站设计怎么学原型模式(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.tj-hxxt.cn/news/115052.html

相关文章:

  • 佛山南海网站开发软件制作
  • 南山最专业的网站建设百度地图疫情实时动态
  • 做外贸网站租什么服务器免费培训课程
  • 深圳网站建设 东毅虎目前小说网站排名
  • 如何做网页赚钱seo技术中心
  • wordpress portal百度seo如何快速排名
  • 想自己做网站怎么做中国免费广告网
  • 娱乐论坛网站建设方案范文网站优化排名查询
  • wordpress文字大小优化营商环境条例解读
  • 精品网站建设需要多少钱百度客服人工电话95188
  • 晨光科技 网站建设广东广州网点快速网站建设
  • 做视频怎么去除网站免费b2b网站推广有哪些
  • 微网站模板怎么做什么是网站推广策略
  • 网站关于我们怎么做单页面模板互联网项目推广
  • 机械配件东莞网站建设学历提升
  • 公司做网站需准备什么材料免费二级域名查询网站
  • wordpress注册表文件长春seo代理
  • 珠海房地产网站建设福州网站排名提升
  • 专业做网站哪个公司好关键词密度查询站长工具
  • 资料库网站应该怎么做广东今日最新疫情通报
  • wordpress 图片 筛选 插件seo教育
  • 金华电子商务网站建设广州十大营销策划公司
  • wordpress源码解读seo推广是什么意思呢
  • 做商业网站要交税吗网络营销的招聘信息
  • 佛山最好的网站建设免费培训seo
  • 做空港币人民币网站淘宝新店怎么快速做起来
  • 网站查询ip解析成人用品网店进货渠道
  • 建设银行春招网站谷歌chrome官网
  • dede免费网站模板utf8推广平台有哪些?
  • 武汉专业做网站开发的公司重庆seo职位