学习设计模式

不讲各个模式,但说哪篇的设计模式讲的容易入门
1 我的做法看完知识点一定要写最简单的相应demo,体会“知识运用”的过程 2 当看到控制台(在文末附上)上的时候体会,模式被调用的过程 3 要有自己的设计模式代码库

参考链接

单例模式

建造者模式

一般使用经典建造者模式

https://www.jianshu.com/p/0adc46f457be

代理模式

https://www.cnblogs.com/daniels/p/8242592.html

责任链模式

https://www.jianshu.com/p/3b23ac02d8af

策略模式

https://www.jianshu.com/p/5053d7ed181e

3079806-cb9aa6059b222576.png
策略模式.png

门面模式

适配器模式

https://www.jianshu.com/p/31686bf8f9a2

观察者模式

无非就是对接口的回调

https://www.jianshu.com/p/8f32da74cd8b

 /**
   * 
   * @Author:  Mr.bingley
   * @Version: 
   * @Date:  2019/4/18
   */

public class Consoler {
    public static void main(String[] args) {

        // 单例模式
        Singer singerInstance = Singer.getInstance();
        A instance = A.getInstance();

        // 构建者模式
        Computer bingley = new Computer.ComputerBuilder().name("bingley").builder();
        System.out.print(bingley.getcName());

        // 原型模式 https://www.jianshu.com/p/6d1333917ae5
        Card card1 = new Card();
        card1.setNum(9527);
        card1.setSpec(6, 7);
        System.out.println(card1.toString());
        System.out.println("----------------------");

        Card card2 = null;
        try {
            card2 = card1.clone();
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
        System.out.println(card2.toString());
        System.out.println("----------clone update value ------------");
        card1.setSpec(10, 11);
        System.out.println(card1.toString());
        System.out.println(card2.toString());
        System.out.println("----------------------");


        // 代理模式  https://www.cnblogs.com/daniels/p/8242592.html
        // 1 静态  ---
        BuyHouseProxy buyHouseProxy = new BuyHouseProxy(new BuyHouseImpl());
        buyHouseProxy.buyHouse();
        // 2 动态代理
        BuyHouse buyHouse = new BuyHouseImpl();
        BuyHouse proxyBuyHouse = (BuyHouse) Proxy.newProxyInstance(BuyHouse.class.getClassLoader(),
                new Class[]{BuyHouse.class}, new DynamicProxyHandler(buyHouse));
        proxyBuyHouse.buyHouse();


        // 责任链模式  https://www.jianshu.com/p/3b23ac02d8af
        ConcreteHandler1 concreteHandler1 = new ConcreteHandler1();
        ConcreteHandler2 concreteHandler2 = new ConcreteHandler2();
        concreteHandler1.successor = concreteHandler2;
        concreteHandler2.successor = concreteHandler1;
        concreteHandler1.handleRequest("ConcreteHandler2");

        Leader groupLeader = new GroupLeader();
        Leader director = new Director();
        Leader manager = new Manager();
        groupLeader.nextLeader = director;
        director.nextLeader = manager;

        groupLeader.handleRequest(5000);
        groupLeader.handleRequest(9000);
        groupLeader.handleRequest(12000);
        groupLeader.handleRequest(30000);


        // 策略 如何改造下面的写法
        // 如同是一个锦囊随时可以替换,每个锦囊独立可以替换,那如何替换if else 形式
        StragetyContext trafficCalculator = new StragetyContext();
        trafficCalculator.setCalculateStragety(new BusStragety());
        int result = trafficCalculator.calculatePrice(66);
        System.out.print("\n result trafficCalulator is" + result);

        // 适配器模式
        System.out.println("---------适配器模式-------------");
        Electric electric = new Electric();
        System.out.println("defaultV:" + electric.output_220v());
        //传递一个对象给适配器
        Adapter phoneAdapter = new PhoneAdapter(electric);
        System.out.println("adapterV:" + phoneAdapter.convert_5v());
        //

        // 装饰着模式


        // 观察者模式
        AObserver aObserver = new AObserver();
        BObserver bObserver = new BObserver();
        PostMan postMan = new PostMan();
        postMan.addObserver(aObserver);
        postMan.addObserver(bObserver);
        postMan.updateObsrever();


        // 工厂模式
        new ProductA().show();
        Factory.create(ProductA.class).show();
    }


}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值