设计模式 第七章 桥接模式、装饰者模式、组合模式

# 设计模式 第七章 桥接模式、装饰者模式、组合模式

前言

	桥接模式,装饰者模式,组合模式

一、桥接模式

1.介绍

桥接模式,结构型模式,将实现与抽象放在二个不同的
类层次中,使二个层次可以独立改变。
类似手机有品牌和样式,需要二者相互独立。

2.代码示例


//接口
public interface Brand {
    void open();
    void close();
}

//接口的实现
public class XiaoMi implements Brand{
    @Override
    public void open() {
        System.out.println("小米开机");
    }

    @Override
    public void close() {
        System.out.println("小米关机");
    }
}
//实现中成员变量含有抽象的接口
public abstract class Phone {

    private Brand brand;
    public Phone(Brand brand){
        super();
        this.brand = brand;
    }
    protected void open() {
        this.brand.open();
    }
    protected void close() {
        this.brand.close();
    }
}
//具体的实现类
public class FoldePhone extends Phone {
    public FoldePhone(Brand brand) {
        super(brand);
    }
    public void open() {
        super.open();
        System.out.println("折叠手机");
    }
    protected void close() {
        super.close();
        System.out.println("折叠手机");
    }
}

public class client {
    public static void main(String[] args) {
        Phone foldePhone = new FoldePhone(new XiaoMi());
        foldePhone.open();
        foldePhone.close();

    }
}

//结果:
小米开机
折叠手机
小米关机
折叠手机


二、装饰者模式

1.介绍

结构模式,动态 将新功能附加到对象上,在对象功能
扩展方面他比继承更有弹性,体现开闭原则。
类似星巴克的咖啡需要加糖,再加巧克力等结构

2.代码示例


//抽象类,被装饰者
public abstract class InputStream implements Closeable {
    public abstract int read() throws IOException;
    
}

//被装饰者的子类
public class FileInputStream extends InputStream
}

//被装饰者类的实现,成员变量含有被装饰者
public class FilterInputStream extends InputStream {
    protected volatile InputStream in;
	public int read() throws IOException 		{
        return in.read();
    }
}

//具体的修饰者,
public class DataInputStream extends FilterInputStream implements DataInput {

    public DataInputStream(InputStream in) {
        super(in);
    }
 public final int read(byte b[]) throws IOException {
        return in.read(b, 0, b.length);
    }
}

三、组合模式

1.介绍

又称部分整体模式,结构型模式,它创建了对象组的树形结构,
将对象组合成树状结构以表示整体部分的层次关系。
类似学校-学院-专业结构。

2.代码示例

//抽象类,管理子部件
public abstract class Organizationcomponent {
    private String name;
    private String des;
    protected void add(Organizationcomponent organizationcomponent) {
        throw new UnsupportedOperationException();
    }
    protected void remove(Organizationcomponent organizationcomponent){
        throw new UnsupportedOperationException();
    }
    public Organizationcomponent(String name, String des) {
        this.name = name;
        this.des = des;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getDes() {
        return des;
    }
    public void setDes(String des) {
        this.des = des;
    }
    protected abstract void print();
}
//非叶子节点,存储子部件
public class College extends Organizationcomponent {
    ArrayList<Organizationcomponent> organizationcomponents = new ArrayList<Organizationcomponent>();
    public College(String name, String des) {
        super(name, des);
    }
    @Override
    protected void add(Organizationcomponent organizationcomponent) {
        organizationcomponents.add(organizationcomponent);
    }
    @Override
    protected void remove(Organizationcomponent organizationcomponent) {
        organizationcomponents.remove(organizationcomponent);
    }
    @Override
    protected void print() {
        System.out.println(getName());
        for (Organizationcomponent organizationcomponent: organizationcomponents){
            organizationcomponent.print();
        }
    }
}

//非叶子节点,存储子部件
public class University extends Organizationcomponent {

    ArrayList<Organizationcomponent> organizationcomponents = new ArrayList<Organizationcomponent>();
    public University(String name, String des) {
        super(name, des);
    }
    @Override
    protected void add(Organizationcomponent organizationcomponent) {
        organizationcomponents.add(organizationcomponent);
    }
    @Override
    protected void remove(Organizationcomponent organizationcomponent) {
        organizationcomponents.remove(organizationcomponent);
    }
    @Override
    protected void print() {
        System.out.println(getName());
        for (Organizationcomponent organizationcomponent: organizationcomponents){
            organizationcomponent.print();
        }
    }
}

//叶子节点,没有子节点
public class Department extends Organizationcomponent {
    public Department(String name, String des) {
        super(name, des);
    }

    @Override
    public String getName() {
        return super.getName();
    }
    @Override
    public String getDes() {
        return super.getDes();
    }
    @Override
    protected void print() {
        System.out.println(getName());
    }
}

public class Client {

    public static void main(String[] args) {
        Organizationcomponent university = new University("清华大学", "顶级大学");
        Organizationcomponent college = new College("计算机学院", "计算机学院");
        Organizationcomponent college2 = new College("信息工程学院", "信息工程学院");

        college.add(new Department("软件工程","软件工程"));
        college.add(new Department("网络工程","网络工程"));

        college2.add(new Department("通信工程","通信工程"));
        college2.add(new Department("信息工程","信息工程"));

        university.add(college);
        university.add(college2);

        System.out.println("---学校---");
        university.print();
        System.out.println("---学院---");
        college.print();
    }
}


//结果
---学校---
清华大学
计算机学院
软件工程
网络工程
信息工程学院
通信工程
信息工程
---学院---
计算机学院
软件工程
网络工程

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值