天天看点

【设计模式】——23种设计模式——核心设计模式(含理解+代码)【2】

设计模式——桥接模式

在代码层面最通俗的理解就是,构造的时候传入不同的对象最终表现不一样,用对象构造整体,随插随拔,又可以称之为配置模式

配置的统一接口

public interface DrawConfigMeta {
    public void drawCircle(int radius, int x, int y);
}
           

承载配置的容器的抽象类

public abstract class Shape {
    protected DrawConfigMeta drawConfigMeta;
    protected Shape(DrawConfigMeta drawConfigMeta){
        this.drawConfigMeta = drawConfigMeta;
    }
    public abstract void draw();
}
           

承载配置的容器的实现类

public class Circle extends Shape {
    private int x, y, radius;

    public Circle(int x, int y, int radius, DrawConfigMeta drawConfigMeta) {
        super(drawConfigMeta);
        this.x = x;
        this.y = y;
        this.radius = radius;
    }

    public void draw() {
        drawConfigMeta.drawCircle(radius,x,y);
    }
}
           

测试

public class _Test01 {
    static class RedCircle implements DrawConfigMeta {
        @Override
        public void drawCircle(int radius, int x, int y) {
            System.out.println("Drawing Circle[ color: red, radius: "
                    + radius +", x: " +x+", "+ y +"]");
        }
    }
    static class GreenCircle implements DrawConfigMeta {
        @Override
        public void drawCircle(int radius, int x, int y) {
            System.out.println("Drawing Circle[ color: green, radius: "
                    + radius +", x: " +x+", "+ y +"]");
        }
    }

    public static void main(String[] args) {
        Shape redCircle = new Circle(100,100, 10, new RedCircle());
        Shape greenCircle = new Circle(100,100, 10, new GreenCircle());
        redCircle.draw();
        greenCircle.draw();
    }
}
           

设计模式——策略者模式

统一实现一个策略接口,策略接口决定了策略函数的参数和返回值;然后针对需求定义不用的策略对象

注意如果你的需求的根据用户的值的情况去

动态的选择一种策略

,这个也避免不了if else这个情况所以不建议这里使用策略模式

但是另一种表现有点像桥接模式,就是

动态的更新一种策略

策略模式只适合固定选择策略,不适合动态选择策略

最佳应用——Map封装策略算法

策略模式的精髓在于策略算法的集中化,否则就会桥接模式没啥区别了,所以利用HashMap是封装策略是最好的,当然用枚举去引用更规范一点

枚举

public enum PreferenceEnum{
    NORMAL("NORMAL","正常价格"),
    PREFERENCE("PREFERENCE","优惠"),
    SUPERPREFERENCE("SUPERPREFERENCE","超级优惠");
    private final String key;
    private final String value;
    public String getKey() {
        return key;
    }
    public String getValue() {
        return value;
    }
    PreferenceEnum(String key, String value) {
        this.key = key;
        this.value = value;
    }

}
           

策略函数接口,统一调用原型

public interface Stragery {
    public double doOperation(int price);
}
           

策略工厂,存储所有算法对象

public class StrageryFactory {
    public static Map<PreferenceEnum,Stragery> stringStrageryMap=new ConcurrentHashMap<>();
    static {
        stringStrageryMap.put(PreferenceEnum.NORMAL, new Stragery() {
            public double doOperation(int price) {
                return (double)price;
            }
        });
        stringStrageryMap.put(PreferenceEnum.PREFERENCE, new Stragery() {
            public double doOperation(int price) {
                return (double)price*0.8;
            }
        });
        stringStrageryMap.put(PreferenceEnum.SUPERPREFERENCE, new Stragery() {
            public double doOperation(int price) {
                return (double)price*0.5;
            }
        });
    }
    public static Stragery getShopStragery(PreferenceEnum key){
        return stringStrageryMap.get(key);
    }
}

           

测试

注意,策略模式只适合在代码中静态的写死某个策略用在哪里,不适合动态选择策略,动态选择,只能内部自己写一个if else了,只不过这样子做if else就不必再业务代码中判断了,

选择策略和策略实现分离,也是可以的,看大家自己选择吧

public class _Test01 {
    public static void main(String[] args) {
        Stragery shopStragery1 = StrageryFactory.getShopStragery(PreferenceEnum.NORMAL);
        Stragery shopStragery2 = StrageryFactory.getShopStragery(PreferenceEnum.PREFERENCE);
        Stragery shopStragery3 = StrageryFactory.getShopStragery(PreferenceEnum.SUPERPREFERENCE);

        double v1 = shopStragery1.doOperation(5);
        double v2 =shopStragery2.doOperation(5);
        double v3 =shopStragery3.doOperation(5);
        System.out.println("策略价格1;"+v1);
        System.out.println("策略价格2;"+v2);
        System.out.println("策略价格3;"+v3);
    }
}
           

设计模式——装饰器模式

扩展功能,我们会想到继承,在子类新增功能,但是每次版本都导致一个子类层级关系太多,所以最好的办法就是置入对象的方式进行扩展,当然这个置入的整体设计需要设计者自己把控,如何层次分明的置入,以至于不会缺失整体性;

每个层级之间只用关系彼此而不必在乎其他

接口继承链

public interface Shape {
    void draw();
}
public interface ShapeDecorator extends Shape{
    public void paint();
}
public interface ShapeDecoratorDecorator extends ShapeDecorator {
    public void listen();
}
           

逐层注入表现

public class ShapeImpl implements Shape {
    public void draw() {
    }
}

public class ShapeDecoratorImpl implements ShapeDecorator {
    protected Shape decoratedShape;

    public ShapeDecoratorImpl(Shape decoratedShape) {
        this.decoratedShape = decoratedShape;
    }
    public void draw() {
        decoratedShape.draw();
    }
    public void paint() {
        decoratedShape.draw();
    }
}
public class ShapeDecoratorDecoratorImpl implements ShapeDecoratorDecorator {
    protected ShapeDecorator decoratedShape;
    public ShapeDecoratorDecoratorImpl(ShapeDecorator decoratedShape) {
        this.decoratedShape = decoratedShape;
    }
    public void draw() {
        decoratedShape.draw();
    }
    public void paint() {
        decoratedShape.paint();
    }
    public void listen(){
        System.out.println("唱歌");
    }
}
           

测试

public class _Test01 {
    public static void main(String[] args) {
        /**
         * @创建实现
         */
        Shape shape =new ShapeImpl();
        ShapeDecorator shapeDecorator =new ShapeDecoratorImpl(shape);
        ShapeDecoratorDecorator decoratorDecorator=new ShapeDecoratorDecoratorImpl(shapeDecorator);
        /**
         * 底层只有一个功能
         */
        shape.draw();

        /**
         * 中层扩展了一个功能
         */
        shapeDecorator.draw();
        shapeDecorator.paint();
        /**
         * 高层扩展了二个功能
         */
        decoratorDecorator.draw();
        decoratorDecorator.paint();
        decoratorDecorator.listen();

        /**
         * 是不是很熟悉 JAVA的IO流??
         */
        ShapeDecoratorDecorator makeDecoratorDecorator=new ShapeDecoratorDecoratorImpl(
                new ShapeDecoratorImpl(
                        new ShapeImpl()
                )
        );
    }
}