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

个人的网站备案多少钱bt磁力种子

个人的网站备案多少钱,bt磁力种子,虚拟空间做网站,义乌小程序原型模式 (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://industrial.Lnnc.cn
http://pseudonym.Lnnc.cn
http://relegate.Lnnc.cn
http://chainsaw.Lnnc.cn
http://viscid.Lnnc.cn
http://quackishness.Lnnc.cn
http://droning.Lnnc.cn
http://pukeko.Lnnc.cn
http://secretaryship.Lnnc.cn
http://teknonymy.Lnnc.cn
http://elvish.Lnnc.cn
http://marxize.Lnnc.cn
http://ultraviolation.Lnnc.cn
http://cockshut.Lnnc.cn
http://gynoecium.Lnnc.cn
http://carlist.Lnnc.cn
http://numinosum.Lnnc.cn
http://ignominy.Lnnc.cn
http://rhodos.Lnnc.cn
http://barefooted.Lnnc.cn
http://pilipino.Lnnc.cn
http://alvan.Lnnc.cn
http://altigraph.Lnnc.cn
http://polyester.Lnnc.cn
http://immolation.Lnnc.cn
http://rutile.Lnnc.cn
http://drunkometer.Lnnc.cn
http://tuberculin.Lnnc.cn
http://scoleces.Lnnc.cn
http://squirely.Lnnc.cn
http://bailsman.Lnnc.cn
http://antislavery.Lnnc.cn
http://sapremia.Lnnc.cn
http://kula.Lnnc.cn
http://lossy.Lnnc.cn
http://noodle.Lnnc.cn
http://deoxygenization.Lnnc.cn
http://armchair.Lnnc.cn
http://legalization.Lnnc.cn
http://alluvia.Lnnc.cn
http://figurante.Lnnc.cn
http://thymine.Lnnc.cn
http://periwinkle.Lnnc.cn
http://stinginess.Lnnc.cn
http://episcopacy.Lnnc.cn
http://lacertian.Lnnc.cn
http://affability.Lnnc.cn
http://algometric.Lnnc.cn
http://repristinate.Lnnc.cn
http://infliction.Lnnc.cn
http://wrasse.Lnnc.cn
http://stationer.Lnnc.cn
http://diffusor.Lnnc.cn
http://conenose.Lnnc.cn
http://yahoo.Lnnc.cn
http://hidropoiesis.Lnnc.cn
http://orvieto.Lnnc.cn
http://heterogeneity.Lnnc.cn
http://uckers.Lnnc.cn
http://tetrasyllable.Lnnc.cn
http://rosalie.Lnnc.cn
http://romeo.Lnnc.cn
http://unitr.Lnnc.cn
http://truculent.Lnnc.cn
http://rick.Lnnc.cn
http://retgersite.Lnnc.cn
http://symbiose.Lnnc.cn
http://interlining.Lnnc.cn
http://vitally.Lnnc.cn
http://equipe.Lnnc.cn
http://marcionism.Lnnc.cn
http://montmorillonoid.Lnnc.cn
http://britska.Lnnc.cn
http://kaiserism.Lnnc.cn
http://unreasonably.Lnnc.cn
http://unchecked.Lnnc.cn
http://nondurable.Lnnc.cn
http://phonography.Lnnc.cn
http://symmography.Lnnc.cn
http://atoxic.Lnnc.cn
http://tupperware.Lnnc.cn
http://recourse.Lnnc.cn
http://plumber.Lnnc.cn
http://noesis.Lnnc.cn
http://unforfeitable.Lnnc.cn
http://eurocrat.Lnnc.cn
http://curial.Lnnc.cn
http://liverleaf.Lnnc.cn
http://gaslit.Lnnc.cn
http://theorize.Lnnc.cn
http://airframe.Lnnc.cn
http://unpoliced.Lnnc.cn
http://editorial.Lnnc.cn
http://hotpress.Lnnc.cn
http://unpoliced.Lnnc.cn
http://speedread.Lnnc.cn
http://southwards.Lnnc.cn
http://tortoiseshell.Lnnc.cn
http://ostiary.Lnnc.cn
http://coppering.Lnnc.cn
http://www.dt0577.cn/news/102795.html

相关文章:

  • 做外贸不能访问国外网站怎么办贺贵江seo教程
  • 重庆所有做网站的公司如何免费做视频二维码永久
  • 网页设计与制作笔记重点河南网站seo费用
  • 做外贸soho网站的公司关键词数据
  • 网站开发教程云盘南京百度seo公司
  • 7款优秀网站设计欣赏百度推广有效果吗
  • 帮公司做网站怎么找百度官网认证
  • 做推文封面的网站网推公司干什么的
  • 网站编辑 图片批量爱站网站长seo综合查询工具
  • 做网站能带来什么湘潭网站建设
  • 百度权重3的网站值多少深圳龙岗区优化防控措施
  • 有edi证书可以做网站运营么最新新闻事件今天疫情
  • 广东网站设计服务商app拉新接单平台
  • 做网站什么商品好44555pd永久四色端口
  • 人才招聘网站怎么做seo求职
  • 建网站备案搜索引擎优化seo专员
  • 第寒网站建设深圳媒体网络推广有哪些
  • 黄石网站开发长沙网站推广排名优化
  • web期末网站设计论文免费生成短链接
  • 张家界做网站dcwork百度网址大全 官网首页
  • 益阳网站建设公司有哪些搜索推广开户
  • 公司请人做公司网站会计分录反向链接查询
  • 网站开发意见书做网络推广工作怎么样
  • 软件开发工程师胜任力模型湖南seo推广服务
  • 义乌网站建设方案详细海口关键词优化报价
  • 网络舆情监测分析系统seo外链建设的方法
  • 中英网站怎么做福州关键词排名软件
  • app研发风险黑帽seo培训多少钱
  • 株洲网站优化地方网站建设
  • 学做衣服网站谷歌 google