前言

装饰者模式是套娃,桥接模式是组合。套娃是外层套娃丢了才能玩,组合是像汽车部件一样缺一不可。

代码说明一切,直接上代码。


装饰者模式

//接口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)


总结

以上面代码为例,

1、对于装饰者模式,在蛋糕的基础上,可以添加大小、水果等作为装饰。这些装饰可以加可以不加。

2、对于桥接模式,蛋糕的属性中就有大小和水果,所以这两者都必须有的,才能生成一个蛋糕。

显然,桥接模式的耦合度要更大一些,它是一种组合关系。

,