设计模式中的结构型

设计模式中的创建型,主要解决对象的创建问题。那么接下来这篇文章主要讲下设计模式中的结构型,主要关系类与类之间的组合和组装。
主要有以下几个模式:代理模式,桥接模式,组合模式,享元模式,装饰器,适配器模式,门面模式。

代理模式

代理模式,分为静态代理、动态代理。我们一般用于添加一些非功能性的需求时,用代理模式。主要应用场景:aop/rpc调用等

public class TestProxy{
    public static void main(String[] args){
        Animal animal = (Animal) new AnimalProxy().createProxy(new Cat());
        animal.name();
    }
}


 class AnimalProxy{
    public Object createProxy(Object objectproxies){
        return Proxy.newProxyInstance(objectproxies.getClass().getClassLoader(),objectproxies.getClass().getInterfaces(),new AnimalInvocation(objectproxies));
    }


    private class AnimalInvocation implements InvocationHandler{
         Object object;


         public AnimalInvocation(Object object){
             this.object = object;
         }


         public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
             System.out.println("method start");
             Object result = method.invoke(object,args);
             System.out.println("method enc");
             return result;
         }
     }
}

桥接模式

当一个类处理两块相关的逻辑类时,通过组合的形式进行解耦。比如一个车,有奔驰,宝马,比亚迪,同时也有燃油,电动,混动。那么如果实现每一种车的话会有M*N个类,为了缓解这种依赖爆炸,则通过定义一个车,一个方式,然后通过桥接模式进行关联。

public class Brige {
    public static void main(String[] args){
     Car car =  new BwmCar(new OilType());
     car.name();
    }
}


interface Car{
    void name();
}


interface CarType{
    String getType();
}


class OilType implements CarType{
    public String getType() {
        return "OilType";
    }
}
class ElectType implements CarType{
    public String getType() {
        return "ElectType";
    }
}


class MixedType implements CarType{
    public String getType() {
        return "MixedType";
    }
}


class BwmCar implements Car{
    String type;
    public BwmCar(CarType carType){
        this.type = carType.getType();
    }
    public void name() {
        System.out.println("this is BwmCar,it's Type is "+type);
    }
}


class BydCar implements Car{
    String type;
    public BydCar(CarType carType){
        this.type = carType.getType();
    }
    public void name() {
        System.out.println("this is BydCar,it's Type is "+type);
    }
}

装饰器模式

装饰器模式,主要应用场景则是在继承同一个类,通过组合的方式增强方法。那为什么不是写在同一个接口里面呢。因为不是所有的方法都需要增强,这样更降低耦合

public class TestWapper {
    public static void main(String[] args){
        TelNotifitionWapper telNotifitionWapper = new TelNotifitionWapper();
        telNotifitionWapper.notifyMsg();
    }
}


interface Notifition{
    void notifyMsg();


    void getMsg();
}


class TelNotifition implements Notifition{


    public void notifyMsg() {
        System.out.println("call somebody telphone");
    }


    public void getMsg() {
        System.out.println("wow");
    }
}


class TelNotifitionWapper extends TelNotifition implements Notifition{
    @Override
    public void notifyMsg() 
        super.notifyMsg();
        System.out.println("say helo");
    }
}

适配器场景

适配器场景主要应用于不兼容的接口改为兼容的接口,主要适用的场景是,兼容老接口,统一多个接口等需要包一层来达到统一,作为一个补救措施。

public class TestAdapter {
    public static void main(String[] args){
      AIHome aiHome =  new AIHome();
      aiHome.goHome(new SpeakerSwitch());
      aiHome.goHome(new SwitchButtonAdapter(new ListenSwich()));
    }


}
class AIHome{
    public void goHome(SwitchButton switchButton){
        switchButton.open();
    }
}


interface SwitchButton{
    void open();
}


class SpeakerSwitch implements SwitchButton{


    public void open() {
        System.out.println("start speak..");
    }
}


class ListenSwich {
    public void push(){
        System.out.println("start listen to music");
    }
}


class SwitchButtonAdapter implements SwitchButton{
    private ListenSwich listenSwich;
    public SwitchButtonAdapter(ListenSwich listenSwich){
        this.listenSwich = listenSwich;
    }
    public void open() {
        listenSwich.push();
    }
}

门面模式

当一个系统对外暴露多个接口,不易管理和维护,这个时候可以讲多个接口 包裹成一个接口方便使用。使用使用场景:提高接口的易用性,提高性能,解决分布式事务问题。
门面模式和适配器模式都是封装,那么我们可以看下区别。适配器模式主要适用于接口转换,门面模式适用于接口整合。

组合模式

主要适用于树形接口的存储。

class TreeNode{
    private String name;
    private String depth;
    private List<TreeNode> treeNodeList = new ArrayList<TreeNode>();
}

享元模式

主要用于复用对象,对象为不可变的对象。和池化技术来比较的话,一个是为了对象复用,一个是重复使用节省效率

public class TestAdapter {
    public static void main(String[] args){
      Editor editor = new Editor();
      editor.add("pwd");
      editor.add("sh");
    }




}
class Editor{
    public static final List<String> history = new ArrayList<String>() ;




    public void add(String words){
        history.add(words);
    }




    public void delete(String words){
        history.remove(history.size()-1);
    }
}

以上就是设计模式中的结构型,主要侧重的类和类的组合和组装。

posted on 2021-05-14 09:56  升级打小怪兽  阅读(54)  评论(0编辑  收藏  举报