Java设计模式泛型化之桥梁模式

这个模式的理解是这样的:

某天你发现有一类事情的完成具有一定的结构,并且这种结构可以套用相当一部分的事物。而且这些事物可以被分类。

也可以这么理解:

当你发现在同一个抽象层下面有众多的具体实现或者表现的话,而且同时这些实现具有某种共性。那么,桥梁模式可以帮助你把它们分离出来。

你可想而知,它们中的某一个肯定保持着对另一个的引用。

下面是代码结构:

一个针对行为以及表现的抽象层
几个行为抽象层的实现
几个表现抽象层的实现
行为抽象层持有对表现抽象层的引用
一个调用者类

行为抽象层

public abstract class Process {
    
    private Thing thing;
    
    public Process(Thing thing) {
        this.thing = thing;
    }

    public void doNothing() {
        thing.canDoSomething();
        thing.canDoOtherthing();
    }
}

表现抽象层

public abstract class Thing {

    public abstract void canDoSomething();
    
    public abstract void canDoOtherthing();
}

行为抽象层的实现

public class ConcreteProcessA extends Process {

    public ConcreteProcessA(Thing thing) {
        super(thing);
    }

    @Override
    public void doNothing() {
        super.doNothing();
        System.out.println("Concrete process A do nothing.");
    }
    
}

public class ConcreteProcessB extends Process {

    public ConcreteProcessB(Thing thing) {
        super(thing);
    }

    @Override
    public void doNothing() {
        super.doNothing();
        System.out.println("Concrete process B do nothing.");
    }

}

表现抽象层的实现

public class ConcreteThingA extends Thing {

    @Override
    public void canDoSomething() {
        System.out.println("Concrete thing A can do something.");
    }

    @Override
    public void canDoOtherthing() {
        System.out.println("Concrete thing A can do other thing.");
    }

}

public class ConcreteThingB extends Thing {

    @Override
    public void canDoSomething() {
        System.out.println("Concrete thing B can do something.");
    }

    @Override
    public void canDoOtherthing() {
        System.out.println("Concrete thing B can do other thing.");
    }

}

调用者类

public class BridgeCaller {

    public static void main(String[] args) {
        ConcreteThingA cta = new ConcreteThingA();
        ConcreteProcessA cpa = new ConcreteProcessA(cta);
        cpa.doNothing();
        
        ConcreteThingB ctb = new ConcreteThingB();
        ConcreteProcessB cpb = new ConcreteProcessB(ctb);
        cpb.doNothing();
        
        ConcreteProcessB cpb2 = new ConcreteProcessB(cta);
        cpb2.doNothing();
    }
}

那么,如何泛型化呢?显而易见,我们应该从Process下手。如下:

行为抽象层

public abstract class Process {
    
    private T t;
    
    public Process(T t) {
        this.t = t;
    }

    public void doNothing() {
        t.canDoSomething();
        t.canDoOtherthing();
    }
}

行为实现类

public class ConcreteProcessA extends Process {

    public ConcreteProcessA(T t) {
        super(t);
    }

    @Override
    public void doNothing() {
        super.doNothing();
        System.out.println("Concrete process A do nothing.");
    }
    
}

public class ConcreteProcessB extends Process {

    public ConcreteProcessB(T t) {
        super(t);
    }

    @Override
    public void doNothing() {
        super.doNothing();
        System.out.println("Concrete process B do nothing.");
    }

}

调用者类

public class BridgeCaller {

    public static void main(String[] args) {
        ConcreteThingA cta = new ConcreteThingA();
        ConcreteProcessA cpa = new ConcreteProcessA(cta);
        cpa.doNothing();
        
        ConcreteThingB ctb = new ConcreteThingB();
        ConcreteProcessB cpb = new ConcreteProcessB(ctb);
        cpb.doNothing();
        
        ConcreteProcessB cpb2 = new ConcreteProcessB(cta);
        cpb2.doNothing();
    }
}


你可能感兴趣的:(设计,Java,Java技术)