今天我的困境來自於試圖理解爲什麼在策略和橋樑模式可以實施時存在重疊的原因。設計模式 - 策略與橋樑(重疊設計)
下面是橋接模式(從抽象的抽象實現)
// Shapes object structure will not be concerned how they draw themselves
public abstract class Shape {
protected DrawAPI drawAPI;
protected Shape(DrawAPI drawAPI){
this.drawAPI = drawAPI;
}
// This could also be put in the subcla
public void draw() {
drawAPI.drawCircle(radius,x,y);
}
}
現在這裏是戰略格局 - 一類的行爲或它的算法可以在運行時更改。計算器將其操作委託給策略
public class Calculator{
private Strategy strategy;
public Calculator(Strategy strategy){
this.strategy = strategy;
}
public int executeStrategy(int num1, int num2){
return strategy.doOperation(num1, num2);
}
}
這兩種模式都涉及丟棄封裝功能的策略對象。請幫助Bridge模式(結構)和策略模式(行爲)之間的明顯差異。我遇到的另一個困惑是他們在不同的知識雨傘下。
可能的[策略與橋接模式]重複(http://stackoverflow.com/questions/5863530/strategy-vs-bridge-patterns) –
http://stackoverflow.com/questions/5863530/strategy-vs -bridge-patterns,http://stackoverflow.com/questions/464524/what-is-the-difference-between-the-bridge-pattern-and-the-strategy-pattern – Andrew
策略是行爲模式,Bridge是結構模式。看看http://stackoverflow.com/questions/464524/what-is-the-difference-between-the-bridge-pattern-and-the-strategy-pattern –