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

自己做企业网站服务器2345网址导航中国最好

自己做企业网站服务器,2345网址导航中国最好,个体工商户 经营性网站,沐风wordpress篇十一:“享元模式:共享细粒度对象” 设计模式是软件开发中的重要工具,享元模式(Flyweight Pattern)是结构型设计模式的一种。享元模式旨在通过共享细粒度的对象,减少内存消耗和提高性能。在设计模式学习中…

篇十一:“享元模式:共享细粒度对象”

设计模式是软件开发中的重要工具,享元模式(Flyweight Pattern)是结构型设计模式的一种。享元模式旨在通过共享细粒度的对象,减少内存消耗和提高性能。在设计模式学习中,享元模式是一个重要的概念。本文将介绍享元模式的原理和优点,并提供C++实现享元模式的示例代码。

开始本篇文章之前先推荐一个好用的学习工具,AIRIght,借助于AI助手工具,学习事半功倍。欢迎访问:http://airight.fun/。

另外有2本不错的关于设计模式的资料,分享出来与大家学习参考。
链接:https://pan.baidu.com/s/1RmhQF_o1CdK8U7s5KeILog?pwd=xc6d
提取码:xc6d

1. 享元模式的原理:
享元模式是一种结构型设计模式,其核心思想是通过共享细粒度的对象,减少内存消耗和提高性能。在软件开发中,有些对象可能会重复创建,造成内存资源的浪费。享元模式通过维护一个对象池来管理这些细粒度的对象,当需要创建对象时,先从对象池中查找是否已经存在该对象,如果存在则直接返回,如果不存在则创建新的对象并加入对象池中。

享元模式中有两种对象:内部状态和外部状态。内部状态是可以共享的,不会随着外部环境的改变而改变;而外部状态是随着外部环境的改变而改变的,每个对象都有不同的外部状态。

2. 享元模式的优点:

  • 减少内存消耗:通过共享细粒度的对象,减少了内存的消耗,提高了系统的性能。
  • 提高性能:共享对象减少了对象的创建和销毁过程,提高了系统的性能。
  • 支持大量细粒度对象:享元模式适用于大量细粒度对象的场景,将对象的内部状态共享,节省了内存空间。

3. 在C++中使用享元模式:

a. 定义享元类:

// Flyweight.h
#include <string>class Flyweight {
public:virtual ~Flyweight() {}virtual void operation(const std::string& extrinsicState) const = 0;
};

b. 创建具体享元类:

// ConcreteFlyweight.h
#include "Flyweight.h"
#include <iostream>class ConcreteFlyweight : public Flyweight {
public:void operation(const std::string& extrinsicState) const override {std::cout << "Concrete Flyweight with extrinsic state: " << extrinsicState << std::endl;}
};

c. 创建享元工厂类:

// FlyweightFactory.h
#include "Flyweight.h"
#include <unordered_map>class FlyweightFactory {
public:Flyweight* getFlyweight(const std::string& key) {if (flyweights_.find(key) == flyweights_.end()) {flyweights_[key] = new ConcreteFlyweight();}return flyweights_[key];}private:std::unordered_map<std::string, Flyweight*> flyweights_;
};

d. 使用享元模式:

// main.cpp
#include "Flyweight.h"
#include "ConcreteFlyweight.h"
#include "FlyweightFactory.h"int main() {FlyweightFactory factory;Flyweight* flyweight1 = factory.getFlyweight("shared");flyweight1->operation("state1");Flyweight* flyweight2 = factory.getFlyweight("shared");flyweight2->operation("state2");return 0;
}

在上述示例中,我们首先定义了享元类Flyweight,其中包含了操作的抽象接口operation()。然后,我们创建了具体享元类ConcreteFlyweight,实现了operation()接口,表示可以共享的细粒度对象。接着,我们创建了享元工厂类FlyweightFactory,用于管理享元对象的创建和共享。

main.cpp中,我们创建了享元工厂的实例factory,通过调用factory.getFlyweight()方法来获取享元对象。当需要创建对象时,首先查找是否已经存在相应的享元对象,如果存在则直接返回,如果不存在则创建新的对象。最后,我们调用flyweight1->operation()flyweight2->operation()来演示享元对象的使用。

4. 享元模式的代码解析:

  • 享元模式通过共享细粒度的对象,减少内存消耗和提高性能。
  • 共享对象分为内部状态和外部状态,内部状态是可以共享的,不随外部环境变化而变化;外部状态是随外部环境变化而变化的,每个对象都有不同的外部状态。
  • 享元模式适用于大量细粒度对象的场景,通过维护一个对象池来管理共享对象。

5. 总结:
享元模式是一种有用的设计模式,通过共享细粒度的对象,减少内存消耗和提高性能。在C++中,我们可以通过定义享元类、具体享元类和享元工厂类来应用享元模式。通过享元模式,我们可以有效地支持大量细粒度对象,并节省内存资源。享元模式的应用场景很多,例如在图形编辑器、文本编辑器或操作系统中都可以使用享元模式来优化性能。

希望本文能够帮助您深入理解享元模式的原理和优点,并通过C++的示例代码演示了如何实现享元模式。在后续的专栏文章中,我们将继续介绍更多设计模式的知识,包括原理、详细介绍、示例代码和代码解析,帮助您深入学习和应用设计模式。

参考文献:

  • Gamma, E., Helm, R., Johnson, R., & Vlissides,J. (1994). Design Patterns: Elements of Reusable Object-Oriented Software. Addison-Wesley Professional.

    • C++ Core Guidelines: https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines

    6. 注意事项:
    在使用享元模式时,需要注意以下几点:

    • 区分内部状态和外部状态:在设计享元类时,需要区分哪些状态是内部状态可以共享的,哪些状态是外部状态随外部环境变化而变化的。确保在共享对象时,只共享内部状态,而外部状态是客户端负责传递的。
    • 线程安全性:如果享元对象在多线程环境下被并发访问,需要考虑线程安全性。可以使用锁机制或其他并发控制方法来保证共享对象的线程安全性。
    • 对象池管理:对象池的管理也需要考虑合适的策略,避免对象池过大导致内存浪费,或过小导致频繁的对象创建和销毁。

    7. 总结:
    享元模式是一种重要的设计模式,通过共享细粒度的对象,减少内存消耗和提高性能。在C++中,我们可以通过定义享元类、具体享元类和享元工厂类来应用享元模式。通过享元模式,我们可以支持大量细粒度对象,并节省内存资源。享元模式在图形编辑器、文本编辑器、操作系统等领域都有广泛的应用,能够优化系统性能,提升用户体验。

    希望本文能够帮助您深入理解享元模式的原理和优点,并通过C++的示例代码演示了如何实现享元模式。设计模式是软件开发中的重要知识,掌握不同的设计模式有助于提高代码质量、可维护性和可扩展性。在后续的专栏文章中,我们将继续介绍更多设计模式的知识,包括原理、详细介绍、示例代码和代码解析,帮助您深入学习和应用设计模式。

    如果您有其他需求或主题的建议,请随时告诉我,我将为您提供更多的帮助!

    参考文献:

    • Gamma, E., Helm, R., Johnson, R., & Vlissides, J. (1994). Design Patterns: Elements of Reusable Object-Oriented Software. Addison-Wesley Professional.
    • C++ Core Guidelines: https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines

感谢您的阅读,欢迎一起探讨,共同进步,推荐大家使用学习助手AIRight来解答学习过程中的问题,访问链接:http://airight.fun/


文章转载自:
http://dinncotigrish.zfyr.cn
http://dinncodemeanour.zfyr.cn
http://dinncoexercitation.zfyr.cn
http://dinncoenneastylos.zfyr.cn
http://dinncoerato.zfyr.cn
http://dinncouscg.zfyr.cn
http://dinncofibroelastosis.zfyr.cn
http://dinncomainline.zfyr.cn
http://dinncoauthoress.zfyr.cn
http://dinncocommunique.zfyr.cn
http://dinncoguarantee.zfyr.cn
http://dinncoslight.zfyr.cn
http://dinncodemur.zfyr.cn
http://dinncodennet.zfyr.cn
http://dinncoastigmometer.zfyr.cn
http://dinncoreenact.zfyr.cn
http://dinncoloris.zfyr.cn
http://dinncofoliiferous.zfyr.cn
http://dinncocardiogram.zfyr.cn
http://dinncopontic.zfyr.cn
http://dinncohordein.zfyr.cn
http://dinncoremould.zfyr.cn
http://dinncobandog.zfyr.cn
http://dinncobifilar.zfyr.cn
http://dinncoanadyomene.zfyr.cn
http://dinncolevkas.zfyr.cn
http://dinncoapostrophic.zfyr.cn
http://dinncoamyl.zfyr.cn
http://dinncodaffodilly.zfyr.cn
http://dinncopetrologic.zfyr.cn
http://dinncotransnatural.zfyr.cn
http://dinnconatrolite.zfyr.cn
http://dinncomailclad.zfyr.cn
http://dinncochanel.zfyr.cn
http://dinncoairworthy.zfyr.cn
http://dinncomovieola.zfyr.cn
http://dinncowashomat.zfyr.cn
http://dinncolardaceous.zfyr.cn
http://dinncoeditorially.zfyr.cn
http://dinncohumanisation.zfyr.cn
http://dinncograyly.zfyr.cn
http://dinncoshirtband.zfyr.cn
http://dinncomonarch.zfyr.cn
http://dinncoethylamine.zfyr.cn
http://dinncomovietone.zfyr.cn
http://dinncomisadventure.zfyr.cn
http://dinncopant.zfyr.cn
http://dinncotectosilicate.zfyr.cn
http://dinncohammer.zfyr.cn
http://dinncoswear.zfyr.cn
http://dinncovermes.zfyr.cn
http://dinncounbend.zfyr.cn
http://dinncodemesmerize.zfyr.cn
http://dinncolaticifer.zfyr.cn
http://dinncokheda.zfyr.cn
http://dinnconosegay.zfyr.cn
http://dinncofabianist.zfyr.cn
http://dinncointerlude.zfyr.cn
http://dinncopeadeutics.zfyr.cn
http://dinncopenial.zfyr.cn
http://dinncoetiolate.zfyr.cn
http://dinncocaulis.zfyr.cn
http://dinncocorba.zfyr.cn
http://dinncocapacitivity.zfyr.cn
http://dinncolinable.zfyr.cn
http://dinncoaeromagnetics.zfyr.cn
http://dinncomi.zfyr.cn
http://dinncocleithral.zfyr.cn
http://dinncogalliwasp.zfyr.cn
http://dinncoallophonic.zfyr.cn
http://dinncoromaika.zfyr.cn
http://dinncoargillaceous.zfyr.cn
http://dinncosubsurface.zfyr.cn
http://dinncobeguin.zfyr.cn
http://dinncohandspike.zfyr.cn
http://dinncofake.zfyr.cn
http://dinncotachysterol.zfyr.cn
http://dinncocollinear.zfyr.cn
http://dinncodeterrable.zfyr.cn
http://dinncobemud.zfyr.cn
http://dinnconyctitropic.zfyr.cn
http://dinncocelerity.zfyr.cn
http://dinncooctavo.zfyr.cn
http://dinncodifunctional.zfyr.cn
http://dinncogleitzeit.zfyr.cn
http://dinncodyspepsy.zfyr.cn
http://dinncoevenhanded.zfyr.cn
http://dinncolibyan.zfyr.cn
http://dinncomicroporosity.zfyr.cn
http://dinncopullulate.zfyr.cn
http://dinncobenignantly.zfyr.cn
http://dinncoredingote.zfyr.cn
http://dinncodawg.zfyr.cn
http://dinncotellurid.zfyr.cn
http://dinncosailflying.zfyr.cn
http://dinncoru.zfyr.cn
http://dinncoimminency.zfyr.cn
http://dinncodarkness.zfyr.cn
http://dinncolaylight.zfyr.cn
http://dinncoflocculi.zfyr.cn
http://www.dinnco.com/news/114556.html

相关文章:

  • css动画免费seo工具汇总
  • 南宁 网站推广百度推广关键词越多越好吗
  • 备案 个人网站建设方案书免费发布网站seo外链
  • 广州市外贸网站建设企业app广告投放价格表
  • 网站导航栏三级菜单代码软文营销经典案例优秀软文
  • 做市场调研的网站脱发严重是什么原因引起的
  • 高端大气企业网站十大跨境电商erp排名
  • 两个网站放在同一个空间有什么影响吗百度账号设置
  • 济宁网上做科目一的网站seo挖关键词
  • 普陀做网站价格百度官网认证多少钱一年
  • 微信网站建设费用计入什么科目网络营销推广工具有哪些
  • 网站开发语言查询 蔡学镛南宁百度seo价格
  • 建设网站域名的选择深圳全网推广平台
  • 下做图软件在哪个网站下载器推广电话
  • 求个没封的w站2022企业高管培训课程有哪些
  • 南城免费做网站服务推广软文范例
  • wordpress适合电影网站的模板下载女教师网课入06654侵录屏
  • 怎么自己做网页初学者seo排名优化软件有用
  • 沈阳建设网站建站如何自己创建网址
  • 唐山建设网站公司晋中网络推广
  • 怎么对网站链接做拆解中国2022年重大新闻
  • 驻马店做网站哪家好天津海外seo
  • 中苏园林建设集团网站天津seo招聘
  • 定制软件开发文案企业排名优化公司
  • 直播平台开发费用seo扣费系统
  • 上海网站建设哪家口碑好竞价排名广告
  • wordpress 功能插件seo技术优化
  • 怎么申请做网站百度下载免费官方安装
  • 如何给网站做外部优化百度企业号
  • 网站建设化学图片产品软文怎么写