除了Framework,5-2節所討論的其餘技術都過於「基礎」和「細小」,有沒有辦法作更大規模的複用設計?
本節將介紹幾種典型的「面向複用」的設計模式,設計模式更強調多個類/對象之間的關係和交互過程—比接口/類複用的粒度更大。html
【適配器模式(Adapter)】java
問題描述:其中LegacyRectangle是已有的類(須要傳入矩形的一個頂點、長和寬),可是與client要求的接口不一致(須要給出對角線兩個頂點座標),咱們此時創建一個新的接口Shape以供客戶端調用,用戶經過Shape接口傳入兩個點的座標。Rectangle做爲Adapter,實現該抽象接口,經過具體的方法實現適配。算法
在不適用適配器時:會發生委派不相容。數據庫
使用了適配器後就可以解決上述問題:編程
【裝飾器模式(Decorator)】設計模式
咱們須要一層層具備多種特徵的object,經過一層層的裝飾來實現:安全
Stack s = new ArrayStack(); //構建一個普通的堆棧 UndoStack s = new UndoStack(new ArrayStack()); //構建撤消堆棧 SecureStack s = new SecureStack( new SynchronizedStack( new UndoStack(s)))//構建安全的同步撤消堆棧
java.util.Collections
中也有一些裝飾器模式: static List<T> unmodifiableList(List<T> lst); static Set<T> unmodifiableSet( Set<T> set); static Map<K,V> unmodifiableMap( Map<K,V> map);
static List<T> synchronizedList(List<T> lst); static Set<T> synchronizedSet( Set<T> set); static Map<K,V> synchronizedMap( Map<K,V> map);
【外觀模式(Facade Pattern)】數據結構
實例二:併發
假設咱們有一個具備一組接口的應用程序來使用MySql / Oracle數據庫,並生成不一樣類型的報告,如HTML報告,PDF報告等。
所以,咱們將有不一樣的接口集合來處理不一樣類型的數據庫。如今客戶端應用程序可使用這些接口來獲取所需的數據庫鏈接並生成報告。可是,當複雜性增長或界面行爲名稱混淆時,客戶端應用程序將難以管理它。
所以,咱們能夠在這裏應用Facade模式,並在現有界面的頂部提供包裝界面以幫助客戶端應用程序。ide
Two Helper Classes for MySQL and Oracle: 分別封裝了客戶端所需的功能
A façade class:
客戶端代碼:
咱們能夠看到採用了Facade設計模式的客戶端代碼簡潔了許多,更方便客戶使用。
【策略模式( Strategy)】
public interface CentralityStrategy { //對應上圖的 Strategy<<interface>> public abstract centrality(); }
1 public class degreeCentralityStrategy<L extends Vertex, E extends Edge> implements CentralityStrategy { 2 3 private final Graph<L, E> g; 4 private final L v; 5 6 public degreeCentralityStrategy(Graph<L, E> g, L v) { 7 this.g = g; 8 this.v = v; 9 } 10 11 @Override 12 public double centrality() { 13 return GraphMetrics.degreeCentrality(g, v); 14 } 15 }
1 public class closenessCentralityStrategy<L extends Vertex, E extends Edge> implements CentralityStrategy { 2 3 private final Graph<L, E> g; 4 private final L v; 5 6 public closenessCentralityStrategy(Graph<L, E> g, L v) { 7 this.g = g; 8 this.v = v; 9 } 10 11 @Override 12 public double centrality() { 13 return GraphMetrics.closenessCentrality(g, v); 14 } 15 }
1 public class betweennessCentralityStrategy<L extends Vertex, E extends Edge> implements CentralityStrategy{ 2 3 private final Graph<L, E> g; 4 private final L v; 5 6 public betweennessCentralityStrategy(Graph<L, E> g, L v) { 7 this.g = g; 8 this.v = v; 9 } 10 11 @Override 12 public double centrality() { 13 return GraphMetrics.betweennessCentrality(g, v); 14 } 15 }
public class CentralityContext { public double centrality(CentralityStrategy centralityType) { return centralityType.centrality(); } }
1 public class Main { 2 3 public static void main(String[] args) { 4 5 Graph<Vertex, Edge> graph = GraphFactory.createGraph("test/graph/GraphPoetTest.txt"); 6 String[] strings = {"F", "24"}; 7 Vertex vertex1 = VertexFactory.createVertex("to", "Word", strings); 8 CentralityContext context = new CentralityContext(); 9 double degree = context.centrality(new DegreeCentralityStrategy<>(graph, vertex1)); 10 double closeness = context.centrality(new ClosenessCentralityStrategy<>(graph, vertex1)); 11 double betweenness = context.centrality(new BetweennessCentralityStrategy<>(graph, vertex1)); 12 System.out.println(degree); 13 System.out.println(closeness); 14 System.out.println(betweenness); 15 } 16 }
【模板模式(Template method)】
1 public abstract class Edge { 2 3 private final String label; 4 private final double weight; 5 6 //the constructor 7 public Edge(String label, double weight) { 8 this.label = label; 9 this.weight = weight; 10 } 11 12 public abstract boolean addVertices(List<Vertex> vertices); 13 14 public abstract boolean containVertex(Vertex v); 15 16 public abstract Set<Vertex> vertices();
1 public class DirectedEdge extends Edge{ 2 3 private Vertex source; 4 private Vertex target; 5 6 //the constructor 7 public DirectedEdge(String label, double weight) { 8 super(label, weight); 9 } 10 11 @Override 12 public boolean addVertices(List<Vertex> vertices) { 13 source = vertices.get(0); 14 target = vertices.get(1); 15 return true; 16 } 17 18 @Override 19 public boolean containVertex(Vertex v) { 20 return source.equals(v) || target.equals(v); 21 } 22 23 @Override 24 public Set<Vertex> vertices() { 25 Set<Vertex> set = new HashSet<Vertex>(); 26 set.add(source); 27 set.add(target); 28 return set; 29 }
1 public class UndirectedEdge extends Edge{ 2 3 private Vertex vertex1; 4 private Vertex vertex2; 5 6 public UndirectedEdge(String label, double weight) { 7 super(label, weight); 8 } 9 10 @Override 11 public boolean addVertices(List<Vertex> vertices) { 12 vertex1 = vertices.get(0); 13 vertex2 = vertices.get(1); 14 return true; 15 } 16 17 @Override 18 public boolean containVertex(Vertex v) { 19 return vertex1.equals(v) || vertex2.equals(v); 20 } 21 22 @Override 23 public Set<Vertex> vertices() { 24 Set<Vertex> set = new HashSet<Vertex>(); 25 set.add(vertex1); 26 set.add(vertex2); 27 return set; 28 }
【迭代器模式( Iterator)】
Iterable
接口,並實現本身的獨特Iterator
迭代器(hasNext, next, remove
),容許客戶端利用這 個迭代器進行顯式或隱式的迭代遍歷。
Iterable
接口:實現該接口的集合對象是可迭代遍歷的
public interface Iterable<T> { ... Iterator<T> iterator(); }
Iterator
接口:迭代器
public interface Iterator<E> { boolean hasNext(); E next(); void remove(); }