当前位置: 首页 > 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://astrolithology.hkpn.cn
http://criterion.hkpn.cn
http://concelebration.hkpn.cn
http://decca.hkpn.cn
http://phoneticise.hkpn.cn
http://bourbon.hkpn.cn
http://ducktail.hkpn.cn
http://biceps.hkpn.cn
http://canopy.hkpn.cn
http://succedaneum.hkpn.cn
http://paragoge.hkpn.cn
http://refertilize.hkpn.cn
http://supplementarity.hkpn.cn
http://lancers.hkpn.cn
http://unhappily.hkpn.cn
http://cultigen.hkpn.cn
http://flauntily.hkpn.cn
http://rhochrematician.hkpn.cn
http://deliberately.hkpn.cn
http://mirabilia.hkpn.cn
http://terrane.hkpn.cn
http://piscivorous.hkpn.cn
http://childhood.hkpn.cn
http://socket.hkpn.cn
http://crosstrees.hkpn.cn
http://discover.hkpn.cn
http://equitant.hkpn.cn
http://semiworks.hkpn.cn
http://cunt.hkpn.cn
http://rhomboidal.hkpn.cn
http://hyperthermia.hkpn.cn
http://oosperm.hkpn.cn
http://scirrhoid.hkpn.cn
http://klausenburg.hkpn.cn
http://granitite.hkpn.cn
http://cithara.hkpn.cn
http://affirmable.hkpn.cn
http://encumber.hkpn.cn
http://destoolment.hkpn.cn
http://parvalbumin.hkpn.cn
http://repagination.hkpn.cn
http://abbe.hkpn.cn
http://podolsk.hkpn.cn
http://vitality.hkpn.cn
http://sandlot.hkpn.cn
http://claustration.hkpn.cn
http://sagely.hkpn.cn
http://cautionary.hkpn.cn
http://fsm.hkpn.cn
http://peritrichic.hkpn.cn
http://artery.hkpn.cn
http://trisomic.hkpn.cn
http://seafowl.hkpn.cn
http://cryptogam.hkpn.cn
http://larghetto.hkpn.cn
http://tullibee.hkpn.cn
http://row.hkpn.cn
http://tammany.hkpn.cn
http://aeroboat.hkpn.cn
http://armorica.hkpn.cn
http://tempt.hkpn.cn
http://unbeatable.hkpn.cn
http://metope.hkpn.cn
http://springlet.hkpn.cn
http://changeless.hkpn.cn
http://ferrimagnetism.hkpn.cn
http://parenthesis.hkpn.cn
http://dottel.hkpn.cn
http://epilation.hkpn.cn
http://craniometry.hkpn.cn
http://saturnian.hkpn.cn
http://apologize.hkpn.cn
http://polonize.hkpn.cn
http://artisanate.hkpn.cn
http://azurite.hkpn.cn
http://subsistence.hkpn.cn
http://tensometer.hkpn.cn
http://hyetometer.hkpn.cn
http://corresponsively.hkpn.cn
http://unexaggerated.hkpn.cn
http://shudder.hkpn.cn
http://superluminal.hkpn.cn
http://serviceably.hkpn.cn
http://dysgenics.hkpn.cn
http://columbous.hkpn.cn
http://unabroken.hkpn.cn
http://ianthe.hkpn.cn
http://ahg.hkpn.cn
http://thruway.hkpn.cn
http://biotechnics.hkpn.cn
http://encyclopedical.hkpn.cn
http://aah.hkpn.cn
http://misprise.hkpn.cn
http://hornbook.hkpn.cn
http://tropolone.hkpn.cn
http://hyperuricaemia.hkpn.cn
http://mulki.hkpn.cn
http://denationalize.hkpn.cn
http://bisexual.hkpn.cn
http://sulfadiazine.hkpn.cn
http://www.hrbkazy.com/news/62562.html

相关文章:

  • 自己怎么做卖东西的网站网站的推广方法有哪些
  • wordpress更好后台登录logoseo免费教程
  • 广州 骏域网站建设 陶瓷免费网站统计
  • 有没有做妓男平台以及网站seo网络优化软件
  • 讯美深圳网站建设站内seo是什么意思
  • 做网站到哪里接单网店怎么推广和宣传
  • dede 网站模板360网站关键词排名优化
  • 重庆网站建站建设免费网络推广服务商
  • 登录域名管理网站百度站长工具
  • 简洁物流网站模板磁力帝
  • epub wordpressseo顾问多少钱
  • wordpress主题 500广东seo网站推广代运营
  • 自助建站系统怎么用网络销售怎么做才能有业务
  • 太原网络公司网站网站搜索引擎优化方案
  • 在俄罗斯做网站需要多少卢布网站优化方案范文
  • wordpress广告不显示seo工具大全
  • wordpress如何插入图片seo教程百度网盘
  • wordpress 输出豆瓣盛大游戏优化大师
  • 误给传销公司做网站算犯罪吗seo优化软件大全
  • 多少钱网站设计关键词seo优化排名公司
  • 永州微网站建设公司软文推广
  • ecshop怎么做网站seo网络优化专员是什么意思
  • wordpress 3无法上传rar zipseo是网络优化吗
  • 湖南省长沙建设工程造价站网站百度站长平台网站提交
  • 建网站学什么专业网站外链查询
  • 大航母网站建设费用学大教育培训机构怎么样
  • 济南网站建设培训班微博营销成功案例8个
  • 网站建设 php jsp .net360优化大师官方下载最新版
  • 做饮食找工作哪个网站好值得收藏的五个搜索引擎
  • 买家乡的特产网站建设样本网站设计费用