合规国际互联网加速 OSASE为企业客户提供高速稳定SD-WAN国际加速解决方案。 广告
### 适配器模式(Adapter) 当我们手头有某个类,而我们需要的却是另外一个类,我们就可以通过 *适配器模式* 来解决问题。唯一需要做的就是产生出我们需要的那个类,有许多种方法可以完成这种适配。 ```java // patterns/adapt/Adapter.java // Variations on the Adapter pattern // {java patterns.adapt.Adapter} package patterns.adapt; class WhatIHave { public void g() {} public void h() {} } interface WhatIWant { void f(); } class ProxyAdapter implements WhatIWant { WhatIHave whatIHave; ProxyAdapter(WhatIHave wih) { whatIHave = wih; } @Override public void f() { // Implement behavior using // methods in WhatIHave: whatIHave.g(); whatIHave.h(); } } class WhatIUse { public void op(WhatIWant wiw) { wiw.f(); } } // Approach 2: build adapter use into op(): class WhatIUse2 extends WhatIUse { public void op(WhatIHave wih) { new ProxyAdapter(wih).f(); } } // Approach 3: build adapter into WhatIHave: class WhatIHave2 extends WhatIHave implements WhatIWant { @Override public void f() { g(); h(); } } // Approach 4: use an inner class: class WhatIHave3 extends WhatIHave { private class InnerAdapter implements WhatIWant { @Override public void f() { g(); h(); } } public WhatIWant whatIWant() { return new InnerAdapter(); } } public class Adapter { public static void main(String[] args) { WhatIUse whatIUse = new WhatIUse(); WhatIHave whatIHave = new WhatIHave(); WhatIWant adapt= new ProxyAdapter(whatIHave); whatIUse.op(adapt); // Approach 2: WhatIUse2 whatIUse2 = new WhatIUse2(); whatIUse2.op(whatIHave); // Approach 3: WhatIHave2 whatIHave2 = new WhatIHave2(); whatIUse.op(whatIHave2); // Approach 4: WhatIHave3 whatIHave3 = new WhatIHave3(); whatIUse.op(whatIHave3.whatIWant()); } } ``` 我想冒昧的借用一下术语“proxy”(代理),因为在 *《设计模式》* 里,他们坚持认为一个代理(proxy)必须拥有和它所代理的对象一模一样的接口。但是,如果把这两个词一起使用,叫做“代理适配器(proxy adapter)”,似乎更合理一些。