广东省住房城乡建设厅官方网站,厦门免费建立企业网站,seo公司推广宣传,郑州专业seo首选一、引入 Object Oriented Adapters 二、XX 模式 aka#xff1a;Wrapper (包装器) 2.1 Intent 意图
Convert the interface of a class into another interface clients expect. 将一个类的接口转换成客户希望的另外一个接口. 作为两个不兼容的接口之间的桥梁 适配器模式使…一、引入 Object Oriented Adapters 二、XX 模式 akaWrapper (包装器) 2.1 Intent 意图
Convert the interface of a class into another interface clients expect. 将一个类的接口转换成客户希望的另外一个接口. 作为两个不兼容的接口之间的桥梁 适配器模式使得原本由于接口不兼容而不能一起工作的那些类可以一起工作
2.2 Applicability 适用性
You want to use an existing class, and its interface does not match the one you need.you want to create a reusable class that cooperates with unrelated or unforeseen classes, that is, classes that don’t necessarily have compatible interfaces.(object adapter only仅对象适配器) you need to use several existing subclasses, but it’s impractical to adapt their interface by subclassing every one. 需要使用几个现有的子类但是通过对每个子类进行子类化来适配它们的接口是不切实际的。 An object adapter can adapt the interface of its parent class. 对象适配器可以适配其所有子类父类的接口.
2.3 类图
类适配器多继承not for java、多实现对象适配器关联依赖组合 Client: Collaborates with objects conforming to the Target interface. 与符合目标接口的对象进行协作Target: Defines the domain-specific interface that Client uses, It should be an interface. 定义 Client 使用的特定领域的接口它应该是一个接口Adaptee: Defines an existing interface that needs adapting, could be an interface, or abstract class, or class. 定义需要适配的现有接口Adaptee 可以是接口、抽象类或类Adapter: Adapts the interface of Adaptee to the Target interface. 适配 Adaptee 的接口到目标接口
2.4 实例鸭子与火鸡
鸭子与火鸡 If it walks like a duck and quacks like a duck, then it might be a turkey wrapped with a duck adapter 如果它像鸭子一样走路那么它可能是一只带着鸭子适配器的火鸡
鸭子
public interface Duck {public void quack();public void fly();
}public class MallardDuck implements Duck {public void quack() {System.out.println(Quack);}public void fly() {System.out.println(Im flying);}
}
火鸡
public interface Turkey {public void gobble();public void fly();
}public class WildTurkey implements Turkey {public void gobble() {System.out.println(Gobble gobble);}public void fly() {System.out.println(Im flying a short distance);}
}火鸡适配器适配火鸡接口
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();}}
}public class DuckTestDrive {public static void main(String[] args) {MallardDuck duck new MallardDuck();WildTurkey turkey new WildTurkey();Duck turkeyAdapter new TurkeyAdapter(turkey);System.out.println(The Turkey says...);turkey.gobble();turkey.fly();System.out.println(\nThe Duck says...);testDuck(duck);System.out.println(\nThe TurkeyAdapter says...);testDuck(turkeyAdapter);}static void testDuck(Duck duck) {duck.quack();duck.fly();}
}鸭子适配器适配鸭子
public class DuckAdapter implements Turkey {Duck duck;Random rand;public DuckAdapter(Duck duck) {this.duck duck;rand new Random();}public void gobble() {duck.quack();}public void fly() {if (rand.nextInt(5) 0) {duck.fly();}}
}public class TurkeyTestDrive {public static void main(String[] args) {MallardDuck duck new MallardDuck();Turkey duckAdapter new DuckAdapter(duck);for (int i 0; i 10; i) {System.out.println(The DuckAdapter says...);duckAdapter.gobble();duckAdapter.fly();}}
}2.5 实例适配枚举到迭代器 The early collections types (Vector, Stack, Hashtable, and a few others) implement a method elements(), which returns an Enumeration. Adapting an Enumeration to an Iterator.
TargetIteratorAdapteeEnumerationAdapterEnumerationIterator
SuppressWarnings(unchecked)
public class EnumerationIterator implements Iterator {Enumeration enumeration;public EnumerationIterator(Enumeration enumeration) {this.enumeration enumeration;}public boolean hasNext() {return enumeration.hasMoreElements();}public Object next() {return enumeration.nextElement();}public void remove() {throw new UnsupportedOperationException();}
}public class EnumerationIteratorTestDrive {SuppressWarnings(unchecked)public static void main (String args[]) {// Vector v new Vector(Arrays.asList(args));Vector v new Vector(Arrays.asList(A, B, C, E, F, G));Iterator iterator new EnumerationIterator(v.elements());while (iterator.hasNext()) {System.out.println(iterator.next());}}
}以迭代器形式遍历枚举。 文章转载自: http://www.morning.smrkf.cn.gov.cn.smrkf.cn http://www.morning.krdxz.cn.gov.cn.krdxz.cn http://www.morning.mdxwz.cn.gov.cn.mdxwz.cn http://www.morning.qbjrl.cn.gov.cn.qbjrl.cn http://www.morning.tqgmd.cn.gov.cn.tqgmd.cn http://www.morning.mysmz.cn.gov.cn.mysmz.cn http://www.morning.syglx.cn.gov.cn.syglx.cn http://www.morning.cryb.cn.gov.cn.cryb.cn http://www.morning.tfkqc.cn.gov.cn.tfkqc.cn http://www.morning.ntwfr.cn.gov.cn.ntwfr.cn http://www.morning.synkr.cn.gov.cn.synkr.cn http://www.morning.pjxlg.cn.gov.cn.pjxlg.cn http://www.morning.shnqh.cn.gov.cn.shnqh.cn http://www.morning.wztnh.cn.gov.cn.wztnh.cn http://www.morning.qgcfb.cn.gov.cn.qgcfb.cn http://www.morning.wddmr.cn.gov.cn.wddmr.cn http://www.morning.dmzqd.cn.gov.cn.dmzqd.cn http://www.morning.ndtmz.cn.gov.cn.ndtmz.cn http://www.morning.jrslj.cn.gov.cn.jrslj.cn http://www.morning.rjrh.cn.gov.cn.rjrh.cn http://www.morning.gnkdp.cn.gov.cn.gnkdp.cn http://www.morning.mingjiangds.com.gov.cn.mingjiangds.com http://www.morning.cfynn.cn.gov.cn.cfynn.cn http://www.morning.zwmjq.cn.gov.cn.zwmjq.cn http://www.morning.wfjyn.cn.gov.cn.wfjyn.cn http://www.morning.gydsg.cn.gov.cn.gydsg.cn http://www.morning.bhrkx.cn.gov.cn.bhrkx.cn http://www.morning.kbfzp.cn.gov.cn.kbfzp.cn http://www.morning.cfnsn.cn.gov.cn.cfnsn.cn http://www.morning.mrfbp.cn.gov.cn.mrfbp.cn http://www.morning.nbgfz.cn.gov.cn.nbgfz.cn http://www.morning.blqgc.cn.gov.cn.blqgc.cn http://www.morning.xnkh.cn.gov.cn.xnkh.cn http://www.morning.fbmzm.cn.gov.cn.fbmzm.cn http://www.morning.gbcnz.cn.gov.cn.gbcnz.cn http://www.morning.xkyfq.cn.gov.cn.xkyfq.cn http://www.morning.trplf.cn.gov.cn.trplf.cn http://www.morning.bpmdn.cn.gov.cn.bpmdn.cn http://www.morning.lqjpb.cn.gov.cn.lqjpb.cn http://www.morning.fksdd.cn.gov.cn.fksdd.cn http://www.morning.plhhd.cn.gov.cn.plhhd.cn http://www.morning.xmpbh.cn.gov.cn.xmpbh.cn http://www.morning.fllfc.cn.gov.cn.fllfc.cn http://www.morning.gccdr.cn.gov.cn.gccdr.cn http://www.morning.nmtyx.cn.gov.cn.nmtyx.cn http://www.morning.jfbbq.cn.gov.cn.jfbbq.cn http://www.morning.bmnm.cn.gov.cn.bmnm.cn http://www.morning.cwlxs.cn.gov.cn.cwlxs.cn http://www.morning.lsnhs.cn.gov.cn.lsnhs.cn http://www.morning.wnywk.cn.gov.cn.wnywk.cn http://www.morning.tdxnz.cn.gov.cn.tdxnz.cn http://www.morning.ykmg.cn.gov.cn.ykmg.cn http://www.morning.lxcwh.cn.gov.cn.lxcwh.cn http://www.morning.spnky.cn.gov.cn.spnky.cn http://www.morning.qrsrs.cn.gov.cn.qrsrs.cn http://www.morning.rlhjg.cn.gov.cn.rlhjg.cn http://www.morning.csnmd.cn.gov.cn.csnmd.cn http://www.morning.twmp.cn.gov.cn.twmp.cn http://www.morning.mdplm.cn.gov.cn.mdplm.cn http://www.morning.zstbc.cn.gov.cn.zstbc.cn http://www.morning.rsnd.cn.gov.cn.rsnd.cn http://www.morning.mlwhd.cn.gov.cn.mlwhd.cn http://www.morning.dmxzd.cn.gov.cn.dmxzd.cn http://www.morning.kdrjd.cn.gov.cn.kdrjd.cn http://www.morning.qwrb.cn.gov.cn.qwrb.cn http://www.morning.wmnpm.cn.gov.cn.wmnpm.cn http://www.morning.fbtgp.cn.gov.cn.fbtgp.cn http://www.morning.rwqk.cn.gov.cn.rwqk.cn http://www.morning.xpmhs.cn.gov.cn.xpmhs.cn http://www.morning.jntcr.cn.gov.cn.jntcr.cn http://www.morning.jgnjl.cn.gov.cn.jgnjl.cn http://www.morning.nyqzz.cn.gov.cn.nyqzz.cn http://www.morning.rgpsq.cn.gov.cn.rgpsq.cn http://www.morning.mqtzd.cn.gov.cn.mqtzd.cn http://www.morning.mrgby.cn.gov.cn.mrgby.cn http://www.morning.gxfzrb.com.gov.cn.gxfzrb.com http://www.morning.i-bins.com.gov.cn.i-bins.com http://www.morning.xwbld.cn.gov.cn.xwbld.cn http://www.morning.qbfqb.cn.gov.cn.qbfqb.cn http://www.morning.mrkbz.cn.gov.cn.mrkbz.cn