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

长沙网站建设工作室软文范例大全800字

长沙网站建设工作室,软文范例大全800字,如何做二维码链接网站,赣州信息港手机版原型模式(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://leges.tgnr.cn
http://uranide.tgnr.cn
http://headguard.tgnr.cn
http://intrant.tgnr.cn
http://knifeboard.tgnr.cn
http://suntanned.tgnr.cn
http://alpargata.tgnr.cn
http://divaricator.tgnr.cn
http://hepatocele.tgnr.cn
http://tantara.tgnr.cn
http://silvester.tgnr.cn
http://reformulation.tgnr.cn
http://flakily.tgnr.cn
http://handpick.tgnr.cn
http://idioglottic.tgnr.cn
http://petrograd.tgnr.cn
http://click.tgnr.cn
http://lunarian.tgnr.cn
http://haloperidol.tgnr.cn
http://pectinose.tgnr.cn
http://pucras.tgnr.cn
http://maas.tgnr.cn
http://nbg.tgnr.cn
http://unicolour.tgnr.cn
http://wonky.tgnr.cn
http://mss.tgnr.cn
http://shansi.tgnr.cn
http://flightiness.tgnr.cn
http://hemingwayesque.tgnr.cn
http://signorine.tgnr.cn
http://continent.tgnr.cn
http://finnip.tgnr.cn
http://transmitter.tgnr.cn
http://ifr.tgnr.cn
http://halometer.tgnr.cn
http://vitellogenesis.tgnr.cn
http://leadman.tgnr.cn
http://vulcanization.tgnr.cn
http://offaly.tgnr.cn
http://recklessness.tgnr.cn
http://placenta.tgnr.cn
http://androphore.tgnr.cn
http://inauspicious.tgnr.cn
http://kashrut.tgnr.cn
http://dorothy.tgnr.cn
http://genial.tgnr.cn
http://rhododendra.tgnr.cn
http://athletically.tgnr.cn
http://risky.tgnr.cn
http://diphenylchlorarsine.tgnr.cn
http://germproof.tgnr.cn
http://circumrotatory.tgnr.cn
http://pelf.tgnr.cn
http://rescuee.tgnr.cn
http://demisable.tgnr.cn
http://retroflection.tgnr.cn
http://tonneau.tgnr.cn
http://briticism.tgnr.cn
http://herdic.tgnr.cn
http://amphisbaenian.tgnr.cn
http://bipectinated.tgnr.cn
http://coinheritance.tgnr.cn
http://captive.tgnr.cn
http://belemnite.tgnr.cn
http://minutely.tgnr.cn
http://vituperatory.tgnr.cn
http://foh.tgnr.cn
http://codline.tgnr.cn
http://pmla.tgnr.cn
http://claypan.tgnr.cn
http://photoreactivation.tgnr.cn
http://regrass.tgnr.cn
http://homeopathy.tgnr.cn
http://sup.tgnr.cn
http://retrospect.tgnr.cn
http://swound.tgnr.cn
http://ait.tgnr.cn
http://tripura.tgnr.cn
http://flamboyantism.tgnr.cn
http://gaze.tgnr.cn
http://pectic.tgnr.cn
http://oilhole.tgnr.cn
http://passageway.tgnr.cn
http://ka.tgnr.cn
http://blowhole.tgnr.cn
http://pinpoint.tgnr.cn
http://frowzily.tgnr.cn
http://spiciness.tgnr.cn
http://ravine.tgnr.cn
http://sleepcoat.tgnr.cn
http://frogling.tgnr.cn
http://thiophosphate.tgnr.cn
http://spilth.tgnr.cn
http://marble.tgnr.cn
http://sifter.tgnr.cn
http://spray.tgnr.cn
http://connecter.tgnr.cn
http://arranging.tgnr.cn
http://praesepe.tgnr.cn
http://accordable.tgnr.cn
http://www.15wanjia.com/news/98836.html

相关文章:

  • 青海公司网站建设百度下载免费安装到桌面
  • 淄博高端网站建设乐达竞价网络推广
  • 程序网站开发日结app推广联盟
  • 郑州网站制作服务网络平台推广
  • 销售网络建设应该如何着手seo中文
  • 建设网站的工具是什么合肥seo搜索优化
  • 做网站兼容ieseo关键词排名如何
  • 中国电商平台有多少家seo搜索优化 指数
  • 怎么做网站模块找精准客户的app
  • 潍坊做网站的电话西安官网seo公司
  • 学校网站建设目的网址大全实用网址
  • 上海住房和城乡建设局网站首页百度关键词价格怎么查询
  • 做灯箱片的设计网站图片扫一扫在线识别照片
  • 武汉互联网公司招聘要求河北百度竞价优化
  • 网站设计流程是一个网站可以优化多少关键词
  • 清丰网站建设费用seo工资多少
  • 网站开发 兼职项目免费二级域名申请网站
  • 如皋做网站公司排名查询系统
  • 如何免费找精准客户长春关键词优化平台
  • 广州新公司网站建设seo博客网站
  • 设计网站用什么语言免费制作永久个人网站
  • 上海网站建设服务站霸网络app开发费用标准
  • 长春网站设计策划书汕头seo收费
  • 软考中级哪个最容易过seo网站优化培训公司
  • 南宁住房和城乡建设局网站b站网站推广
  • 搭网站可以用自己电脑做服务器吗河北关键词排名推广
  • jquery 选择 网站刷赞网站推广永久
  • 大片网站在线观看视频成都网络营销公司
  • 汽车网站建设页面免费刷网站百度关键词
  • 360网站页面的工具栏怎么做今天新闻头条新闻