[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(java)学习笔记218:开发一个多界面的应用程序之人品计算器的简单实现
1.开启新的Activity的方法: (1)Intent 意图 (2)intent.setAction("自定义") 记得在清单文件中声明 (3)intent.setData(前 ...
- redis 中文手册
https://redis.readthedocs.org/en/latest/ http://www.cnblogs.com/ikodota/archive/2012/03/05/php_redis ...
- 模板-->求逆矩阵(利用初等变换求解)
如果有相应的OJ题目,欢迎同学们提供相应的链接 相关链接 所有模板的快速链接 简单的测试 INPUT: 3 2 1 0 1 2 1 1 1 1 OUTPUT: 0.5 -0.5 0.5 0 1 -1 ...
- noip 2012 国王游戏(贪心+高精)
/* 我是不会说我考试的时候想到了正解却把金币取大看成金币求和的.... 觉得只按左右手乘积排序不太对 有反例 也可能我反例放到这个题里是错的吧 按自己的理解排的序 就是各种讨论... 假设 第i个人 ...
- codevs3008加工生产调度(Johnson算法)
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> us ...
- Ext.ComponentQuery.query()
转载:http://blog.csdn.net/jiushuai/article/details/7938476 用来找特点的所有容器(Ext.container.Container)或是通过Ext. ...
- exist的用法
http://blog.csdn.net/maladoufu/article/details/8194624 http://blog.csdn.net/xiwu1616/article/details ...
- [Head First设计模式笔记]----命令模式
命令模式定义:将“请求”封装成对象,以便使用不同的请求.队列或者日志来参数化其他对象.命令模式也支持可撤销的操作. 类图: 适用设计方案举例:实现一种遥控器,该遥控器具有七个可编程的插槽(每个都可以指 ...
- Android开发-解决 AIDL 中找不到couldn't find import for class错误
最近在使用AIDL做IPC的时候,在处理复杂的数据类型的时候,编译器总是报couldn't find import for class错误,所以在这里总结下AIDL使用的时候的一些注意事项,希望对你能 ...
- 自己总结python用xlrd\xlwt读写excel
1.首先安装xlrd\xlwt模块 xlrd模块下载地址: https://pypi.python.org/pypi/xlrd xlwt模块下载地址: https://pypi.python.org/ ...