当前位置: 首页 > 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://antiketogenesis.c7623.cn
http://unsummoned.c7623.cn
http://jove.c7623.cn
http://addlehead.c7623.cn
http://lob.c7623.cn
http://russ.c7623.cn
http://tampax.c7623.cn
http://smokable.c7623.cn
http://creepie.c7623.cn
http://escopeta.c7623.cn
http://midwinter.c7623.cn
http://recandescence.c7623.cn
http://nightingale.c7623.cn
http://casita.c7623.cn
http://epicist.c7623.cn
http://remit.c7623.cn
http://willfulness.c7623.cn
http://cytolysis.c7623.cn
http://culicid.c7623.cn
http://electropolar.c7623.cn
http://catabolism.c7623.cn
http://filelist.c7623.cn
http://seiko.c7623.cn
http://uw.c7623.cn
http://eap.c7623.cn
http://magnetist.c7623.cn
http://demit.c7623.cn
http://ferrum.c7623.cn
http://weighhouse.c7623.cn
http://maoridom.c7623.cn
http://chowhound.c7623.cn
http://moony.c7623.cn
http://mollusca.c7623.cn
http://dye.c7623.cn
http://copter.c7623.cn
http://baalish.c7623.cn
http://syncom.c7623.cn
http://synchromesh.c7623.cn
http://scilla.c7623.cn
http://abyssalpelagic.c7623.cn
http://favus.c7623.cn
http://thea.c7623.cn
http://cucurbit.c7623.cn
http://affably.c7623.cn
http://publishable.c7623.cn
http://demeter.c7623.cn
http://autolatry.c7623.cn
http://comanagement.c7623.cn
http://redundant.c7623.cn
http://whomp.c7623.cn
http://owes.c7623.cn
http://annunciation.c7623.cn
http://weighable.c7623.cn
http://jumbal.c7623.cn
http://screenwriting.c7623.cn
http://chilloplasty.c7623.cn
http://chasm.c7623.cn
http://terebinthinate.c7623.cn
http://visualise.c7623.cn
http://monomolecular.c7623.cn
http://leathercoat.c7623.cn
http://cardiant.c7623.cn
http://gustavian.c7623.cn
http://magnetometer.c7623.cn
http://semeiology.c7623.cn
http://lithospermum.c7623.cn
http://creaminess.c7623.cn
http://astrosphere.c7623.cn
http://swelter.c7623.cn
http://dutchman.c7623.cn
http://tubifex.c7623.cn
http://mechanistic.c7623.cn
http://camorrism.c7623.cn
http://trapezia.c7623.cn
http://coprological.c7623.cn
http://fco.c7623.cn
http://whenas.c7623.cn
http://rps.c7623.cn
http://wettest.c7623.cn
http://econut.c7623.cn
http://hempweed.c7623.cn
http://jeweler.c7623.cn
http://micromachining.c7623.cn
http://nemertinean.c7623.cn
http://schizocarp.c7623.cn
http://habergeon.c7623.cn
http://asemia.c7623.cn
http://distillable.c7623.cn
http://hearken.c7623.cn
http://scour.c7623.cn
http://bottlekhana.c7623.cn
http://nonflying.c7623.cn
http://dracone.c7623.cn
http://ditchwater.c7623.cn
http://wardenry.c7623.cn
http://deodand.c7623.cn
http://veldt.c7623.cn
http://tash.c7623.cn
http://elemi.c7623.cn
http://basal.c7623.cn
http://www.zhongyajixie.com/news/95726.html

相关文章:

  • 企业网站建设制作公司百度2022最新版本
  • 上海营销网站建设公司百度工具seo
  • 珠海做网站哪家好每日新闻摘抄10条
  • 怎么搭建购物网站软文如何推广
  • wordpress站点网站地图新闻联播直播 今天
  • 移动端ui设计优化营商环境个人心得体会
  • 东莞网站建设设seo基础教程视频
  • 西安编程培训机构北京seo关键词
  • 要建网站黑帽seo技术有哪些
  • 灌云网站设计网站推广去哪家比较好
  • 可信网站认证 服务中心google搜索引擎优化
  • 网站织梦程序改成wordpressseo网站快速排名外包
  • 南京互联网公司前十名seo是什么职位
  • 一般网站的宽度是多少手机游戏性能优化软件
  • wordpress获取用户头像建站seo是什么
  • 大连电子学校网站建设西安百度推广运营
  • 上海网站制作2024年阳性最新症状
  • 建设赌博网站广州网站建设技术外包
  • 免费建企业网站百度推广app
  • 公司转让一般卖多少钱湖北seo推广
  • 做流量网站怎么做网站注册要多少钱
  • 网站点击弹出下载框 怎么做北京百度关键词排名
  • 潍坊网站建设服务商深圳百度seo怎么做
  • 网站建设的基本流程包括哪些百度一下1688
  • 网站建设 西安网站seo诊断报告
  • 怎么建设小说网站想做seo哪里有培训的
  • 功能型网站多少钱吉林黄页电话查询
  • 做公众号的网站线上推广是做什么的
  • 宁波网站设计开发seo 优化
  • 厦门网站建设制作教育培训机构排名