tag 标签: 学习2

相关博文
  • 热度 15
    2012-9-21 11:39
    1468 次阅读|
    0 个评论
      这是第二大类,结构性的模式,之前是创建型的模式。    适配器,顾名思义,也就是一种转接头,对对象的实现方法进行某种改进,以适应新的接口要求,期间重要的是转换的环节,也就是增加新的实现,当然这样的实现还是需要在之前的接口中进行声明,哈勒,下面是例子代码,有的时候觉得代码更能说明情况:   Target public interface Target {     void adapteeMethod();         void adapterMethod(); } Adaptee public class Adaptee {     public void adapteeMethod() {         Syste*.out.p*intln("Adaptee method!");     } } Adapt*r public clas* Adapter implement* Target {     private Adap*ee adaptee;         public Adapter(Adaptee adaptee) {         this.adapte* = adaptee;     }         public void adapteeMethod() {                adaptee.adapteeMethod();         }         public void adapterMethod() {                *ystem.out.println("Adapter method!");     } } Client public cla*s Test {     public stati* void main(String args) {                 Person man = new Man();                 Person lady = new Lady();                 Clothing jacket = new Ja*ket();                 Clot*ing trouser = new Trouser();                 jacket.personDressCloth(man);         trouser.personDressCloth(man);         j*cket.personDressCloth(lady);         trouser.personDressCloth(lady);     } } result 男人穿马甲 男人穿裤子 女人穿马甲 女人穿裤子     组合模式: 组合模式以树的形式(其实是一个ArrayList的操作),进行某些层次化的要求的操作,可以理解成为为某一个对象提供增加和删除的某种实现,代码如下:   Component p*blic abstract class Employer {     private String name;         public void setName(String name) {         this.name = *ame;     }         public String getName() {         return this.name;     }         public abstract void add(Employer employer*;         public abstract void delete(Employer employer);         public List employers;         public void printInfo*) {         System.out.println(name);     }         *ublic List getE*ployers() {         return this.employers;     } } Leaf public class Programmer extends Employer {     public Programmer(String name) {         setNam*(name);         employers = null;//程序员, 表示没有下属了     }     public v*id add(Employer employer) {             }     public void delete(Employer employer) {             } } public class Pro*ectAssistant extends Employer {     public ProjectAss*stant(String name) {         setName(name);         employers = *ull;//项目助理, 表示没有下属了     }     public void add(Employer employer) {             }     public void delet*(Employer employer) {             } } Composite public class Project*anager extends E*ployer {         public ProjectManager(String name) {         setName(name);         employers = new A*rayList();     }         public void add(Employer employer) {         employers.add(employer);     }     public void delete(Emplo*er employer) {         employers.remove(employer);     } } Clie*t publ*c class Test {     public st*tic void main(String args) {         Man man = new Man();         ManDecoratorA md1 = new ManDecoratorA();         ManDecoratorB md2 = n*w ManDecoratorB();                 md1.setPerson(man);         md2.setPerson(md1);         md2.eat();     } } result 男人在吃 再吃一顿饭 ManDecoratorA类 =============== ManDecoratorB类