[Design Pattern] Flywight Pattern 简单案例
Flywight Pattern, 即享元模式,用于减少对象的创建,降低内存的占用,属于结构类的设计模式。根据名字,我也将其会理解为 轻量模式。
下面是享元模式的一个简单案例。
享元模式,主要是重用已有的对象,通过修改部分属性重新使用,避免申请大量内存。
本模式需要主要两个点:
1. 对象的 key 应该是不可变得,本例中 color 作为 key,所以我在 color 前添加了 final 的修饰符。
2. 并非线程安全,多个线程同时获取一个对象,并同时修改其属性,会导致无法预计的结果。

代码实现:
public interface Shape {
public void draw();
}
Circle 类实现 Shape 接口
public class Circle implements Shape {
private int x;
private int y;
private int radius;
private final String color;
public Circle(String color){
this.color = color;
}
@Override
public void draw() {
System.out.println(" Circle draw - [" + color + "] x :" + x + ", y :" + y + ", radius :" + radius);
}
public void setX(int x) {
this.x = x;
}
public void setY(int y) {
this.y = y;
}
public void setRadius(int radius) {
this.radius = radius;
}
}
ShapeFactory 作为一个工厂,提供 Circle 的对象,同时负责重用 color 相同的对象。
public class ShapeFactory {
private HashMap<String, Shape> circleMap = new HashMap<>();
public Shape getCircle(String color){
Shape circle = null;
if (circleMap.containsKey(color)){
circle = (Circle)circleMap.get(color);
}
else{
System.out.println(" Createing cicle - " + color);
circle = new Circle(color);
circleMap.put(color, circle);
}
return circle;
}
}
代码演示,多次调用工厂 ShapeFactory 获得对象
public class FlyweightPatternDemo {
static String[] colors = "red,green,blue,black,white".split(",");
public static void main(){
ShapeFactory shapeFactory = new ShapeFactory();
shapeFactory.getCircle("red");
for (int i = 0; i < 20; i++){
Circle circle = (Circle)shapeFactory.getCircle(getRandomColor());
circle.setX(getRandomX());
circle.setY(getRandomY());
circle.setRadius(getRandomRadius());
circle.draw();
}
}
public static String getRandomColor(){
return colors[(int)(Math.random() * colors.length)];
}
public static int getRandomX(){
return (int)(Math.random() * 100);
}
public static int getRandomY(){
return (int)(Math.random() * 100);
}
public static int getRandomRadius(){
return (int)(Math.random() * 100);
}
}
参考资料
Design Patterns - Flyweight Pattern, TutorialsPoint
[Design Pattern] Flywight Pattern 简单案例的更多相关文章
- [Design Pattern] Facde Pattern 简单案例
Facade Pattern, 即外观模式,用于隐藏复杂的系统内部逻辑,提供简洁的接口给客户端调用,属于结构类的设计模式.我会将其名字理解为,门户模式. 下面是 Facade Pattern 的一个简 ...
- [Design Pattern] Front Controller Pattern 简单案例
Front Controller Pattern, 即前端控制器模式,用于集中化用户请求,使得所有请求都经过同一个前端控制器处理,处理内容有身份验证.权限验证.记录和追踪请求等,处理后再交由分发器把请 ...
- [Design Pattern] Observer Pattern 简单案例
Observer Pattern,即观察者模式,当存在一对多关系,例如一个对象一有变动,就要自动通知被依赖的全部对象得场景,属于行为类的设计模式. 下面是一个观察者模式的简单案例. Observer ...
- [Design Pattern] Mediator Pattern 简单案例
Meditor Pattern,即调解模式,用一个调解类类处理所有的沟通事件,使得降低多对象之间的沟通难度,属于行为类的设计模式.为了方便理解记忆,我也称其为,沟通模式. 下面是一个调解模式的简单案例 ...
- [Design Pattern] Iterator Pattern 简单案例
Iterator Pattern,即迭代时模式,按照顺序依次遍历集合内的每一个元素,而不用了解集合的底层实现,属于行为类的设计模式.为了方便理解记忆,我也会称其为遍历模式. 下面是一个迭代器模式的简单 ...
- [Design Pattern] Command Pattern 简单案例
Command Pattern, 即命令模式,把一个命令包裹在一个对象里面,将命令对象传递给命令的执行方,属于行为类的设计模式 下面是命令模式的一个简单案例. Stock 代表被操作的对象.Order ...
- [Design Pattern] Proxy Pattern 简单案例
Proxy Pattern, 即代理模式,用一个类代表另一个类的功能,用于隐藏.解耦真正提供功能的类,属于结构类的设计模式. 下面是 代理模式的一个简单案例. Image 定义接口,RealImage ...
- [Design Pattern] Filter Pattern 简单案例
Filter Pattern,即过滤模式,通过不同的过滤标准,或者低耦合将过滤标准组合在一起,对一组对象进行过滤,属于结构类的设计模式. 下面是一个过滤模式的简单案例. Criteria 定义过滤接口 ...
- [Design Pattern] Adapter Pattern 简单案例
Adapter Pattern, 即适配器模式,用于连接两个不兼容的接口,属于结构类的设计模式. 或者叫做,转换器模式. 下面是一个转换器模式简单案例. 假设已有 AudioPlayer 专门播放 m ...
随机推荐
- Android 项目建立步骤
使用eclipse,进行安卓开发,在建立项目的时候,有些步骤必须注意的, 本文就是对使用eclipse进行android开发的简单说明: 一.模拟器配置设定 使用eclipse开发安卓,需要用到and ...
- prim 堆优化+ kruskal 按秩优化
#include<iostream> #include<cstdio> #include<cstring> #include<queue> #defin ...
- 关于C#中get和set
在看书的时候看见了一段代码,有两个类person: public class person { public string name; } public class person { public s ...
- 新闻web小酌
首页如上 类图如下: 添加新闻的方法(dao): public boolean Add(News news) { boolean flag=false; Connection con =getConn ...
- IDisposable 接口2
定义一种释放分配的资源的方法. 命名空间: System程序集: mscorlib(在 mscorlib.dll 中) 语法 C# C++ F# VB [ComVisibleAttribute(t ...
- c# 使用递归 循环遍历导航树结构 并解析
1.数据书库结构 1 家用电器 0 一级菜单 2 手机.数码.京东通信 0 一级菜单 3 电脑.办公 0 一级菜单 4 家具.家居.厨房 0 一级菜单 5 男装.女装.童装.内衣 0 一级菜单 6 个 ...
- 关于Core Data的一些整理(二)
关于Core Data的一些整理(二) 创建NSManagedObject的子类时,有一点是在这中间要强调的一点是,要不要勾选 Use scalar properties for primitive ...
- 重拾C++ 基础知识总结(一)
1.使用gcc编译c++文件报错 proc1.cc:(.text+0x14): undefined reference to `std::cout' C++程序使用gcc命令只能编译,不能链接库文件 ...
- 【NOI2006】最大获利
[问题描述] 新的技术正冲击着手机通讯市场,对于各大运营商来说,这既是机遇,更是挑战.THU 集团旗下的CS&T 通讯公司在新一代通讯技术血战的前夜,需要做太多的准备工作,仅就站址选择一项,就 ...
- Hibernate4 clob字段存取
domain的字段: private Clob content; hibernate的xml映射 <property name="content" type="cl ...