【发布时间】:2017-10-10 04:12:23
【问题描述】:
今天,我的困境源于试图理解为什么在实施策略和桥接模式方面存在重叠。
这是桥接模式(从抽象中抽象出一个实现)
// 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);
}
}
这两种模式都涉及丢弃封装功能的策略对象。请帮助明确桥梁模式(结构)和策略模式(行为)之间的区别。我遇到的另一个困惑是它们处于不同的知识范围内。
【问题讨论】:
-
策略是行为模式,桥梁是结构模式。看看stackoverflow.com/questions/464524/…
标签: java oop design-patterns