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

相關文章
相關標籤/搜索