flyweight 享元模式

享元模式:有很多个小的对象,有很多的属性相同,就把他们变成一个对象,把那些不同的属性变成方法的参数(称之为外部状态),相同的属性称为内部状态。

享元模式是减少内存的使用情况。

什么时候使用享元模式:

1、需要创建大量的对象。

2、因为大量的内存,本身就是制约条件。

3、When most of the object attributes can be made external and shared.

4、应用程序不能强制唯一对象,后执行一个会被反复使用。

5、其实,外在的状态可以被计算,而不是更好的存储。

解释如下:

享元模式是所有关于记忆和共享。时下的平均桌面带有500 GB硬盘,4GB内存以及与此你可以在里面的东西你的整个家庭和仍然有剩余的空间,把大象在里面。我们真的需要操心内存和用法?由于成本降下来,没有限制有效地使用它。想想那些每天都不断增加,他们仍然有内存限制的移动设备。即使你有大量的内存,在某些情况下,应用程序可能需要有效地使用它。例如,假设我们正在与映射stars从宇宙的应用程序。在这个应用中,如果我们要创建一个对象的每一颗星星,然后想起来,我们将需要多少内存。的团伙已经给定的文本编辑器在他们的书中的一个例子。如果我们在一个文件中创建一个对象,为每一个字符,想起来了多少个对象,我们将创建一个长文档。会有怎样的应用程序的性能。

 创建享元模式:

与内在状态的对象被称为轻量级的对象。当我们执行轻量级我们创建具体的对象,并且有存储在本征态。要创建这些具体的对象,我们将拥有的工厂和那个叫享元工厂。这家工厂是为了确保该对象被共享,我们最终不会创建重复的对象。让我们以图示例场景。我们需要绘制不同的几何形状,如矩形和椭圆形的巨大的数字。每个形状可能在颜色,大小各不相同,填充类型,字体使用。对于为了实现让限制我们的形状两个矩形和椭圆形。每个形状将伴随着其直接与形状映射它的标签。这是所有矩形都会有标签为' R'和所有的椭圆形都会有标签为“ O” 。现在我们的轻量级将有内在的状态,因为只有标签。因此,我们将只有两个轻量级的对象。对不同性质的颜色,大小,填充类型和字体将是外在的。我们将有一个轻量级的工厂,将保持两个轻量级的对象,并分发到客户端相应。将有飞铁来实现,使我们有一个共同的蓝图,那就是轻量级接口的接口。客户端代码将使用随机数生成器来创建外在属性。我们不存放外在性质的任何地方,我们将计算在飞行中,并通过它。利用随机数发生器是为了方便起见。
  1 package com.javapapers.designpattern.flyweight;
  2  
  3 import java.awt.Color;
  4 import java.awt.Graphics;
  5  
  6 public interface MyShape {
  7   public void draw(Graphics g, int x, int y, int width, int height,
  8       Color color, boolean fill, String font);
  9 }
 10 
 11 
 12 package com.javapapers.designpattern.flyweight;
 13  
 14 import java.awt.Color;
 15 import java.awt.Font;
 16 import java.awt.Graphics;
 17  
 18 public class MyOval implements MyShape {
 19  
 20   private String label;
 21  
 22   public MyOval(String label) {
 23     this.label = label;
 24  
 25   }
 26  
 27   public void draw(Graphics oval, int x, int y, int width, int height,
 28       Color color, boolean fill, String font) {
 29     oval.setColor(color);
 30     oval.drawOval(x, y, width, height);
 31     oval.setFont(new Font(font, 12, 12));
 32     oval.drawString(label, x + (width / 2), y);
 33     if (fill)
 34       oval.fillOval(x, y, width, height);
 35   }
 36 }
 37 
 38 
 39 package com.javapapers.designpattern.flyweight;
 40  
 41 import java.awt.Color;
 42 import java.awt.Font;
 43 import java.awt.Graphics;
 44  
 45 public class MyRectangle implements MyShape {
 46  
 47   private String label;
 48  
 49   public MyRectangle(String label) {
 50     this.label = label;
 51  
 52   }
 53  
 54   public void draw(Graphics rectangle, int x, int y, int width, int height,
 55       Color color, boolean fill, String font) {
 56     rectangle.setColor(color);
 57     rectangle.drawRect(x, y, width, height);
 58     rectangle.setFont(new Font(font, 12, 12));
 59     rectangle.drawString(label, x + (width / 2), y);
 60     if (fill)
 61       rectangle.fillRect(x, y, width, height);
 62   }
 63 }
 64 
 65 package com.javapapers.designpattern.flyweight;
 66  
 67 import java.util.HashMap;
 68  
 69 public class ShapeFactory {
 70  
 71   private static final HashMap shapes = new HashMap();
 72  
 73   public static MyShape getShape(String label) {
 74     MyShape concreteShape = (MyShape) shapes.get(label);
 75  
 76     if (concreteShape == null) {
 77       if (label.equals("R")) {
 78         concreteShape = new MyRectangle(label);
 79       } else if (label.equals("O")) {
 80         concreteShape = new MyOval(label);
 81       }
 82       shapes.put(label, concreteShape);
 83     }
 84     return concreteShape;
 85   }
 86 }
 87  
 88 
 89 package com.javapapers.designpattern.flyweight;
 90  
 91 import java.awt.BorderLayout;
 92 import java.awt.Color;
 93 import java.awt.Container;
 94 import java.awt.Graphics;
 95 import java.awt.event.ActionEvent;
 96 import java.awt.event.ActionListener;
 97  
 98 import javax.swing.JButton;
 99 import javax.swing.JFrame;
100 import javax.swing.JPanel;
101  
102 public class Client extends JFrame {
103  
104   private static final int WIDTH = 400;
105   private static final int HEIGHT = 400;
106  
107   private static final String shapes[] = { "R", "O" };
108   private static final Color colors[] = { Color.red, Color.green, Color.blue };
109   private static final boolean fill[] = { true, false };
110   private static final String font[] = { "Arial", "Courier" };
111  
112   public Client() {
113     Container contentPane = getContentPane();
114  
115     JButton startButton = new JButton("Draw Shapes");
116     final JPanel panel = new JPanel();
117  
118     contentPane.add(panel, BorderLayout.CENTER);
119     contentPane.add(startButton, BorderLayout.SOUTH);
120     setSize(WIDTH, WIDTH);
121     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
122     setVisible(true);
123  
124     startButton.addActionListener(new ActionListener() {
125       public void actionPerformed(ActionEvent event) {
126         Graphics g = panel.getGraphics();
127         for (int i = 0; i < 100; ++i) {
128           MyShape shape = ShapeFactory.getShape(getRandomShape());
129           shape.draw(g, getRandomX(), getRandomY(), getRandomWidth(),
130               getRandomHeight(), getRandomColor(),
131               getRandomFill(), getRandomFont());
132         }
133       }
134     });
135   }
136  
137   private String getRandomShape() {
138     return shapes[(int) (Math.random() * shapes.length)];
139   }
140  
141   private int getRandomX() {
142     return (int) (Math.random() * WIDTH);
143   }
144  
145   private int getRandomY() {
146     return (int) (Math.random() * HEIGHT);
147   }
148  
149   private int getRandomWidth() {
150     return (int) (Math.random() * (WIDTH / 7));
151   }
152  
153   private int getRandomHeight() {
154     return (int) (Math.random() * (HEIGHT / 7));
155   }
156  
157   private Color getRandomColor() {
158     return colors[(int) (Math.random() * colors.length)];
159   }
160  
161   private boolean getRandomFill() {
162     return fill[(int) (Math.random() * fill.length)];
163   }
164  
165   private String getRandomFont() {
166     return font[(int) (Math.random() * font.length)];
167   }
168  
169   public static void main(String[] args) {
170     Client client = new Client();
171   }
172 }
View Code

posted @ 2014-04-21 22:49  场者  阅读(280)  评论(0编辑  收藏  举报