flyweight 享元模式

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

享元模式是减小内存的使用状况。dom

何时使用享元模式:编辑器

一、须要建立大量的对象。ide

二、由于大量的内存,自己就是制约条件。性能

三、When most of the object attributes can be made external and shared.字体

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

五、其实,外在的状态能够被计算,而不是更好的存储。spa

解释以下:code

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

 建立享元模式:

与内在状态的对象被称为轻量级的对象。 当咱们执行轻量级咱们建立具体的对象,而且有存储在本征态。 要建立这些具体的对象,咱们将拥有的工厂和那个叫享元工厂。 这家工厂是为了确保该对象被共享,咱们最终不会建立重复的对象。 让咱们以图示例场景。 咱们须要绘制不一样的几何形状,如矩形和椭圆形的巨大的数字。 每一个形状可能在颜色,大小各不相同,填充类型,字体使用。 对于为了实现让限制咱们的形状两个矩形和椭圆形。 每一个形状将伴随着其直接与形状映射它的标签。 这是全部矩形都会有标签为' 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

相关文章
相关标签/搜索