前言
裝飾者模式是套娃,橋接模式是組合。套娃是外層套娃丢了才能玩,組合是像汽車部件一樣缺一不可。
代碼說明一切,直接上代碼。
裝飾者模式
//接口ICake
public interface ICake {
void name();
}
//實現類Cake
public class Cake implements ICake {
@Override
public void name() {
System.out.println("蛋糕");
}
}
//第一個抽象裝飾者類SizeCake
public abstract class SizeCake implements ICake {
private ICake cake;
public SizeCake(ICake cake) {
this.cake = cake;
}
@Override
public void name() {
cake.name();
size();
}
public abstract void size();
}
//具體裝飾者類BigCake
public class BigCake extends SizeCake {
public BigCake(ICake cake) {
super(cake);
}
@Override
public void size() {
System.out.println("大的");
}
}
//具體裝飾者類MiddleCake
public class MiddleCake extends SizeCake {
public MiddleCake(ICake cake) {
super(cake);
}
@Override
public void size() {
System.out.println("中的");
}
}
//具體裝飾者類SmallCake
public class SmallCake extends SizeCake {
public SmallCake(ICake cake) {
super(cake);
}
@Override
public void size() {
System.out.println("小的");
}
}
//第一個抽象裝飾者類FruitCake
public abstract class FruitCake implements ICake {
private ICake cake;
public FruitCake(ICake cake) {
this.cake = cake;
}
@Override
public void name() {
cake.name();
fruit();
}
public abstract void fruit();
}
//具體裝飾者類AppleCake
public class AppleCake extends FruitCake {
public AppleCake(ICake cake) {
super(cake);
}
@Override
public void fruit() {
System.out.println("蘋果");
}
}
//具體裝飾者類PearCake
public class PearCake extends FruitCake {
public PearCake(ICake cake) {
super(cake);
}
@Override
public void fruit() {
System.out.println("梨");
}
}
//具體裝飾者類StrawberryCake
public class StrawberryCake extends FruitCake {
public StrawberryCake(ICake cake) {
super(cake);
}
@Override
public void fruit() {
System.out.println("草莓");
}
}
//客戶端測試代碼
public static void main(String[] args) {
Cake cake = new Cake();
BigCake bigCake = new BigCake(cake);
AppleCake appleCake = new AppleCake(bigCake);
appleCake.name();
}
橋接模式
//抽象父Cake
public abstract class Cake {
private IFruit fruit;
public void name() {
System.out.println("蛋糕");
size();
if (null != fruit) {
fruit.name();
}
}
public abstract void size();
public void setFruit(IFruit fruit) {
this.fruit = fruit;
}
}
//子類BigCake
public class BigCake extends Cake {
@Override
public void size() {
System.out.println("大的");
}
}
//子類MiddleCake
public class MiddleCake extends Cake {
@Override
public void size() {
System.out.println("中的");
}
}
//子類SmallCake
public class SmallCake extends Cake {
@Override
public void size() {
System.out.println("小的");
}
}
//接口IFruit
public interface IFruit {
void name();
}
//實現接口IFruit的類Apple
public class Apple implements IFruit {
@Override
public void name() {
System.out.println("蘋果");
}
}
//實現接口IFruit的類Pear
public class Pear implements IFruit {
@Override
public void name() {
System.out.println("梨");
}
}
//實現接口IFruit的類Strawberry
public class Strawberry implements IFruit {
@Override
public void name() {
System.out.println("草莓");
}
}
public static void main(String[] args) {
BigCake cake = new BigCake();
cake.setFruit(new Apple());
cake.name();
}
結果
總結
以上面代碼為例,
1、對于裝飾者模式,在蛋糕的基礎上,可以添加大小、水果等作為裝飾。這些裝飾可以加可以不加。
2、對于橋接模式,蛋糕的屬性中就有大小和水果,所以這兩者都必須有的,才能生成一個蛋糕。
顯然,橋接模式的耦合度要更大一些,它是一種組合關系。
,更多精彩资讯请关注tft每日頭條,我们将持续为您更新最新资讯!