什么是外观模式和中介者模式的区别?我想了解在哪种情况下选择哪种设计模式。我查看了以下链接,发现两者在用例方面相同。
外观设计模式:http://www.tutorialspoint.com/design_pattern/facade_pattern.htm 中介者设计模式:http://www.java2s.com/Tutorial/Java/0460__Design-Pattern/CoordinatingYourObjectswiththeMediatorPatterns.htm 我对以下代码段感到困惑,在这两个设计模式中看起来相似。
外观设计模式:http://www.tutorialspoint.com/design_pattern/facade_pattern.htm 中介者设计模式:http://www.java2s.com/Tutorial/Java/0460__Design-Pattern/CoordinatingYourObjectswiththeMediatorPatterns.htm 我对以下代码段感到困惑,在这两个设计模式中看起来相似。
外观类:
public class ShapeMaker {
private Shape circle;
private Shape rectangle;
private Shape square;
public ShapeMaker() {
circle = new Circle();
rectangle = new Rectangle();
square = new Square();
}
public void drawCircle(){
circle.draw();
}
public void drawRectangle(){
rectangle.draw();
}
public void drawSquare(){
square.draw();
}
}
中介者类:
public class Mediator {
Welcome welcome;
Browse browse;
Purchase purchase;
Exit exit;
public Mediator() {
welcome = new Welcome(this);
browse = new Browse(this);
purchase = new Purchase(this);
exit = new Exit(this);
}
public void handle(String state) {
if (state.equals("welcome.shop")) {
browse.execute();
} else if (state.equals("shop.purchase")) {
purchase.execute();
} else if (state.equals("purchase.exit")) {
exit.execute();
}