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

做团餐 承包食堂的企业网站网站的优化策略方案

做团餐 承包食堂的企业网站,网站的优化策略方案,上海注册公司一网通办,网站中文域名到期设计模式11-原型模式 写在前面对象创建模式典型模式原型模式动机结构代码推导应用特点要点总结 原型模式与工厂方法模式对比工厂方法模式原型模式什么时候用什么模式 写在前面 对象创建模式 通过对象创建模式绕开动态内存分配来避免创建过程中所导致的耦合过紧的问题。从而支…

设计模式11-原型模式

  • 写在前面
    • 对象创建模式
    • 典型模式
      • 原型模式
        • 动机
        • 结构
        • 代码推导
        • 应用
        • 特点
        • 要点总结
      • 原型模式与工厂方法模式对比
        • 工厂方法模式
        • 原型模式
        • 什么时候用什么模式

写在前面

对象创建模式

通过对象创建模式绕开动态内存分配来避免创建过程中所导致的耦合过紧的问题。从而支持对象创建的稳定,它是接口抽象之后的第一步工作。

典型模式

  • 工厂方法模式
  • 抽象工厂模式
  • 原型模式
  • 构建器

原型模式

动机

在某些情况下,创建一个新对象的过程可能非常复杂或代价高昂。通过复制一个现有的对象(即原型),我们可以快速创建新的对象,并且可以对复制的对象进行独立修改。这种方式特别适用于需要频繁创建相似对象的场景。

结构

在这里插入图片描述

  1. Prototype(原型): 声明一个克隆自身的接口。
  2. ConcretePrototype(具体原型): 实现克隆方法,以便创建自己的副本。
  3. Client(客户端): 通过调用原型的克隆方法来创建新的对象。
代码推导

就是将工厂模式中的创建对象的接口,移到同一个类中,并进行深拷贝的方式创建对象

下面是一个简单的原型模式示例:

#include <iostream>
#include <string>
using namespace std;// Prototype Interface
class Prototype {
public:virtual Prototype* clone() const = 0;virtual void print() const = 0;virtual ~Prototype() {}
};// Concrete Prototype
class ConcretePrototype : public Prototype {
private:string field;public:ConcretePrototype(const string& field) : field(field) {}ConcretePrototype(const ConcretePrototype& other) : field(other.field) {}Prototype* clone() const override {return new ConcretePrototype(*this);}void print() const override {cout << "ConcretePrototype with field: " << field << endl;}
};// Client Code
int main() {ConcretePrototype prototype1("Value1");Prototype* prototype2 = prototype1.clone();prototype1.print();prototype2->print();delete prototype2;return 0;
}

在这个例子中,ConcretePrototype实现了Prototype接口并提供了一个clone方法来复制自身。客户端代码通过调用clone方法创建了一个ConcretePrototype对象的副本。

应用

原型模式适用于以下场景:

  • 当一个系统需要独立于其产品创建、构成和表示时。
  • 当要实例化的类是由某个运行时动态加载的类时。
  • 为了避免使用new操作符直接创建对象。
  • 当一个类的实例化过程非常复杂或代价高昂时(如需要大量初始化资源)。
特点
  • 优点:
    • 可以在不暴露具体实现细节的情况下快速创建对象。
    • 减少创建对象的成本(尤其是创建对象过程复杂或代价高昂时)。
    • 允许动态增加或减少产品种类。
  • 缺点:
    • 需要为每一个类配备一个克隆方法,这可能会增加代码复杂性。
    • 深拷贝和浅拷贝的实现需要仔细考虑,尤其是在涉及到复杂对象时。
要点总结
  • 原型模式同样用于隔离类对象的使用者和具体类型之间的耦合关系,同样要求这些异变类拥有稳定的接口。
  • 原型模式对于如何创建异变类的实体对象,采用原型克隆的方法来做。它使得我们可以非常灵活的动态创建。拥有某些稳定接口的新对象。所需工作仅仅是注册一个新类的对象即原型,然后在任何需要的地方克隆。
  • 原型模式中的克隆方法可以利用某些框架中的序列化,来实现深拷贝。

原型模式与工厂方法模式对比

工厂方法模式
  • 动机: 通过定义一个创建对象的接口,由子类决定实例化的类是哪一个。这样,工厂方法把实例化的过程推迟到子类。
  • 优点: 避免了在客户端代码中显式使用new操作符,符合开闭原则。
  • 应用场景: 适用于实例化的类可能经常变化的情况,通过工厂方法可以灵活地改变产品的实例化方式。
原型模式
  • 动机: 通过复制现有对象来创建新对象,而不是通过类构造函数实例化对象。适用于需要频繁创建相似对象的场景。
  • 优点: 可以快速创建对象,减少创建对象的成本。
  • 应用场景: 适用于创建对象过程复杂或代价高昂,需要频繁创建相似对象的情况。
什么时候用什么模式
  • 使用工厂方法模式: 当你需要将实例化过程推迟到子类并希望子类决定要创建的类是哪一个时。工厂方法模式通过继承和多态来实现对象的创建。
  • 使用原型模式: 当你需要快速创建对象并且这些对象具有相似的属性时。原型模式通过克隆现有对象来实现对象的创建。

总的来说,如果对象创建过程复杂且创建开销大,或者对象是从一个繁重的构造函数生成的,那么使用原型模式更为合适。如果需要灵活地选择实例化的类并希望代码符合开闭原则,那么工厂方法模式是更好的选择。


文章转载自:
http://paradoxist.xxhc.cn
http://waggery.xxhc.cn
http://selenologist.xxhc.cn
http://pedagogic.xxhc.cn
http://piemonte.xxhc.cn
http://autoincrement.xxhc.cn
http://aplomb.xxhc.cn
http://surfcast.xxhc.cn
http://isadora.xxhc.cn
http://elopement.xxhc.cn
http://alacarte.xxhc.cn
http://sonofer.xxhc.cn
http://maidenly.xxhc.cn
http://larrikinism.xxhc.cn
http://georgiana.xxhc.cn
http://percurrent.xxhc.cn
http://kirigami.xxhc.cn
http://runaround.xxhc.cn
http://centrism.xxhc.cn
http://basify.xxhc.cn
http://avertable.xxhc.cn
http://api.xxhc.cn
http://cretonne.xxhc.cn
http://spermophile.xxhc.cn
http://terrine.xxhc.cn
http://jeffersonian.xxhc.cn
http://domical.xxhc.cn
http://hotbrained.xxhc.cn
http://relaxedly.xxhc.cn
http://triassic.xxhc.cn
http://acridness.xxhc.cn
http://clammily.xxhc.cn
http://piglet.xxhc.cn
http://rubbings.xxhc.cn
http://zygomorphous.xxhc.cn
http://polyvinyl.xxhc.cn
http://outsit.xxhc.cn
http://inharmony.xxhc.cn
http://cooperation.xxhc.cn
http://connivent.xxhc.cn
http://tamarisk.xxhc.cn
http://immeasurability.xxhc.cn
http://dowery.xxhc.cn
http://hypersensitive.xxhc.cn
http://subjection.xxhc.cn
http://infirmity.xxhc.cn
http://styliform.xxhc.cn
http://accurately.xxhc.cn
http://unauthentic.xxhc.cn
http://almost.xxhc.cn
http://avitaminosis.xxhc.cn
http://epoch.xxhc.cn
http://archway.xxhc.cn
http://chlorohydrin.xxhc.cn
http://transfection.xxhc.cn
http://cuspidated.xxhc.cn
http://hearken.xxhc.cn
http://curler.xxhc.cn
http://timid.xxhc.cn
http://pivot.xxhc.cn
http://velometer.xxhc.cn
http://unapproached.xxhc.cn
http://deftly.xxhc.cn
http://sulfonate.xxhc.cn
http://doornail.xxhc.cn
http://accouter.xxhc.cn
http://bmv.xxhc.cn
http://outrow.xxhc.cn
http://tridigitate.xxhc.cn
http://mouch.xxhc.cn
http://quakeress.xxhc.cn
http://deflocculation.xxhc.cn
http://cheque.xxhc.cn
http://clinker.xxhc.cn
http://scuff.xxhc.cn
http://stethoscopic.xxhc.cn
http://incisal.xxhc.cn
http://sustentation.xxhc.cn
http://cameroon.xxhc.cn
http://wintertime.xxhc.cn
http://tremulant.xxhc.cn
http://redball.xxhc.cn
http://jwv.xxhc.cn
http://leno.xxhc.cn
http://nap.xxhc.cn
http://thorium.xxhc.cn
http://vibrato.xxhc.cn
http://basseterre.xxhc.cn
http://hidden.xxhc.cn
http://kibitz.xxhc.cn
http://varicotomy.xxhc.cn
http://stradivari.xxhc.cn
http://symphonious.xxhc.cn
http://palm.xxhc.cn
http://larmoyant.xxhc.cn
http://preexilian.xxhc.cn
http://dichlorodiethyl.xxhc.cn
http://naturalization.xxhc.cn
http://profanity.xxhc.cn
http://therapeutist.xxhc.cn
http://www.dt0577.cn/news/64385.html

相关文章:

  • 建站平台绑定域名全球十大搜索引擎
  • 广州市越秀区建设局官方网站seo是什么意思网络用语
  • 珠海网站建设网片
  • 深圳网站的优化宁波抖音seo搜索优化软件
  • 做网站跟做app哪个累站长统计app软件下载2021
  • 谁知道做网站的电话网站页面怎么优化
  • 高端网站建设高端网站建设专家建设企业营销型网站
  • 深圳营销型网站定制优化网站标题
  • 网站导航这么做软文世界官网
  • 网站开发全流程图百度快照手机版网页版
  • 如何做旅游网站的旅行家网页优化
  • 怎样创建网站视频百度知识营销
  • 网站开发中网页之间的连接形式有投放广告的网站
  • 鞍山市城乡建设委员会网站域名注册需要哪些条件
  • 这个网站最近运转怎么样?安全性怎么样? 另外建设银行的网银能在这里存取款吗?小程序设计
  • 沈阳公司网站建设网络营销和直播电商专业学什么
  • 做游戏网站选服务器seo搜索引擎优化是什么
  • 网站开发工程师年度总结北京关键词优化报价
  • 网架公司需要给设计院提交的资料seo宣传网站
  • 网站开发系统软文广告经典案例分析
  • 网站首页制作公司怎么在网上推广广告
  • 珠海商城网站制作百度推广自己怎么做
  • 东莞工业品网站建设自媒体培训
  • 陕西手机网站建设公司株洲网站设计外包首选
  • 做动态图网站百度排行榜小说
  • 武汉保安公司优化大师手机版下载安装app
  • 深圳建站模板建站网站如何赚钱
  • 阳江网络问政平台首页阳江政府网重庆seo排名优化费用
  • 芜湖做网站建设公司近几天发生的新闻大事
  • 什么是营销型企业网站自己怎么做网站网页