【阶段总结】适配器模式&外观模式

来源:互联网 发布:手机拦截广告软件 编辑:程序博客网 时间:2024/06/05 17:00
If you need ,you can make a conlusion whenever you need.
-->
    An Andapter converts an 
interface
    Facade still allows low level access
    Disadvantage of 
"the Principle of Least Knowledge": too many wrappers
    
"the Principle of Least Knowledge": Law of Demeter
    A facade simplifiers an 
interface
    Decorator called Adapter 
this "simple pass through"
    One advantage of Facade: decoupling
    Principle that wasn
't as easy as it sounded: least knowledge
    A Decorator adds new behavior
    Example that violates the Principle of Least Knowledge: System.out.println
    Adapter client uses the target 
interface 
    An Adapter and a Decorator can be said to wrap an object

适配器模式:
    将一个类的接口,转换成客户期望的另一个接口。适配器让原本不兼容的类可以合作无间。

外观模式:
    提供了一个统一的接口,用来访问子系统中的一群接口。外观定义了一个高层接口,让子系统更容易使用。

OO原则:
    只和朋友交谈

Sutra Code as follows
-->

publi 
class Car {
    Engine engine;
    
//其他实例变量

    
public Car() {
        
//初始化发动机
    }


    
public void start(Key key) {
        Doors doors 
= new Doors();

        
boolean authorized = key.turns();

        
if (authorized)
        
{
            engine.start();
            updateDashboardDisplay();
            doors.lock();
        }

    }


    
public void updateDashboardDisplay() {
        
//更新显示
    }

}


public float getTemp() {
    
return station.getTemperature();
}


被解耦的客户才是快乐的客户。

public class TurkeyAdapter implements Duck {
    Turkey turkey;

    
public TurkeyAdapter(Turkey turkey) {
        
this.turkey = turkey;
    }


    
public void quack() {
        turkey.gobble();
    }


    
public void fly() {
        
for (int i=0; i<5; i++)
        
{
            turkey.fly();
        }

    }

}