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 简单案例的更多相关文章

  1. [Design Pattern] Facde Pattern 简单案例

    Facade Pattern, 即外观模式,用于隐藏复杂的系统内部逻辑,提供简洁的接口给客户端调用,属于结构类的设计模式.我会将其名字理解为,门户模式. 下面是 Facade Pattern 的一个简 ...

  2. [Design Pattern] Front Controller Pattern 简单案例

    Front Controller Pattern, 即前端控制器模式,用于集中化用户请求,使得所有请求都经过同一个前端控制器处理,处理内容有身份验证.权限验证.记录和追踪请求等,处理后再交由分发器把请 ...

  3. [Design Pattern] Observer Pattern 简单案例

    Observer Pattern,即观察者模式,当存在一对多关系,例如一个对象一有变动,就要自动通知被依赖的全部对象得场景,属于行为类的设计模式. 下面是一个观察者模式的简单案例. Observer ...

  4. [Design Pattern] Mediator Pattern 简单案例

    Meditor Pattern,即调解模式,用一个调解类类处理所有的沟通事件,使得降低多对象之间的沟通难度,属于行为类的设计模式.为了方便理解记忆,我也称其为,沟通模式. 下面是一个调解模式的简单案例 ...

  5. [Design Pattern] Iterator Pattern 简单案例

    Iterator Pattern,即迭代时模式,按照顺序依次遍历集合内的每一个元素,而不用了解集合的底层实现,属于行为类的设计模式.为了方便理解记忆,我也会称其为遍历模式. 下面是一个迭代器模式的简单 ...

  6. [Design Pattern] Command Pattern 简单案例

    Command Pattern, 即命令模式,把一个命令包裹在一个对象里面,将命令对象传递给命令的执行方,属于行为类的设计模式 下面是命令模式的一个简单案例. Stock 代表被操作的对象.Order ...

  7. [Design Pattern] Proxy Pattern 简单案例

    Proxy Pattern, 即代理模式,用一个类代表另一个类的功能,用于隐藏.解耦真正提供功能的类,属于结构类的设计模式. 下面是 代理模式的一个简单案例. Image 定义接口,RealImage ...

  8. [Design Pattern] Filter Pattern 简单案例

    Filter Pattern,即过滤模式,通过不同的过滤标准,或者低耦合将过滤标准组合在一起,对一组对象进行过滤,属于结构类的设计模式. 下面是一个过滤模式的简单案例. Criteria 定义过滤接口 ...

  9. [Design Pattern] Adapter Pattern 简单案例

    Adapter Pattern, 即适配器模式,用于连接两个不兼容的接口,属于结构类的设计模式. 或者叫做,转换器模式. 下面是一个转换器模式简单案例. 假设已有 AudioPlayer 专门播放 m ...

随机推荐

  1. jquery ajax 提交表单(file && input)

    用到的插件 jquery.js jquery.form.js[http://malsup.github.io/jquery.form.js] 提交页面 <form enctype="m ...

  2. python函数的使用和返回值

    #coding=utf-8 def a(): i=1a() #函数的返回值,用return语句实现 #一个返回值的情况def test(): i=7 return iprint test() #多个返 ...

  3. Android开发手记(19) 数据存储四 ContentProvider

    转载自:http://www.cnblogs.com/devinzhang/archive/2012/01/20/2327863.html Android为数据存储提供了五种方式: 1.SharedP ...

  4. WebSocket协议

    websocket 简介 (2013-04-09 15:39:28) 转载▼   分类: websocket 一 WebSocket是html5新增加的一种通信协议,目前流行的浏览器都支持这个协议,例 ...

  5. UIWebView取消长按放大(用于长按识别二维码)

    禁用长按UIWebView时放大镜及选择功能: //通过js调用 - (void)webViewDidFinishLoad:(UIWebView*)webView { // Disable user ...

  6. delphi服务程序(service)的调试方法

    方法一: 1.调试delphi 写的服务程序,有这么一个办法.原来每次都是用attach to process方法,很麻烦.并且按照服务线程的执行线路,可能会停不到想要的断点.笨办法是,在proced ...

  7. c#利用WebClient和WebRequest获取网页源代码

    C#中一般是可以利用WebClient类和WebRequest类获取网页源代码.下面分别说明这两种方法的实现.   WebClient类获取网页源代码   WebClient类   WebClient ...

  8. JavaScript设计模式之单例模式

    一.单例模式概念 单例就是保证一个类只有一个实例,实现方法一般是先判断实例存在与否,如果存在直接返回,如果不存在就创建了再返回,这就确保了一个类只有一个实例对象.在JavaScript里,单例作为一个 ...

  9. java判断网络连接是否正常

    /** * 判断本机当前的网络状态是否联通 * 在这里主要用到中国天气信息,所以访问百度地址是否能够访问成功来判断当前的网络状态 */ public static boolean isConnect( ...

  10. C# FTP操作

    using System; using System.Collections.Generic; using System.Net; using System.IO; namespace FTP操作 { ...