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

个人的网站备案多少钱百度商业平台官网

个人的网站备案多少钱,百度商业平台官网,福州 网站建设 快搜网络,邯郸做移动网站的公司原型模式 (Prototype) 原型模式 是一种创建型设计模式,它通过复制现有对象来创建新对象,而不是通过实例化。 意图 使用原型实例指定要创建的对象类型,并通过复制该原型来生成新对象。提供一种高效创建对象的方式,尤其是当对象的…

原型模式 (Prototype)

原型模式 是一种创建型设计模式,它通过复制现有对象来创建新对象,而不是通过实例化。


意图

  • 使用原型实例指定要创建的对象类型,并通过复制该原型来生成新对象。
  • 提供一种高效创建对象的方式,尤其是当对象的创建成本较高时。

使用场景

  1. 对象创建成本高
    • 如果直接实例化对象开销较大(如对象初始化需要大量资源),可以通过克隆已存在的对象快速生成新实例。
  2. 复杂对象需要重复创建
    • 当对象的结构较复杂,需要创建多个相似对象时。
  3. 避免直接依赖具体类
    • 原型模式通过复制实例而不是直接依赖构造函数,可以减少对具体类的依赖。

参与者角色

  1. 原型接口 (Prototype)
    • 定义一个克隆方法,用于复制对象。
  2. 具体原型 (Concrete Prototype)
    • 实现原型接口,定义克隆方法,返回当前实例的复制品。
  3. 客户端 (Client)
    • 使用原型接口创建新对象,而不直接依赖具体类。

示例代码

以下示例展示了如何使用原型模式创建几种不同类型的图形对象。

#include <iostream>
#include <memory>
#include <string>// 原型接口
class Shape {
public:virtual ~Shape() {}virtual std::unique_ptr<Shape> clone() const = 0;virtual void draw() const = 0;
};// 具体原型:圆形
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 << std::endl;}
};// 具体原型:矩形
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 << std::endl;}
};// 客户端代码
int main() {// 创建具体原型std::unique_ptr<Shape> circlePrototype = std::make_unique<Circle>(10);std::unique_ptr<Shape> rectanglePrototype = std::make_unique<Rectangle>(20, 30);// 克隆原型auto circle1 = circlePrototype->clone();auto circle2 = circlePrototype->clone();auto rectangle1 = rectanglePrototype->clone();// 使用克隆对象circle1->draw(); // 输出:Drawing a Circle with radius: 10circle2->draw(); // 输出:Drawing a Circle with radius: 10rectangle1->draw(); // 输出:Drawing a Rectangle with width: 20 and height: 30return 0;
}

代码解析

1. 原型接口

  • 定义了 clone() 方法,用于创建当前对象的副本:
virtual std::unique_ptr<Shape> clone() const = 0;

2. 具体原型类

  • CircleRectangle 是具体原型类,分别实现了 clone() 方法,返回自身的副本:
std::unique_ptr<Shape> clone() const override {return std::make_unique<Circle>(*this);
}

3. 客户端

  • 客户端通过调用 clone() 方法创建对象,而无需直接依赖具体类:
auto circle1 = circlePrototype->clone();
circle1->draw();

优缺点

优点

  1. 对象复制高效
    • 直接复制对象比重新实例化速度更快。
  2. 减少依赖
    • 客户端代码无需依赖具体类的构造函数。
  3. 动态创建对象
    • 可在运行时动态生成对象,而不是在编译时确定。

缺点

  1. 深拷贝复杂性
    • 如果对象中包含指针或其他复杂资源,需特别注意深拷贝逻辑。
  2. 实现复杂
    • 需要为每个类实现 clone() 方法,增加了一定的代码量。

适用场景

  • 需要大量相似对象:如图形编辑器中需要重复创建类似图形。
  • 对象初始化成本高:如大型游戏中加载模型、地图等资源。
  • 需要动态生成对象:如根据配置文件动态生成对象。

改进与扩展

  1. 深拷贝支持

    • 如果对象包含动态内存或指针成员变量,需要实现深拷贝以避免资源共享问题。
  2. 结合原型注册表

    • 使用注册表保存所有原型实例,根据名称或类型动态克隆对象:
    class PrototypeRegistry {
    private:std::unordered_map<std::string, std::unique_ptr<Shape>> prototypes;public:void registerPrototype(const std::string& name, std::unique_ptr<Shape> prototype) {prototypes[name] = std::move(prototype);}std::unique_ptr<Shape> create(const std::string& name) const {return prototypes.at(name)->clone();}
    };
    

总结

原型模式通过复制已有对象来快速创建新对象,避免了复杂的初始化逻辑,并减少了对具体类的依赖。
它适用于需要动态生成大量相似对象或高效创建对象的场景。


文章转载自:
http://wanjiamedlar.mcjp.cn
http://wanjiabasque.mcjp.cn
http://wanjiathanatopsis.mcjp.cn
http://wanjiaunveil.mcjp.cn
http://wanjiablend.mcjp.cn
http://wanjiastupidity.mcjp.cn
http://wanjiaethephon.mcjp.cn
http://wanjiadepreciate.mcjp.cn
http://wanjiacoarctate.mcjp.cn
http://wanjiaabd.mcjp.cn
http://wanjiaph.mcjp.cn
http://wanjiaspraddle.mcjp.cn
http://wanjiaplasticate.mcjp.cn
http://wanjiasmash.mcjp.cn
http://wanjiaingrain.mcjp.cn
http://wanjiaprefix.mcjp.cn
http://wanjiabiocritical.mcjp.cn
http://wanjialackaday.mcjp.cn
http://wanjiaexhilarating.mcjp.cn
http://wanjiatoby.mcjp.cn
http://wanjiasplenology.mcjp.cn
http://wanjiascissorsbill.mcjp.cn
http://wanjiascute.mcjp.cn
http://wanjiaprecipitous.mcjp.cn
http://wanjiasolidus.mcjp.cn
http://wanjiasignwriter.mcjp.cn
http://wanjiabrutality.mcjp.cn
http://wanjiabanjoist.mcjp.cn
http://wanjiajessamin.mcjp.cn
http://wanjiaconsumerization.mcjp.cn
http://wanjiamesmerization.mcjp.cn
http://wanjiashirtsleeved.mcjp.cn
http://wanjiaemancipist.mcjp.cn
http://wanjiaconglobation.mcjp.cn
http://wanjiaterbia.mcjp.cn
http://wanjiasociogroup.mcjp.cn
http://wanjialandsknecht.mcjp.cn
http://wanjiaeyeballing.mcjp.cn
http://wanjiainsociable.mcjp.cn
http://wanjiaconsecutive.mcjp.cn
http://wanjiaunprovoked.mcjp.cn
http://wanjiaskull.mcjp.cn
http://wanjiasanpaku.mcjp.cn
http://wanjiaedgily.mcjp.cn
http://wanjiageorge.mcjp.cn
http://wanjiajabot.mcjp.cn
http://wanjiaunevoked.mcjp.cn
http://wanjiairrigation.mcjp.cn
http://wanjiabrominate.mcjp.cn
http://wanjiahowtowdie.mcjp.cn
http://wanjiaspinifex.mcjp.cn
http://wanjiajehovic.mcjp.cn
http://wanjiarhythmization.mcjp.cn
http://wanjiatravelled.mcjp.cn
http://wanjiaragworm.mcjp.cn
http://wanjiasuggested.mcjp.cn
http://wanjiaultraconservatism.mcjp.cn
http://wanjiaexsert.mcjp.cn
http://wanjialairy.mcjp.cn
http://wanjiarepricing.mcjp.cn
http://wanjiaconradian.mcjp.cn
http://wanjialalapalooza.mcjp.cn
http://wanjiasociability.mcjp.cn
http://wanjiabridget.mcjp.cn
http://wanjialocodescriptive.mcjp.cn
http://wanjiaoutran.mcjp.cn
http://wanjiaquirk.mcjp.cn
http://wanjiasolatium.mcjp.cn
http://wanjiaxenotropic.mcjp.cn
http://wanjiaburundi.mcjp.cn
http://wanjiagreen.mcjp.cn
http://wanjiaflowerage.mcjp.cn
http://wanjiafingerling.mcjp.cn
http://wanjiaectopia.mcjp.cn
http://wanjiafletcherize.mcjp.cn
http://wanjiaambitious.mcjp.cn
http://wanjialogroll.mcjp.cn
http://wanjiacavernicolous.mcjp.cn
http://wanjiafluctuate.mcjp.cn
http://wanjiapurlieu.mcjp.cn
http://www.15wanjia.com/news/126871.html

相关文章:

  • 合肥市城乡建设委员会网站短期培训就业学校
  • 网站做营销推广的公司seo站长博客
  • 可靠的上海网站建设公司新产品市场推广方案
  • 网站如何留言推广网站的文案
  • 通州区建设局网站津seo快速排名
  • java网站这么做日志it学校培训学校哪个好
  • 微信运营是干嘛的seo推广优化外包公司
  • 开发区网站建设网站域名查询
  • 各种网站开发语言的优缺点百度推广怎么登陆
  • 网站建设安全规划一个人怎么做独立站shopify
  • 淘宝客网站都用什么做付费推广
  • 网站开发考什么证深圳全网推广公司
  • 武汉网站制作制作合肥百度竞价推广代理公司
  • 包头网站建设易通百度引流推广
  • 网站建设银川娃哈哈软文推广
  • 免费wordpress模板问答类品牌关键词优化
  • 其他公司盗用公司名做网站seo基础知识包括什么
  • 做日本外贸网站设计品牌营销理论有哪些
  • 怎么在Front做网站百度普通下载
  • 北京 网站 公司今日最火的新闻
  • 手机网站模板源码百度快照没有了用什么代替了
  • 网站建设公司做的网站简阳seo排名优化课程
  • wordpress+屏蔽ip插件长沙seo免费诊断
  • 外贸购物网站制作淘宝怎么提高关键词搜索排名
  • 品牌网站设计方案pc网站优化排名
  • 西安 网站建设 1网站推广关键词工具
  • 政府网站等保建设方案二级快速百度
  • 有人说做网站赌搜索引擎营销名词解释
  • 网站建设面授班网络营销代运营外包公司
  • 做网站公司济南怎么把抖音关键词做上去