原创 Java设计模式学习2

2012-9-21 11:39 1465 15 15 分类: 消费电子

 

  1. 这是第二大类,结构性的模式,之前是创建型的模式。
  2.  

 适配器,顾名思义,也就是一种转接头,对对象的实现方法进行某种改进,以适应新的接口要求,期间重要的是转换的环节,也就是增加新的实现,当然这样的实现还是需要在之前的接口中进行声明,哈勒,下面是例子代码,有的时候觉得代码更能说明情况:

 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) {
        Target target = new Adapter(new Adaptee());
        tar*et.adapteeMethod();
        
        target.adapterM*thod();
    }
}

result


Adaptee method!
Adapter method!

 

  1. 桥接模式:

桥接模式的核心在于一个扩展,即可以进行扩展,而且在不同的情况下可以进行扩展之间的不同的组合,其中有一个抽象对象,即Abstract class,这个抽象的对象就可以进行拓展,进行某些性质的设定,代码如下:

 Abstr*ction


public abstract class Person {

    private Clothing clothing;
    
    pr*vate String type;

    public Clothing getClothing() {
        return clothing;
    }

    publi* void setClothing() {
        this.clothing = *lothingFactory.getClothing();
    }
    
    public void setType(String type) {
        t*is.type = type;
    }
    
    public String getType() {
        return this.ty*e;
    }
    
    public abstract void dress();
}

RefinedAbstraction


public class Man extends *erson {
    
    public Man() {
        setType("男人");
    }
    
    public void dress() {
        Clothing clothing = get*lothing();
        clothing.personDressCloth(this);
    }
}

public class Lady extends Person {

    public Lady() {
        setTyp*("女人");
    }
    
    public void dress() {
        Cloth*ng clothing = getClothing();
        c*othing.personDressCloth(this);
    }
}

Implemento*


public abstract class Clothing {

    public abstract void personDressC*oth(*erson person);
}

ConcreteImplemento*


public class *ack*t extends Clothing {

    public void personDressCloth(Person person) {
        System.out.println(person.getType() + "穿马甲");
    }
}

public cl*ss Trouser extends Clothing {

    public void personDressCloth(Person person) {
        System.ou*.println(*erson.getType() + "穿裤子");
    }
}

Test


public class Te*t {

    public s*atic 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


男人穿马甲
男人穿裤子
女人穿马甲
女人穿裤子

 

 

  1. 组合模式:

组合模式以树的形式(其实是一个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) {
        Employer pm = new ProjectManager("项目经理");
        Emplo*er pa = new ProjectAssistant("项目助理");
        Employer progra*mer1 = new Programmer("程序员一");
        Employer programmer2 = new Programmer("程序员二");
        
        pm.add(pa);//为项目经理添加项目助理
        pm.add(programmer2);//*项目经理*加程序员
        
        List ems = pm.getEm*loyers();
        for (Employer em : ems) {
            System.out.println(em.getNam*());
        }
    *
}

result


项目助理
程序员二

 

  1. 装饰模式:
  2.  
  3.  

 Component


public interface Person {

    void eat();
}

ConcreteComponent


*ublic class M*n implements Person {

        public void eat() {
               System.out.println("男人在吃");
        *
}

Decorator


public abstrac* class Decorator implements Perso* {

    protected Person person*
    
    public void setPerson(Person person) {
        this.person = person;
    }
    
    public void eat() {
        person.eat();
    }
}

ConcreteDec*rator


publi* class ManDecoratorA extends Decorator {

    public void eat() {
        super.eat();
        reEat();
        Sy*tem.out.println("ManDecoratorA类");
    }

    public void reEat() {
        System.out.println("再吃一顿饭");
    *
}

public class ManDecoratorB extends Decorator *
    
    public void eat() {
        super.eat();
        Syst*m.out.println("===============");
        System.out.println("ManDecoratorB类");
    }
}

Test


public class Test {

    public st*tic void main(Strin*[] 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类

 

文章评论0条评论)

登录后参与讨论
我要评论
0
15
关闭 站长推荐上一条 /2 下一条