享元模式的兩個狀態:html
/** * 享元對象接口 */ public interface Flyweight { void operation(String extrinsicState); } /** * 享元對象工廠類,享元類 */ public final class FlyweightFactory { /** * 享元類容器 */ private static Map<String, Flyweight> flyweights = new HashMap<>(); public static Flyweight getFlyweight (String key) { if (flyweights.containsKey(key)) { return flyweights.get(key); } else { Flyweight flyweight = new ConcreteFlyweight(key); flyweights.put(key, flyweight); return flyweight; } } public static int size () { return flyweights.size(); } } /** * 能夠被共享的對象 */ public class ConcreteFlyweight implements Flyweight{ private String intrinsicState; public ConcreteFlyweight(String intrinsicState) { this.intrinsicState = intrinsicState; } @Override public void operation(String extrinsicState) { System.out.println(this.intrinsicState); System.out.println(extrinsicState); } } /** * 不被共享的對象 */ public class UnsharedConcreteFlyweight implements Flyweight { private String allState; public UnsharedConcreteFlyweight(String allState) { this.allState = allState; } @Override public void operation(String extrinsicState) { System.out.println(this.allState); System.out.println(extrinsicState); } }
/** * 應用與測試 */ public class Test { public static void main(String[] args) { Stream.of("1", "1", "2", "2", "3").forEach(key -> { Flyweight flyweight = FlyweightFactory.getFlyweight(key); flyweight.operation("測試" + key); }); System.out.println("size: " + FlyweightFactory.size()); } }
1---:測試1 1---:測試1 2---:測試2 2---:測試2 3---:測試3 size: 3
享元模式角色介紹java
Flyweight
: 抽象享元類。全部具體享元類的超類或者接口,經過這個接口,Flyweight能夠接受並做用於外部專題。ConcreteFlyweight
: 具體享元類。指定內部狀態,爲內部狀態增長存儲空間。UnsharedConcreteFlyweight
: 非共享具體享元類。指出那些不須要共享的Flyweight子類。FlyweightFactory
: 享元工廠類。用來建立並管理Flyweight對象,它主要用來確保合理地共享Flyweight,當用戶請求一個Flyweight時,FlyweightFactory就會提供一個已經建立的Flyweight對象或者新建一個(若是不存在)。假設一個公司中的每一個部門的部門經理都要進行彙報不止一次
/** * 員工接口 */ public interface Employee { void report(); } /** * 員工工廠 */ public class EmployeeFactory { private static final Map<String, Employee> EMPLOYEE_MAP = new HashMap<>(); public static Employee getManager(String department) { Manager manager = (Manager) EMPLOYEE_MAP.get(department); if (manager == null) { manager = new Manager(department); System.out.println("建立部門經理:" + department); String reportContent = department + "部門彙報:這次報告內容是。。。"; manager.setReportContent(reportContent); System.out.println("建立報告: " + reportContent); EMPLOYEE_MAP.put(department, manager); } return manager; } } /** * 部門經理 */ public class Manager implements Employee { /** * 內部狀態 */ private String title = "部門經理"; /** * 外部狀態,須要在應用層引入,就是外部狀態 */ private String department; private String reportContent; public Manager(String department) { this.department = department; } public void setReportContent(String reportContent) { this.reportContent = reportContent; } @Override public void report() { System.out.println(reportContent); } }
/** * 測試與應用 */ public class Test { private static final String[] departments = {"RD", "QA", "PM", "BD"}; /** * 要注意線程安全的問題 */ public static void main(String[] args) { for (int i = 0; i < 10; i ++) { String department = departments[(int) (Math.random() * departments.length)]; Manager manager = (Manager) EmployeeFactory.getManager(department); manager.report(); } System.out.println(EmployeeFactory.size()); } }
建立部門經理:BD 建立報告: BD部門彙報:這次報告內容是。。。 BD部門彙報:這次報告內容是。。。 建立部門經理:PM 建立報告: PM部門彙報:這次報告內容是。。。 PM部門彙報:這次報告內容是。。。 建立部門經理:QA 建立報告: QA部門彙報:這次報告內容是。。。 QA部門彙報:這次報告內容是。。。 QA部門彙報:這次報告內容是。。。 QA部門彙報:這次報告內容是。。。 BD部門彙報:這次報告內容是。。。 PM部門彙報:這次報告內容是。。。 建立部門經理:RD 建立報告: RD部門彙報:這次報告內容是。。。 RD部門彙報:這次報告內容是。。。 PM部門彙報:這次報告內容是。。。 PM部門彙報:這次報告內容是。。。 4
慕課網設計模式精講
: https://coding.imooc.com/class/270.html 設計模式讀書筆記----享元模式
: https://www.cnblogs.com/chenssy/p/3330555.html