鍍金池/ 教程/ Java/ Java享元模式(Flyweight Pattern)
Java前端控制器模式
Java工廠設(shè)計(jì)模式
Java抽象工廠模式
Java觀察者模式
Java門(mén)面模式(或外觀模式)
Java備忘錄模式
Java MVC模式
Java單例模式
Java傳輸對(duì)象模式
Java迭代器模式
Java責(zé)任鏈模式
Java命令模式
Java原型模式
Java解釋器模式
Java適配器模式
Java狀態(tài)模式
Java中介者模式(Mediator Pattern)
Java攔截過(guò)濾器模式
Java策略模式
Java組合模式
Java業(yè)務(wù)代理模式
Java裝飾模式
Java模板模式
Java橋接模式
Java過(guò)濾器模式(條件模式)
Java享元模式(Flyweight Pattern)
Java建造者(Builder)模式
Java設(shè)計(jì)模式
Java空對(duì)象模式
Java數(shù)據(jù)訪問(wèn)對(duì)象模式
Java訪問(wèn)者模式
Java組合實(shí)體模式
Java服務(wù)定位器模式

Java享元模式(Flyweight Pattern)

享元模式(Flyweight Pattern)主要用于減少創(chuàng)建的對(duì)象數(shù)量,并減少內(nèi)存占用并提高性能。 這種類型的設(shè)計(jì)模式屬于結(jié)構(gòu)模式,因?yàn)樵撃J教峁┝藴p少對(duì)象計(jì)數(shù)的方法,從而改善應(yīng)用的對(duì)象結(jié)構(gòu)。

享元模式(Flyweight Pattern)嘗試通過(guò)存儲(chǔ)已經(jīng)存在的類似對(duì)象以重用,并在找不到匹配的對(duì)象時(shí)創(chuàng)建新對(duì)象。我們將通過(guò)繪制不同位置的20個(gè)圓圈來(lái)演示這種模式,但是這里只創(chuàng)建5個(gè)對(duì)象。只有5種顏色可用,因此color屬性用于檢查已經(jīng)存在的Circle對(duì)象。

實(shí)現(xiàn)實(shí)例

在這個(gè)實(shí)例中,將創(chuàng)建一個(gè)Shape接口和一個(gè)實(shí)現(xiàn)Shape接口的具體類Circle。在下一步中將定義一個(gè)工廠類ShapeFactory。

ShapeFactory有一個(gè)HashMapCircle作為Circle對(duì)象的顏色。每當(dāng)一個(gè)請(qǐng)求向ShapeFactory創(chuàng)建一個(gè)指定顏色的圓形時(shí),它會(huì)檢查HashMap中的圓形對(duì)象,如果找到對(duì)象則返回這個(gè)對(duì)象,否則就會(huì)創(chuàng)建一個(gè)新對(duì)象然后存儲(chǔ)在hashmap中以供將來(lái)使用,并返回這個(gè)新創(chuàng)建的對(duì)象給客戶端。

FlyWeightPatternDemo這是一個(gè)演示類,將使用ShapeFactory來(lái)獲取一個(gè)Shape對(duì)象。它將信息(紅色/綠色/藍(lán)色/黑色/白色)傳遞給ShapeFactory以獲得所需顏色的圓形。

享元模式的實(shí)現(xiàn)實(shí)例結(jié)構(gòu)如下圖中所示 -

第1步

創(chuàng)建一個(gè)接口,如下代碼 -
Shape.java

public interface Shape {
   void draw();
}

第2步

創(chuàng)建一個(gè)實(shí)現(xiàn)相同接口的具體類。
Circle.java

public class Circle implements Shape {
   private String color;
   private int x;
   private int y;
   private int radius;

   public Circle(String color){
      this.color = color;        
   }

   public void setX(int x) {
      this.x = x;
   }

   public void setY(int y) {
      this.y = y;
   }

   public void setRadius(int radius) {
      this.radius = radius;
   }

   @Override
   public void draw() {
      System.out.println("Circle: Draw() [Color : " + color + ", x : " + x + ", y :" + y + ", radius :" + radius);
   }
}

第3步

創(chuàng)建一個(gè)工廠根據(jù)給定的信息生成具體類的對(duì)象。
ShapeFactory.java

import java.util.HashMap;

public class ShapeFactory {
   private static final HashMap<String, Shape> circleMap = new HashMap();

   public static Shape getCircle(String color) {
      Circle circle = (Circle)circleMap.get(color);

      if(circle == null) {
         circle = new Circle(color);
         circleMap.put(color, circle);
         System.out.println("Creating circle of color : " + color);
      }
      return circle;
   }
}

第4步

使用工廠并通過(guò)傳遞諸如顏色的信息來(lái)獲得具體類的對(duì)象。

FlyweightPatternDemo.java

public class FlyweightPatternDemo {
   private static final String colors[] = { "Red", "Green", "Blue", "White", "Black" };
   public static void main(String[] args) {

      for(int i=0; i < 20; ++i) {
         Circle circle = (Circle)ShapeFactory.getCircle(getRandomColor());
         circle.setX(getRandomX());
         circle.setY(getRandomY());
         circle.setRadius(100);
         circle.draw();
      }
   }
   private static String getRandomColor() {
      return colors[(int)(Math.random()*colors.length)];
   }
   private static int getRandomX() {
      return (int)(Math.random()*100 );
   }
   private static int getRandomY() {
      return (int)(Math.random()*100);
   }
}

第5步

驗(yàn)證輸出,執(zhí)行上面的代碼得到以下結(jié)果 -

Creating circle of color : Black
Circle: Draw() [Color : Black, x : 36, y :71, radius :100
Creating circle of color : Green
Circle: Draw() [Color : Green, x : 27, y :27, radius :100
Creating circle of color : White
Circle: Draw() [Color : White, x : 64, y :10, radius :100
Creating circle of color : Red
Circle: Draw() [Color : Red, x : 15, y :44, radius :100
Circle: Draw() [Color : Green, x : 19, y :10, radius :100
Circle: Draw() [Color : Green, x : 94, y :32, radius :100
Circle: Draw() [Color : White, x : 69, y :98, radius :100
Creating circle of color : Blue
Circle: Draw() [Color : Blue, x : 13, y :4, radius :100
Circle: Draw() [Color : Green, x : 21, y :21, radius :100
Circle: Draw() [Color : Blue, x : 55, y :86, radius :100
Circle: Draw() [Color : White, x : 90, y :70, radius :100
Circle: Draw() [Color : Green, x : 78, y :3, radius :100
Circle: Draw() [Color : Green, x : 64, y :89, radius :100
Circle: Draw() [Color : Blue, x : 3, y :91, radius :100
Circle: Draw() [Color : Blue, x : 62, y :82, radius :100
Circle: Draw() [Color : Green, x : 97, y :61, radius :100
Circle: Draw() [Color : Green, x : 86, y :12, radius :100
Circle: Draw() [Color : Green, x : 38, y :93, radius :100
Circle: Draw() [Color : Red, x : 76, y :82, radius :100
Circle: Draw() [Color : Blue, x : 95, y :82, radius :100