模式類型:
??? Flyweight?? 享元模式 - 結構型模式?
意圖:
????The intent of this pattern is to use sharing to support a large number of objects that have part of their internal state in common where the other part of state can vary.
??? 運用共享技術有效地支持大量細粒度的對象.
?? ?
概述:
?? ?享元模式的作用在于節省內存開銷,對于系統內存在的大量相似的對象,通過享元的設計方式,可以提取出可共享的部分,將其只保留一份進而節省大量的內存開銷。
?? ?并不是所有的對象都適合進行享元設計,它要求對象具有可共享的特征,這些可共享的特征可以做享元設計,對象的可共享特征比例越大,進行享元設計后節省的內存越多。
?? ?注意,對象的不可共享特征不能計入享元設計,所以需要仔細判斷區分對象的可共享特征與不可共享特征。
????享元模式的本質是:分離和共享。分離的是對象狀態中變與不變的部分,共享的是對象中不變的部分。享元模式的關鍵之處就是在于分離變與不變,把不變的部分作為享元對象的內部狀態,把變化的部分作為外部狀態,這樣享元對象就可以達到共享的目的,從而減少對象數量,節約內存空間。
?? ?
角色:
??? 1、抽象享元(Flyweight)角色:享元接口,通過這個接口可以接收并作用于外部狀態。通過這個接口傳入外部的狀態,在享元對象的方法處理中可能會使用這些外部狀態數據。
??? 2、具體享元(ConcreteFlyweight)角色:具體的享元對象,必須是共享的,需要封裝Flyweight的內部狀態。
??? 3、非共享的具體享元(UnsharedConcreteFlyweight)角色:非共享的具體享元對象,并不是所有的Flyweight對象都需要共享,非共享的享元對象通常是對共享享元對象的組合對象。
??? 4、享元工廠(FlyweightFactory)角色:享元工廠,主要用來創建并管理共享的享元對象,并對外提供訪問共享享元對象的接口。
??? 5、客戶端(Client)角色:享元客戶端,主要的工作是維持一個對享元對象的引用,計算或存儲享元對象的外部狀態,也可以訪問共享和非共享的享元對象。
模式的應用場景:
?? ?1 一個應用程序使用了大量的對象
?? ?2 完全由于使用大量的對象,造成很大的存儲開銷?
?? ?3 對象的大多數狀態都可變為外部狀態
?? ?4 如果刪除對象的外部狀態,那么可以用相對較少的共享對象取代很多組對象
?? ?5 應用程序不依賴對象標識.由于Flyweight對象可以被共享,對于概念明顯有別的想對象,標識測試將返回真值.
結構圖:
?
模式的優缺點:
代碼:
網上的實例都差不多,這里還有一個外國網站的(和下面的實例差不多):http://www.tutorialspoint.com/design_pattern/flyweight_pattern.htm
import java.util.HashMap; import java.util.Map; class ServiceContext { private int tableIndex; private String customerName; public ServiceContext(int tableIndex, String customerName) { this.tableIndex = tableIndex; this.customerName = customerName; } public int getTableIndex() { return tableIndex; } public String getCustomerName() { return customerName; } } interface Drink { void provideService(ServiceContext serviceContext); } class Coffee implements Drink { public Coffee() { System.out.println("Coffee is created."); } @Override public void provideService(ServiceContext serviceContext) { System.out.println("Coffee is serving for table " + serviceContext.getTableIndex() + " customer " + serviceContext.getCustomerName()); } } class Tea implements Drink { public Tea() { System.out.println("Drink is created."); } @Override public void provideService(ServiceContext serviceContext) { System.out.println("Drink is serving for table " + serviceContext.getTableIndex() + " customer " + serviceContext.getCustomerName()); } } class Water implements Drink { public Water() { System.out.println("Water is created."); } @Override public void provideService(ServiceContext serviceContext) { System.out.println("Water is serving for table " + serviceContext.getTableIndex() + " customer " + serviceContext.getCustomerName()); } } class DrinkFactory { private Map<String, Drink> drinks = new HashMap<>(); public Drink createDrink(String type) { Drink drink = drinks.get(type); if (drink == null) { // 以下可以考慮抽象工廠模式實現以符合開閉原則,也可以使用反射 if (type.equals("Water")) { drink = new Water(); } else if (type.equals("Tea")) { drink = new Tea(); } else if (type.equals("Coffee")) { drink = new Coffee(); } drinks.put(type, drink); } return drink; } } public class WaiterTest { public static void main(String[] args) { String[] types = {"Water", "Tea", "Coffee"}; DrinkFactory drinkFactory = new DrinkFactory(); for (int i = 1; i <= 9; i++) { Drink drink = drinkFactory.createDrink(types[i % 3]);// Drink 可共享特征 在 DrinkFactory 內部實現享元 ServiceContext serviceContext = new ServiceContext(i, "Sir" + i);// 服務細節為不可共享特征,不能享元 drink.provideService(serviceContext);//外部特征,不可共享特征,保證調用過程不會影響下次調用。 } } }
輸出:
Drink is created. Drink is serving for table 1 customer Sir1 Coffee is created. Coffee is serving for table 2 customer Sir2 Water is created. Water is serving for table 3 customer Sir3 Drink is serving for table 4 customer Sir4 Coffee is serving for table 5 customer Sir5 Water is serving for table 6 customer Sir6 Drink is serving for table 7 customer Sir7 Coffee is serving for table 8 customer Sir8 Water is serving for table 9 customer Sir9
可以看出:在9次的服務過程中,飲品只創建了三次,一種飲品都僅僅創建了一次,減小了很多內存開銷,服務可以很好的進行。
這就是享元模式的精髓,但務必注意區分出可共享與不可共享部分,保證不可共享部分在使用之后不會影響下次使用,即不會改變可共享部分。
相關模式:
??? 享元模式與單例模式:兩者可以組合使用。享元模式中的享元工廠完全可以實現為單例,另外,享元工廠中緩存的享元對象,都是單例實例,可以看成是單例模式的一種變形控制,在享元工廠中來單例享元對象。
??? 享元模式與組合模式:兩者可以組合使用。在享元模式中,存在不需要共享軟件的享元實現,這些不需要共享的享元通常是對共享的享元對象的組合對象。換句話來說,就是通過將將兩種模式組合使用,可以實現更復雜的對象層次結構。
??? 享元模式與狀態模式:兩者可以組合使用。可以使用享元模式來共享狀態模式中的狀態對象。通常在狀態模式中,會存在數量很大的,細粒度的狀態對象,而且它們基本上可以重復使用的,都是用來處理某一個固定的狀態的,它們需要的數據通常都是由上下文傳入,也就是變化部分都被分離出去呢,所以可以用享元模式來實現這些狀態對象呢。
??? 享元模式與策略模式:兩者可以組合使用。也可以使用享元來實現策略模式中的策略對象。和狀態模式一樣,策略模式中也存在大量細粒度的策略對象,它們需要的數據同樣也是從上下文傳入的,因而可以通過享元模式來實現這些策略對象。
?
所有模式:
?? ? 創建型模式,共五種:工廠方法模式、抽象工廠模式、單例模式、建造者模式、原型模式。
結構型模式,共七種:適配器模式、裝飾器模式、代理模式、外觀模式、橋接模式、組合模式、享元模式。
??? 行為型模式,共十一種:策略模式、模板方法模式、觀察者模式、迭代子模式、責任鏈模式、命令模式、備忘錄模式、狀態模式、訪問者模式、中介者模式、解釋器模式。
??? 補充模式:空對象模式
?
原文地址:https://blog.csdn.net/paincupid/article/details/46896653
?