Flyweight Design Pattern

Flyweight pattern is primarily used to

  • reduce the number of objects created
  • decrease memory footprint
  • increase performance.

Flyweight pattern tries to reuse already existing similar kind objects by storing them and creates new object when no matching object is found.

This type of design pattern comes under structural pattern.

Below is an example with diagram and code as further explanation.

Diagram

Code Explanation.

We will demonstrate this pattern by drawing 20 circles of different locations but we will create only 5 objects.

Only 5 colors are available so color property is used to check already existing Circle objects.

原文地址:https://www.cnblogs.com/codingyangmao/p/11275399.html

时间: 2024-08-12 01:48:45

Flyweight Design Pattern的相关文章

Flyweight Design Pattern 共享元设计模式

就是利用一个类来完成多种任务,不用每次都创建一个新类. 个人觉得这个设计模式在C++里面,好像可以就使用一个函数代替,利用反复调用这个函数完成任务和反复利用这个类,好像差不多. 不过既然是一个设计模式,那么就使用类来完成任务.而对于Java来说是不面向过程的,故此就必须使用这个设计模式了. 我这里设计一个仓库来保存这样的类,需要的时候反复取出来使用. 非常简单的设计模式: #include <stdio.h> class ReusedObject { public: ReusedObject(

C++ Design Pattern: What is a Design Pattern?

Q: What is a Design Pattern? A: Design Patterns represent solutions to problems what arise when developing software within a particular context. Each pattern describes a problem which occurs over and over again in our environment, and then describes

Design Pattern Iterator 迭代器设计模式

这个设计模式感觉非常简单,我们平时写程序的时候也是经常需要调用iterator的,C++和Java都是. 所以感觉没什么特别的,就是需要模仿C++或者Java的iterator类的功能吧. 这里简单写个,使用C++模仿Java的iterator一些功能,呵呵. 首先我们有一个集合类,而这个集合类包含了其他类,当我们需要遍历这个集合类包含的类的时候,就好使用iterator功能了. 比如有一个原始的类: class SalesPerson { private: string name; strin

设计模式(Design pattern)概述

设计模式(Design pattern)是一套被反复使用.多数人知晓的.经过分类编目的.代码设计经验的总结, 使用设计模式是为了可重用代码.让代码更容易被他人理解.保证代码可靠性 设计框架 可复用面向对象软件系统一般划分为两大类:应用程序工具箱和框架(Framework) 我们平时开发的具体软件都是应用程序 而框架是构成一类特定软件可复用设计的一组相互协作的类 框架通常定义了应用体系的整体结构类和对象的关系等等设计参数,以便于具体应用实现者能集中精力于应用本身的特定细节. 框架主要记录软件应用中

DP什么意思 design pattern 设计模式

DP  design pattern 大话设计模式  中的DP 是设计模式的意思 设计模式的书 ,最经典最原始的就是 GOF 的<设计模式>了. 设计模式的书基本上大多是以这 20 多个模式分开讲.含<大话设计模式> 学了 OOL 写的程序基本上是 OB 的. 只有慢慢掌握了 DP 才能写出真正的 OO 程序. 思想 -> 设计原则 -> DP -> OOD

Head First Design Pattern 读书笔记(2) 观察者模式

Head First Design Pattern 读书笔记(2) Observer Pattern 观察者模式 Observer Pattern 类图 定义 观察者模式:在对象间定义一个一对多的关系,当其中一个的对象发生改变时,所有依赖于这个对象的对象(即观察者们)都会自动更新或做执行某些行为. 几个OO的原测 尽量以松耦合的方式处理对象间关系–>软件工程时候学的"高內聚,低耦合"的好处 关于观察者模式 被观察对象通知观察者可以使用推送的方式(类图中带参数的notifyActi

Design Pattern Singleton 单一模式

单一模式的几个注意点: 一) 设计单一模式,首先需要把构造函数给私有化了,不让外界访问,那么外界只能通过提供的函数获取一个新的类. 二) C++的单一模式,记得要在类外初始化一个类,否则或内存出错的. 三) 这个唯一的类必须是要静态的 程序: #ifndef _SINGLETON_H #define _SINGLETON_H #include <iostream> #include <string> using namespace std; class DuGuJiuJian {

Design Pattern 设计模式1 - Strategy 1

实现 : Defferent Heros attack Defferently. - 不同的英雄使用不用的招数 Strategy设计的思路: 基类A,更加小的基类B,新的继承类C: 1 从基类A中抽出一个更加小的基类B 2 利用这个更加小的基类B实现不同的效果 3 把这个更加小的基类B包含进基类A中去 4 新的继承类C只需要和基类A打交道,设计不同行为,不需要理会更加小的基类B #pragma once #ifndef _STRATEGY_HEROS_H #define _STRATEGY_HE

简单工厂设计模式(Simple Factory Design Pattern)

[引言]最近在Youtub上面看到一个讲解.net设计模式的视频,其中作者的一个理解让我印象很深刻:所谓的设计模式其实就是运用面向对象编程的思想来解决平时代码中的紧耦合,低扩展的问题.另外一点比较有见解的是,区分了设计模式(Design Pattern),结构模式(Architecture Pattern),架构类型(Architecture Style). 如下图所示 Design Pattern:是基于代码层面的,就是针对解决功能模块之间的问题而采用恰当的设计模式,比如依赖注入,简单工厂,适