> 文档中心 > Java设计模式学习以及底层源码分析

Java设计模式学习以及底层源码分析

源码在分支master

工厂模式

把具体创建产品的细节封装起来,你要什么产品,我给你什么产品即可。

简单工厂模式

在这里插入图片描述

工厂方法模式

缓存层:抽象类
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

抽象工厂模式

缓存层是:接口
在这里插入图片描述
在这里插入图片描述

原型模式

问题:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
原型模式的源码分析:Spring 中的 getBean()这个方法低层实现。是否选择单例、和原型模式。

自己的Code

/* * Package: com.example.demo.design.prototype * Type:    User * Date:    2022-03-06 14:22 * * Copyright (c) 2022 HUANENG GUICHENG TRUST CORP.,LTD All Rights Reserved. * * You may not use this file except in compliance with the License. */package com.example.demo.design.prototype;import lombok.Data;import lombok.extern.slf4j.Slf4j;/ * 功能描述: 原型模式分析: *  无线创建相同对象  但是对象的地址值都不是相同的 * * * @author Songxianyang * @date 2022-03-06 14:22 */@Data@Slf4jpublic class User implements Cloneable {    private  String name;    private int age; public User(String name, int age) { this.name = name; this.age = age;    } @Override    public String toString() { return "User{" + "name='" + name + '\'' + ", age=" + age + '}';    } @Override    protected Object clone()  { User user =null; try { user = (User) super.clone(); } catch (CloneNotSupportedException e) {     log.error("clone: ", "无法或不应克隆对象"); } return user;    }}

实现:

/* * Package: com.example.demo.design.prototype * Type:    Test * Date:    2022-03-06 14:29 * * Copyright (c) 2022 HUANENG GUICHENG TRUST CORP.,LTD All Rights Reserved. * * You may not use this file except in compliance with the License. */package com.example.demo.design.prototype;/ * 功能描述: * * @author Songxianyang * @date 2022-03-06 14:29 */public class Test {    public static void main(String[] args) { User user = new User("sxy", 22); System.out.println(user); System.out.println(user.clone()); System.out.println(user.clone());     System.out.println(user==user.clone());    }}

在这里插入图片描述
在这里插入图片描述

建造者模式

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

源码盘点

项目截图

Java设计模式学习以及底层源码分析

/* * Package: com.example.demo.design.builder * Type:    BuilderHouse * Date:    2022-03-06 21:14 * * Copyright (c) 2022 HUANENG GUICHENG TRUST CORP.,LTD All Rights Reserved. * * You may not use this file except in compliance with the License. */package com.example.demo.design.builder;/ * 功能描述: 抽象的一个构建者 * * @author Songxianyang * @date 2022-03-06 21:14 */public interface BuilderHouse {    House house = new House(); /     * 地基     */    void foundation(); /     * 砌墙     */    void buildWall(); /     * 封顶     */    void capping(); /     * 构建对象     * @return house     */    default House builder() { return house;    }}
/* * Package: com.example.demo.design.builder * Type:    Commander * Date:    2022-03-06 21:29 * * Copyright (c) 2022 HUANENG GUICHENG TRUST CORP.,LTD All Rights Reserved. * * You may not use this file except in compliance with the License. */package com.example.demo.design.builder;/ * 功能描述: 指挥者:负责怎么去执行 :行为 * * @author Songxianyang * @date 2022-03-06 21:29 */public class Commander {    BuilderHouse builderHouse = null; public Commander(BuilderHouse builderHouse) { this.builderHouse = builderHouse;    } public House builder () { builderHouse.foundation(); builderHouse.buildWall(); builderHouse.capping(); return builderHouse.builder();    }}
/* * Package: com.example.demo.design.builder * Type:    HighHouse * Date:    2022-03-06 21:28 * * Copyright (c) 2022 HUANENG GUICHENG TRUST CORP.,LTD All Rights Reserved. * * You may not use this file except in compliance with the License. */package com.example.demo.design.builder;/ * 功能描述: 高楼 * * @author Songxianyang * @date 2022-03-06 21:28 */public class HighHouse implements BuilderHouse {    @Override    public void foundation() {  System.out.println("高楼打地基");    } @Override    public void buildWall() { System.out.println("高楼砌墙");    } @Override    public void capping() { System.out.println("高楼 吊顶");    }}
/* * Package: com.example.demo.design.builder * Type:    House * Date:    2022-03-06 21:11 * * Copyright (c) 2022 HUANENG GUICHENG TRUST CORP.,LTD All Rights Reserved. * * You may not use this file except in compliance with the License. */package com.example.demo.design.builder;import lombok.Data;/ * 功能描述: 房子是一个产品类 * * @author Songxianyang * @date 2022-03-06 21:11 */@Datapublic class House {    private String spar;    private Integer height;}
/* * Package: com.example.demo.design.builder * Type:    PlainHouse * Date:    2022-03-06 21:20 * * Copyright (c) 2022 HUANENG GUICHENG TRUST CORP.,LTD All Rights Reserved. * * You may not use this file except in compliance with the License. */package com.example.demo.design.builder;/ * 功能描述:普通房子 实现类 * * @author Songxianyang * @date 2022-03-06 21:20 */public class PlainHouse implements BuilderHouse {    @Override    public void foundation() {  System.out.println("普通房子打地基");    } @Override    public void buildWall() { System.out.println("普通房子砌墙");    } @Override    public void capping() { System.out.println("普通房子 吊顶");    }}
/* * Package: com.example.demo.design.builder * Type:    Test * Date:    2022-03-06 21:33 * * Copyright (c) 2022 HUANENG GUICHENG TRUST CORP.,LTD All Rights Reserved. * * You may not use this file except in compliance with the License. */package com.example.demo.design.builder;/ * 功能描述:测试类 * * @author Songxianyang * @date 2022-03-06 21:33 */public class Test {    public static void main(String[] args) { //普通房子 PlainHouse plainHouse = new PlainHouse(); Commander commander = new Commander(plainHouse); commander.builder(); System.out.println("-------------------"); //高楼大厦     HighHouse highHouse = new HighHouse(); Commander high = new Commander(highHouse); high.builder(); }}

测试截图

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

源码分析

new StringBuilder();建造者 建造一个产品需要那些步骤:Appendable具体建造出什么样的抽象对象具有什么功能:AbstractStringBuilder 指挥者:具体指挥返回什么样的对象StringBuilder、StringBuffer

适配器模式-结构化

在这里插入图片描述
在这里插入图片描述

类适配器

Java设计模式学习以及底层源码分析

上代码

/* * Package: com.example.demo.design.adapter.classadapter * Type:    Voltage * Date:    2022-03-12 10:14 * * Copyright (c) 2022 HUANENG GUICHENG TRUST CORP.,LTD All Rights Reserved. * * You may not use this file except in compliance with the License. */package com.example.demo.design.adapter.classadapter;/ * 功能描述:5V电压转换器 * * @author Songxianyang * @date 2022-03-12 10:14 */public class Change extends Source implements VoltageAdapter {    @Override    public String output5v() { String v220 = super.output(); v220="5v"; return v220; }}
/* * Package: com.example.demo.design.adapter.classadapter * Type:    Phone * Date:    2022-03-12 10:28 * * Copyright (c) 2022 HUANENG GUICHENG TRUST CORP.,LTD All Rights Reserved. * * You may not use this file except in compliance with the License. */package com.example.demo.design.adapter.classadapter;/ * 功能描述:手机充电 * * @author Songxianyang * @date 2022-03-12 10:28 */public class Phone {    private VoltageAdapter adapter; public Phone(VoltageAdapter adapter) { this.adapter = adapter;    } public void chargePhone() { System.out.println("我得手机以及充上"+adapter.output5v());    }}
/* * Package: com.example.demo.design.adapter * Type:    Source * Date:    2022-03-12 10:03 * * Copyright (c) 2022 HUANENG GUICHENG TRUST CORP.,LTD All Rights Reserved. * * You may not use this file except in compliance with the License. */package com.example.demo.design.adapter.classadapter;/ * 类适配器 * 功能描述:电源类220v   被适配器 * * @author Songxianyang * @date 2022-03-12 10:03 */public class Source {    public String output() { return "220v";    }}
/* * Package: com.example.demo.design.adapter.classadapter * Type:    Test * Date:    2022-03-12 10:34 * * Copyright (c) 2022 HUANENG GUICHENG TRUST CORP.,LTD All Rights Reserved. * * You may not use this file except in compliance with the License. */package com.example.demo.design.adapter.classadapter;/ * 功能描述: * * @author Songxianyang * @date 2022-03-12 10:34 */public class Test {    public static void main(String[] args) { Phone phone = new Phone(new Change()); phone.chargePhone();    }}
/* * Package: com.example.demo.design.adapter.classadapter * Type:    SourceAdapter * Date:    2022-03-12 10:06 * * Copyright (c) 2022 HUANENG GUICHENG TRUST CORP.,LTD All Rights Reserved. * * You may not use this file except in compliance with the License. */package com.example.demo.design.adapter.classadapter;/ * 功能描述: 充电器适配器 * * @author Songxianyang * @date 2022-03-12 10:06 */public interface VoltageAdapter {    /     * 输出5v电压     * @return s     */    String output5v();}

Java设计模式学习以及底层源码分析

对象适配器

在这里插入图片描述
Java设计模式学习以及底层源码分析

/* * Package: com.example.demo.design.adapter.classadapter * Type:    Voltage * Date:    2022-03-12 10:14 * * Copyright (c) 2022 HUANENG GUICHENG TRUST CORP.,LTD All Rights Reserved. * * You may not use this file except in compliance with the License. */package com.example.demo.design.adapter.objadapter;/ * 功能描述:5V电压转换器 :适配器 * * @author Songxianyang * @date 2022-03-12 10:14 */public class Change {    private Source source = new Source(); public String output5v() { String v220 = source.output(); v220="5v"; return v220; }}
/* * Package: com.example.demo.design.adapter.classadapter * Type:    Phone * Date:    2022-03-12 10:28 * * Copyright (c) 2022 HUANENG GUICHENG TRUST CORP.,LTD All Rights Reserved. * * You may not use this file except in compliance with the License. */package com.example.demo.design.adapter.objadapter;/ * 功能描述:手机充电 * * @author Songxianyang * @date 2022-03-12 10:28 */public class Phone {    private Change change= new Change(); public void chargePhone() { System.out.println("我得手机以及充上"+change.output5v());    }}
/* * Package: com.example.demo.design.adapter * Type:    Source * Date:    2022-03-12 10:03 * * Copyright (c) 2022 HUANENG GUICHENG TRUST CORP.,LTD All Rights Reserved. * * You may not use this file except in compliance with the License. */package com.example.demo.design.adapter.objadapter;/ * 类适配器 * 功能描述:电源类220v   被适配器 * * @author Songxianyang * @date 2022-03-12 10:03 */public class Source {    public String output() { return "220v";    }}
/* * Package: com.example.demo.design.adapter.classadapter * Type:    Test * Date:    2022-03-12 10:34 * * Copyright (c) 2022 HUANENG GUICHENG TRUST CORP.,LTD All Rights Reserved. * * You may not use this file except in compliance with the License. */package com.example.demo.design.adapter.objadapter;/ * 功能描述: * * @author Songxianyang * @date 2022-03-12 10:34 */public class Test {    public static void main(String[] args) { Phone phone = new Phone(); phone.chargePhone();    }}

在这里插入图片描述

接口适配器

在这里插入图片描述

结构:

Java设计模式学习以及底层源码分析

代码以及优化
/* * Package: com.example.demo.design.adapter.interfaceadapter * Type:    AVoltageAdapter * Date:    2022-03-12 15:48 * * Copyright (c) 2022 HUANENG GUICHENG TRUST CORP.,LTD All Rights Reserved. * * You may not use this file except in compliance with the License. */package com.example.demo.design.adapter.interfaceadapter;/ * 功能描述:抽象的一个多种电压 初始化 * * @author Songxianyang * @date 2022-03-12 15:48 */public abstract class AbsVoltageAdapter implements IVoltageAdapter { @Override    public String v5() { return null;    } @Override    public String v21() { return null;    }}
/* * Package: com.example.demo.design.adapter.interfaceadapter * Type:    V * Date:    2022-03-12 15:47 * * Copyright (c) 2022 HUANENG GUICHENG TRUST CORP.,LTD All Rights Reserved. * * You may not use this file except in compliance with the License. */package com.example.demo.design.adapter.interfaceadapter;/ * 功能描述: 多种电压 接口 * * @author Songxianyang * @date 2022-03-12 15:47 */public interface IVoltageAdapter {    /     * 充手机     * @return s     */    public String v5(); /     * 充电车     * @return s     */    String v21();    /*    ......     */}
/* * Package: com.example.demo.design.adapter.interfaceadapter * Type:    Phone * Date:    2022-03-12 15:56 * * Copyright (c) 2022 HUANENG GUICHENG TRUST CORP.,LTD All Rights Reserved. * * You may not use this file except in compliance with the License. */package com.example.demo.design.adapter.interfaceadapter;/ * 功能描述:手机需要5v电压 * * @author Songxianyang * @date 2022-03-12 15:56 */public class Phone {    private AbsVoltageAdapter abs; public Phone(AbsVoltageAdapter abs) { this.abs = abs;    } public void change() { System.out.println("小米手机已经充上"+abs.v5());    }}
/* * Package: com.example.demo.design.adapter.interfaceadapter * Type:    PhoneAdapter * Date:    2022-03-12 15:53 * * Copyright (c) 2022 HUANENG GUICHENG TRUST CORP.,LTD All Rights Reserved. * * You may not use this file except in compliance with the License. */package com.example.demo.design.adapter.interfaceadapter;/ * 功能描述:手机适配器5v * 优化点 * @author Songxianyang * @date 2022-03-12 15:53 */public class PhoneAdapter extends AbsVoltageAdapter {    @Override    public String v5() { return "5v";    }}
/* * Package: com.example.demo.design.adapter.classadapter * Type:    Test * Date:    2022-03-12 10:34 * * Copyright (c) 2022 HUANENG GUICHENG TRUST CORP.,LTD All Rights Reserved. * * You may not use this file except in compliance with the License. */package com.example.demo.design.adapter.interfaceadapter;/ * 功能描述: * * @author Songxianyang * @date 2022-03-12 10:34 */public class Test {    public static void main(String[] args) { Phone phone = new Phone(new PhoneAdapter()); phone.change();    }}

Java设计模式学习以及底层源码分析

源码分析 mvc

在这里插入图片描述

源码 分析

DispatcherServlet dispatcherServlet = new DispatcherServlet();DispatcherServlet:{方法:doDispatch(HttpServletRequest request){// 获取 requestHttpServletRequest processedRequest = request;// 执行器对象:Handler HandlerExecutionChain mappedHandler = null; // 获取映射执行器对象 mappedHandler = this.getHandler(processedRequest); // 获取具体的 HandlerAdapter 的适配器 HandlerAdapter ha = this.getHandlerAdapter(mappedHandler.getHandler()); // 不同的适配器 返回不同的 mv mv = ha.handle(processedRequest, response, mappedHandler.getHandler());//被调用的代码//遍历获取具体的handler 并返回protected HandlerAdapter getHandlerAdapter(Object handler) throws ServletException { if (this.handlerAdapters != null) {     Iterator var2 = this.handlerAdapters.iterator();     while(var2.hasNext()) {  HandlerAdapter adapter = (HandlerAdapter)var2.next();  if (adapter.supports(handler)) {      return adapter;  }     } } throw new ServletException("No adapter for handler [" + handler + "]: The DispatcherServlet configuration needs to include a HandlerAdapter that supports this handler");    }}//接口 :具体的实现 有6个适配器public interface HandlerAdapter {handler 标记    boolean supports(Object handler);    @Nullable    ModelAndView handle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception;@Deprecated    long getLastModified(HttpServletRequest request, Object handler);}}

在这里插入图片描述
适配器到此结束;
Java设计模式学习以及底层源码分析

桥接模式-结构化

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

代码分享

类图

Java设计模式学习以及底层源码分析

/* * Package: com.example.demo.bridg * Type:    Brand * Date:    2022-03-12 20:47 * * Copyright (c) 2022 HUANENG GUICHENG TRUST CORP.,LTD All Rights Reserved. * * You may not use this file except in compliance with the License. */package com.example.demo.bridg;/ * 功能描述:为手机品牌做的接口 * * @author Songxianyang * @date 2022-03-12 20:47 */public interface Brand {    /     * 打电话     */    void call(); /     * 上网     */    void net();}
/* * Package: com.example.demo.bridg * Type:    Flat * Date:    2022-03-12 20:55 * * Copyright (c) 2022 HUANENG GUICHENG TRUST CORP.,LTD All Rights Reserved. * * You may not use this file except in compliance with the License. */package com.example.demo.bridg;/ * 功能描述:平板 手机 * * @author Songxianyang * @date 2022-03-12 20:55 */public class FlatPhone extends StylePhone {    private String name = "平板:"; public FlatPhone(Brand brand) { super(brand);    } @Override    public void call() { System.out.println(name); super.call();    } @Override    protected void net() { System.out.println(name);  super.net();    }}
/* * Package: com.example.demo.bridg * Type:    FlipPhone * Date:    2022-03-12 20:55 * * Copyright (c) 2022 HUANENG GUICHENG TRUST CORP.,LTD All Rights Reserved. * * You may not use this file except in compliance with the License. */package com.example.demo.bridg;/ * 功能描述:翻盖 手机 * * @author Songxianyang * @date 2022-03-12 20:55 */public class FlipPhone extends StylePhone {    private String name = "翻盖:"; public FlipPhone(Brand brand) { super(brand);    } @Override    public void call() { System.out.println(name); super.call();    } @Override    protected void net() { System.out.println(name); super.net();    }}
/* * Package: com.example.demo.bridg * Type:    XiaoMPhone * Date:    2022-03-12 20:49 * * Copyright (c) 2022 HUANENG GUICHENG TRUST CORP.,LTD All Rights Reserved. * * You may not use this file except in compliance with the License. */package com.example.demo.bridg;/ * 功能描述:小米手机 * * @author Songxianyang * @date 2022-03-12 20:49 */public class HuaweiPhone implements Brand {    @Override    public void call() { System.out.println("Huawei手机打电话");    } @Override    public void net() { System.out.println("Huawei手机上网");    }}
/* * Package: com.example.demo.bridg * Type:    StylePhone * Date:    2022-03-12 20:53 * * Copyright (c) 2022 HUANENG GUICHENG TRUST CORP.,LTD All Rights Reserved. * * You may not use this file except in compliance with the License. */package com.example.demo.bridg;/ * 功能描述:手机样式 * * @author Songxianyang * @date 2022-03-12 20:53 */public abstract class StylePhone { private Brand brand; public StylePhone(Brand brand) { this.brand = brand;    }      // 开始搭桥      protected void call(){ brand.call();    }  protected void net() { brand.net();    }}
/* * Package: com.example.demo.bridg * Type:    Test * Date:    2022-03-12 20:51 * * Copyright (c) 2022 HUANENG GUICHENG TRUST CORP.,LTD All Rights Reserved. * * You may not use this file except in compliance with the License. */package com.example.demo.bridg;/ * 功能描述:桥接模式   : 抽象类与接口分开 * * @author Songxianyang * @date 2022-03-12 20:51 */public class Test {    public static void main(String[] args) { StylePhone flatPhone = new FlatPhone(new XiaoMiPhone()); flatPhone.call(); flatPhone.net(); System.out.println("----------------------"); StylePhone flipPhone = new FlipPhone(new XiaoMiPhone()); flipPhone.call(); flipPhone.net(); System.out.println("-----------------"); StylePhone huawei = new FlipPhone(new HuaweiPhone()); huawei.call(); huawei.net();    }}
/* * Package: com.example.demo.bridg * Type:    XiaoMPhone * Date:    2022-03-12 20:49 * * Copyright (c) 2022 HUANENG GUICHENG TRUST CORP.,LTD All Rights Reserved. * * You may not use this file except in compliance with the License. */package com.example.demo.bridg;/ * 功能描述:小米手机 * * @author Songxianyang * @date 2022-03-12 20:49 */public class XiaoMiPhone implements Brand{    @Override    public void call() { System.out.println("Xiaomi手机打电话");    } @Override    public void net() { System.out.println("Xiaomi手机上网");    }}

在这里插入图片描述

jdbc源码分析

没有分析明白

桥接总结

在这里插入图片描述

装饰者设计模式

需求

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

代码结构

在这里插入图片描述

详细代码 展示

/* * Package: com.example.demo.design.decorator.master * Type:    AmericanoCoffee * Date:    2022-03-13 15:08 * * Copyright (c) 2022 HUANENG GUICHENG TRUST CORP.,LTD All Rights Reserved. * * You may not use this file except in compliance with the License. */package com.example.demo.design.decorator.master;/ * 功能描述:美式咖啡 * * @author Songxianyang * @date 2022-03-13 15:08 */public class AmericanoCoffee extends Coffee{    public AmericanoCoffee() { setName("美式咖啡"); setPrice(2.0f);    }}
/* * Package: com.example.demo.design.decorator.master * Type:    ChinaCoffee * Date:    2022-03-13 15:12 * * Copyright (c) 2022 HUANENG GUICHENG TRUST CORP.,LTD All Rights Reserved. * * You may not use this file except in compliance with the License. */package com.example.demo.design.decorator.master;/ * 功能描述:中国咖啡 * * @author Songxianyang * @date 2022-03-13 15:12 */public class ChinaCoffee extends Coffee{    public ChinaCoffee() { setName("中国咖啡"); setPrice(2.0f);    }}
/* * Package: com.example.demo.design.decorator.master * Type:    Coffee * Date:    2022-03-13 15:07 * * Copyright (c) 2022 HUANENG GUICHENG TRUST CORP.,LTD All Rights Reserved. * * You may not use this file except in compliance with the License. */package com.example.demo.design.decorator.master;/ * 功能描述: * * @author Songxianyang * @date 2022-03-13 15:07 */public class Coffee extends Drink { @Override    float count() { return getPrice();    }}
/* * Package: com.example.demo.design.decorator.master * Type:    DecoratorSeasone * Date:    2022-03-13 15:14 * * Copyright (c) 2022 HUANENG GUICHENG TRUST CORP.,LTD All Rights Reserved. * * You may not use this file except in compliance with the License. */package com.example.demo.design.decorator.master;/ * 功能描述: 装饰者 调料 * * @author Songxianyang * @date 2022-03-13 15:14 */public class DecoratorSeason extends Drink{    private Drink drink; public DecoratorSeason(Drink drink) { this.drink = drink;    } @Override    float count() { // getPrice 当前调料的钱   drink.count() 上一次计算的钱 return getPrice()+drink.count();    }     @Override    public void setName(String name) { super.setName(name+"&&"+drink.getName());    }}
/* * Package: com.example.demo.design.decorator.master * Type:    Drink * Date:    2022-03-13 15:01 * * Copyright (c) 2022 HUANENG GUICHENG TRUST CORP.,LTD All Rights Reserved. * * You may not use this file except in compliance with the License. */package com.example.demo.design.decorator.master;/ * 功能描述: 饮料类 抽象起来 * * @author Songxianyang * @date 2022-03-13 15:01 */public abstract class Drink {    private String name; private float price; /     * 计算价格     * @return     */    abstract float count(); public String getName() { return name;    } public void setName(String name) { this.name = name;    } public float getPrice() { return price;    } public void setPrice(float price) { this.price = price;    }}
/* * Package: com.example.demo.design.decorator.master * Type:    Milk * Date:    2022-03-13 15:21 * * Copyright (c) 2022 HUANENG GUICHENG TRUST CORP.,LTD All Rights Reserved. * * You may not use this file except in compliance with the License. */package com.example.demo.design.decorator.master;/ * 功能描述:调料 牛奶 * * @author Songxianyang * @date 2022-03-13 15:21 */public class Milk extends DecoratorSeason{    public Milk(Drink drink) { super(drink); setName("调料 牛奶"); setPrice(1.0f);    } }
/* * Package: com.example.demo.design.decorator.master * Type:    SoyBean * Date:    2022-03-13 15:23 * * Copyright (c) 2022 HUANENG GUICHENG TRUST CORP.,LTD All Rights Reserved. * * You may not use this file except in compliance with the License. */package com.example.demo.design.decorator.master;/ * 功能描述:豆浆 * * @author Songxianyang * @date 2022-03-13 15:23 */public class SoyBean extends DecoratorSeason{    public SoyBean(Drink drink) { super(drink); setName("豆浆"); setPrice(2.0f);    }}
/* * Package: com.example.demo.design.decorator * Type:    Test * Date:    2022-02-20 13:36 * * Copyright (c) 2022 HUANENG GUICHENG TRUST CORP.,LTD All Rights Reserved. * * You may not use this file except in compliance with the License. */package com.example.demo.design.decorator.master;/ * 功能描述: * * @author Songxianyang * @date 2022-02-20 13:36 */public class Test {    public static void main(String[] args) { //普通一杯 中国咖啡 Drink order = new ChinaCoffee(); System.out.println(order.getPrice()); // 中国咖啡+牛奶 order= new Milk(order); System.out.println("咖啡总价"+ order.count()); System.out.println(order.getName()); // 中国咖啡+豆浆 order=new SoyBean(order);     System.out.println("咖啡总价"+ order.count()); System.out.println(order.getName()); // 中国咖啡+豆浆+豆浆 order=new SoyBean(order);     System.out.println("咖啡总价"+ order.count()); System.out.println(order.getName()); }}

测试截图

在这里插入图片描述
注意的点: 在装饰器类:DecoratorSeason 中两个的方法 。有说明

装饰器 挺绕的 面试经常问

IO 分析

在这里插入图片描述
在这里插入图片描述

/* * Package: com.example.demo.jdk * Type:    Io * Date:    2022-03-15 22:25 * * Copyright (c) 2022 HUANENG GUICHENG TRUST CORP.,LTD All Rights Reserved. * * You may not use this file except in compliance with the License. */package com.example.demo.jdk;import java.io.DataInputStream;import java.io.FileInputStream;/ * 功能描述: * * @author Songxianyang * @date 2022-03-15 22:25 */public class Io {    public static void main(String[] args) throws Exception { /  * FilterInputStream   装饰器  *   protected volatile InputStream in; 饮料  *DataInputStream 咖啡  */ DataInputStream dataInputStream = new DataInputStream(new FileInputStream("")); System.out.println(dataInputStream.read()); dataInputStream.close();    }}

妊娠纹产品大全