# 設計模式 第七章 橋接模式、裝飾者模式、組合模式
前言
橋接模式,裝飾者模式,組合模式
一、橋接模式
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();
}
}
---學校---
清華大學
計算機學院
軟體工程
網路工程
資訊工程學院
通訊工程
資訊工程
---學院---
計算機學院
軟體工程
網路工程