装饰模式
来源:互联网 发布:淘宝达人注册 编辑:程序博客网 时间:2024/06/10 07:51
一、桥梁模式说明:
1. 桥梁模式是面向对象思想的一种重要体现,主要应用于减少类之间的继承关系,使类和类之间尽量采用聚合关系,这样提高了代码的可维护性和重用性。
2. 举例说明:飞机从功能角度可以分为客运机和货运机,从制造商的角度可以分为波音和空客等等。如果完全采用继承关系设计会使类之间的关系变的复杂,不易于维护。桥梁模式正好可以用户处理这一类的问题。设计结构图可以如下所示:
二、Java语言描述桥梁模式:
package com.solid.pattern;
/**
* 桥梁模式
* @author solid
*/
public abstract class AirPlane {
public MakerPlane makerPlane;
public AirPlane(MakerPlane makerPlane) {
this.makerPlane = makerPlane;
}
public abstract void fly();
public void operation() {
makerPlane.make();
}
}
package com.solid.pattern;
/**
* 桥梁模式(修正类)
* @author solid
*/
public class GuestPlane extends AirPlane {
public GuestPlane(MakerPlane makerPlane) {
super(makerPlane);
}
public void fly() {
System.out.println("===GuestPlane===");
}
}
package com.solid.pattern;
/**
* 桥梁模式(修正类)
* @author solid
*/
public class BagPlane extends AirPlane {
public BagPlane(MakerPlane makerPlane) {
super(makerPlane);
}
public void fly() {
System.out.println("===BagPlane===");
}
}
package com.solid.pattern;
/**
* 桥梁模式
* @author solid
*/
public abstract class MakerPlane {
public abstract void make();
}
package com.solid.pattern;
/**
* 桥梁模式(实现类)
* @author solid
*/
public class KongkeMaker extends MakerPlane {
public void make() {
System.out.println("===KongkeMaker===");
}
}
package com.solid.pattern;
/**
* 桥梁模式(实现类)
* @author solid
*/
public class BoYin extends MakerPlane {
public void make() {
System.out.println("===BoYinMaker===");
}
}
package com.solid.pattern;
/**
* 桥梁模式测试
* @author solid
*/
public class TestBridge {
public static void main(String[] args) {
MakerPlane plane = new BoYin();
AirPlane airplane = new GuestPlane(plane);
airplane.fly();
}
}
- 装饰模式-撤销装饰
- 装饰模式
- 装饰模式
- 装饰模式
- 装饰模式
- 装饰模式
- 装饰模式
- 装饰模式
- 装饰模式
- 装饰模式
- 装饰模式
- 装饰模式
- 装饰模式
- 装饰模式
- 装饰模式
- 装饰模式
- 装饰模式
- 装饰模式
- Introduction to the Direct Rendering Infrastructure
- 深入浅出Linux字符设备驱动程序解析(转自http___linux_chinaitlab_com_driver_720255_html)
- 权威数据:百度9月后最新算法转SEOwhy
- Linux网卡驱动分析
- 昨天去听了IBM的宣讲会
- 装饰模式
- Linux系统管理实践(2):OpenSSH远程管理配置
- 支持曾轶可!支持春哥!
- Ubuntu Linux下搜索文件和文件内容
- linux查看目录的大小(转)
- 常用环境的配置
- Linux文件内容查询命令(转)
- Ubuntu设置环境变量三法
- Linux下制作软盘和光盘镜像(转)