天天看點

【設計模式】——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()
                )
        );
    }
}