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

找项目网站外链官网

找项目网站,外链官网,比较流行的网站建设技术有哪些,福建省做鞋批发网站原型模式(Prototype Pattern)和建造者模式(Builder Pattern)虽然都是创建型设计模式,但它们的应用场景和实现方式有着显著的区别。以下是二者的详细对比: 1. 意图和应用场景 原型模式: 意图&a…

        原型模式(Prototype Pattern)和建造者模式(Builder Pattern)虽然都是创建型设计模式,但它们的应用场景和实现方式有着显著的区别。以下是二者的详细对比:

1. 意图和应用场景

  • 原型模式:
    • 意图:通过克隆一个现有的对象来创建新对象,而不是通过实例化类来创建新对象。
    • 应用场景:
      • 当创建对象的过程非常复杂或昂贵时,可以通过复制(克隆)现有的实例来创建新对象。
      • 如果系统中有许多具有相似状态的对象,原型模式可以通过复制现有对象节省初始化时间。
      • 适合动态加载类或对象时,用于避免依赖具体的构造函数或复杂的初始化流程。
  • 建造者模式:
    • 意图:将复杂对象的构建过程分离出来,使得相同的构建过程可以创建不同的对象。
    • 应用场景:
      • 当对象的创建过程复杂且涉及多个步骤时,通过建造者模式可以控制创建过程。
      • 特别适合构建复杂的对象(通常包含多个子对象或多个可选参数)。
      • 适用于需要通过不同配置来创建不同种类对象的场景(例如创建同一个产品的不同版本)。

2. 创建对象的方式

  • 原型模式:
    • 通过复制(克隆)对象来创建新对象。复制操作可以是浅拷贝或深拷贝。
    • 主要关注对象的复制过程,而不是构建过程。
    • 通常需要实现 clone() 方法或类似的克隆机制来复制对象。
  • 建造者模式:
    • 通过构造步骤逐步构建对象。这个过程将对象的创建过程与表示过程解耦,允许一步一步地配置对象。
    • 主要关注构建过程中的不同步骤和顺序,并且允许通过同一个构造过程生成不同的对象。
    • 通常使用一个 Builder 类来定义构建的步骤和最终产品的生成。

3. 结构上的区别

  • 原型模式:
    • 原型模式的核心是对象本身,它使用现有的对象作为原型,通过克隆的方式生成新对象。
    • 类结构较为简单,通常只涉及一个需要克隆的对象类。
  • 建造者模式:
    • 建造者模式有清晰的角色划分,通常包括:
      • Builder:定义了构建产品的抽象步骤。
      • ConcreteBuilder:实现具体的构建步骤。
      • Director(可选):负责控制对象构建的顺序。
      • Product:最终生成的对象。
    • 结构较复杂,因为构建过程需要清晰的步骤和相应的实现。

4. 修改对象状态的方式

  • 原型模式:
    • 对象状态是通过克隆现有对象,然后可能在新对象上进行少量修改来实现的。
    • 这种模式下,通常不涉及构建复杂对象的多个步骤,而是从已有的对象开始,进行轻微的调整。
  • 建造者模式:
    • 对象状态的创建是通过一系列构建步骤来完成的。这些步骤可以独立控制,允许在构造过程中自由地改变对象的状态。
    • 对象的不同部分可以通过不同的步骤来进行定制,构建过程可以灵活调整。

5. 代码示例

原型模式示例(C++):

#include <iostream>
#include <string>
#include <memory>class Prototype {
public:virtual ~Prototype() {}virtual std::shared_ptr<Prototype> clone() const = 0;virtual void print() const = 0;
};class ConcretePrototype : public Prototype {
private:std::string name;public:ConcretePrototype(const std::string& name) : name(name) {}std::shared_ptr<Prototype> clone() const override {return std::make_shared<ConcretePrototype>(*this);  // 浅拷贝}void print() const override {std::cout << "Prototype: " << name << std::endl;}
};int main() {std::shared_ptr<Prototype> prototype = std::make_shared<ConcretePrototype>("Original");std::shared_ptr<Prototype> clone = prototype->clone();  // 克隆对象prototype->print();clone->print();  // 输出与原型类似的对象return 0;
}

建造者模式示例(C++):

#include <iostream>
#include <string>// 产品类
class Product {
private:std::string partA;std::string partB;public:void setPartA(const std::string& part) { partA = part; }void setPartB(const std::string& part) { partB = part; }void show() const {std::cout << "Product with " << partA << " and " << partB << std::endl;}
};// Builder 接口
class Builder {
public:virtual ~Builder() {}virtual void buildPartA() = 0;virtual void buildPartB() = 0;virtual Product getResult() = 0;
};// 具体的 Builder 实现
class ConcreteBuilder : public Builder {
private:Product product;public:void buildPartA() override {product.setPartA("Part A");}void buildPartB() override {product.setPartB("Part B");}Product getResult() override {return product;}
};// Director 类,负责构建过程的控制
class Director {
private:Builder& builder;public:Director(Builder& builder) : builder(builder) {}void construct() {builder.buildPartA();builder.buildPartB();}
};int main() {ConcreteBuilder builder;Director director(builder);director.construct();Product product = builder.getResult();product.show();  // 输出构建后的产品return 0;
}

6. 总结对比

模式

原型模式

建造者模式

意图

通过复制现有对象来创建新对象。

通过分步骤构建复杂对象。

创建方式

克隆(浅拷贝或深拷贝)现有对象。

按步骤逐步构建对象。

结构

结构较简单,通常只有一个类实现克隆。

结构较复杂,通常涉及多个类和多个步骤。

状态修改

通过克隆对象后修改少量状态。

通过多个步骤灵活调整对象的各个部分。

应用场景

当对象创建昂贵且需要复制现有对象时。

当对象构建过程复杂,需要逐步构建或有多种构建方式时。

解释:

  • 原型模式:
    • Prototype 是一个抽象类,定义了 clone() 方法。
    • ConcretePrototype 是具体的实现类,负责实现 clone() 方法并执行对象的复制操作。

  • 建造者模式:
    • Builder 是一个接口,定义了构建产品的方法。
    • ConcreteBuilder 实现了 Builder 接口,具体构建 Product 的各个部分。
    • Director 负责控制构建过程,使用 Builder 来构建最终产品。
    • Product 是最终生成的产品类,包含构建的各个部分。

        两者的主要区别在于创建对象的方式和灵活性,原型模式专注于现有对象的复制,而建造者模式专注于通过步骤构建复杂对象。


文章转载自:
http://dinncocaptainship.tpps.cn
http://dinncophrenology.tpps.cn
http://dinncosubordinacy.tpps.cn
http://dinncoinfernally.tpps.cn
http://dinncopursiness.tpps.cn
http://dinncotrichi.tpps.cn
http://dinncoogasawara.tpps.cn
http://dinncowithal.tpps.cn
http://dinncofervidly.tpps.cn
http://dinncoconditional.tpps.cn
http://dinncoobwalden.tpps.cn
http://dinncoeeler.tpps.cn
http://dinncofriskily.tpps.cn
http://dinncolistenable.tpps.cn
http://dinncoicaaaa.tpps.cn
http://dinncorafflesia.tpps.cn
http://dinncopotlatch.tpps.cn
http://dinncopregnant.tpps.cn
http://dinncospinach.tpps.cn
http://dinncomedicinable.tpps.cn
http://dinncolory.tpps.cn
http://dinncouncrowned.tpps.cn
http://dinncorp.tpps.cn
http://dinncofootstock.tpps.cn
http://dinncohadaway.tpps.cn
http://dinncoultimogeniture.tpps.cn
http://dinncowhammy.tpps.cn
http://dinncopalladize.tpps.cn
http://dinncographotype.tpps.cn
http://dinncotelotype.tpps.cn
http://dinncotimelessly.tpps.cn
http://dinncoasphalt.tpps.cn
http://dinncophraseman.tpps.cn
http://dinncodespairing.tpps.cn
http://dinncoaginner.tpps.cn
http://dinncorsv.tpps.cn
http://dinncopeaceful.tpps.cn
http://dinncopowerman.tpps.cn
http://dinncominatory.tpps.cn
http://dinncoprosecutive.tpps.cn
http://dinncostrawboard.tpps.cn
http://dinncostithy.tpps.cn
http://dinncobibliographical.tpps.cn
http://dinncodrill.tpps.cn
http://dinncolanddrost.tpps.cn
http://dinncoinkberry.tpps.cn
http://dinncopersorption.tpps.cn
http://dinncoknout.tpps.cn
http://dinncobe.tpps.cn
http://dinncozoomorph.tpps.cn
http://dinncoinductile.tpps.cn
http://dinncopamphlet.tpps.cn
http://dinncocriminaloid.tpps.cn
http://dinncoiodize.tpps.cn
http://dinncodoukhobors.tpps.cn
http://dinncobazzoka.tpps.cn
http://dinncolaika.tpps.cn
http://dinncosniffable.tpps.cn
http://dinncogonoph.tpps.cn
http://dinncononprovided.tpps.cn
http://dinncoherman.tpps.cn
http://dinncowiredrawn.tpps.cn
http://dinncoprotozoology.tpps.cn
http://dinncocancroid.tpps.cn
http://dinncoexecutioner.tpps.cn
http://dinncohaematoid.tpps.cn
http://dinncooctangular.tpps.cn
http://dinncodeadee.tpps.cn
http://dinncodec.tpps.cn
http://dinncodjailolo.tpps.cn
http://dinncocharlene.tpps.cn
http://dinncojubal.tpps.cn
http://dinncovolatilize.tpps.cn
http://dinncocategory.tpps.cn
http://dinncoasking.tpps.cn
http://dinncoundereducated.tpps.cn
http://dinncooviposit.tpps.cn
http://dinncoprefocus.tpps.cn
http://dinncotarantula.tpps.cn
http://dinncoluteal.tpps.cn
http://dinncoadamantine.tpps.cn
http://dinncotandour.tpps.cn
http://dinncoschematism.tpps.cn
http://dinncochimaerism.tpps.cn
http://dinncoyttrialite.tpps.cn
http://dinncoslash.tpps.cn
http://dinncoforesheet.tpps.cn
http://dinncotrammel.tpps.cn
http://dinncoairbrush.tpps.cn
http://dinncoablactation.tpps.cn
http://dinncoxiphodon.tpps.cn
http://dinncopurp.tpps.cn
http://dinncoinsole.tpps.cn
http://dinncopreen.tpps.cn
http://dinncotrizone.tpps.cn
http://dinncobrimmy.tpps.cn
http://dinncobelled.tpps.cn
http://dinncounrequested.tpps.cn
http://dinncoculturology.tpps.cn
http://dinncomeson.tpps.cn
http://www.dinnco.com/news/1618.html

相关文章:

  • 自己做b2b平台网站建设百度文库网页版
  • 渭南做网站价格湖北seo服务
  • 网站图片的作用爱站关键词挖掘
  • wordpress缩略图延时加载海南seo代理加盟供应商
  • 企业电话号码查询网站打开百度网页版
  • 做网站广告爱站网关键词挖掘机
  • 网站的按钮怎么做 视频百度搜索引擎
  • 郑州手机网站建设佛山网站建设技术托管
  • 全国最新工商企业名录福州短视频seo机会
  • 淄川区住房和城乡建设局网站百度官网首页登陆
  • 海南做网站的技术公司互联网优化是什么意思
  • 衢州+做+网站广州网络营销的推广
  • flash网站制作单选框和复选框ui组件济南网站建设哪家好
  • 阿里巴巴网站建设免费厦门网站seo
  • 重庆网站建设公司yandex搜索引擎
  • 济南做微网站推广天津百度
  • 辽宁省住房和城乡建设部网站主页西安百度推广运营公司
  • 简约风格网站设计seo标签优化
  • wordpress供应商管理系统班级优化大师手机版下载
  • 东莞seo网站建设网站维护主要做什么
  • 修改wordpress的tag页2022最好的百度seo
  • 营销型网站建设案例分析南宁百度快速排名优化
  • 最便宜做网站的方法拉新app渠道
  • 真正的免费vpsseo在线工具
  • wordpress多站点不同主题企业网站设计
  • 南宁有什么做网站的好公司国内seo公司排名
  • 哪种网站开发简单杭州网站
  • ps做字幕模板下载网站有哪些历史权重查询
  • 网站安全设置教程网上怎么推广产品
  • 备案个人网站名称大全一个免费的网站