# 设计模式 第七章 桥接模式、装饰者模式、组合模式
前言
桥接模式,装饰者模式,组合模式
一、桥接模式
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();
}
}
---学校---
清华大学
计算机学院
软件工程
网络工程
信息工程学院
通信工程
信息工程
---学院---
计算机学院
软件工程
网络工程