设计模式-9-桥接模式
来源:互联网 发布:数据分析与知识发现 编辑:程序博客网 时间:2024/06/13 22:10
桥接模式:用于把抽象化与实现化解耦,使得二者可以独立变化。
帮助理解:现有颜色(白,灰,黑)和形状(正方形,长方形,圆形),现需要将三种两两配对。
形状抽象类:
package com.structuralPattern.bridge.edition1;public abstract class Shape { Color color;//颜色抽象类,是颜色和形状配对 public void setColor(Color color) { this.color = color; } public abstract void draw();}
形状实际类:
package com.structuralPattern.bridge.edition1;public class Circle extends Shape{ public void draw() { color.bepaint("圆形"); }}
package com.structuralPattern.bridge.edition1;public class Rectangle extends Shape{ public void draw() { color.bepaint("长方形"); }}
package com.structuralPattern.bridge.edition1;public class Square extends Shape{ public void draw() { color.bepaint("正方形"); }}
颜色抽象类:
package com.structuralPattern.bridge.edition1;public interface Color { public void bepaint(String shape);}
实际颜色类
package com.structuralPattern.bridge.edition1;public class White implements Color{ public void bepaint(String shape) { System.out.println("白色的" + shape); }}
package com.structuralPattern.bridge.edition1;public class Gray implements Color{ public void bepaint(String shape) { System.out.println("灰色的" + shape); }}
package com.structuralPattern.bridge.edition1;public class Black implements Color{ public void bepaint(String shape) { System.out.println("黑色的" + shape); }}
将颜色和形状进行组合
package com.structuralPattern.bridge.edition1;/* * 将形状和颜色,进行组合 */public class Client { public static void main(String[] args) { //白色 Color white = new White(); //正方形 Shape square = new Square(); //白色的正方形 square.setColor(white); square.draw(); //长方形 Shape rectange = new Rectangle(); rectange.setColor(white); rectange.draw(); }}
阅读全文
0 0
- 设计模式-9-桥接模式
- 大话设计模式9 单例模式 桥接模式
- 设计模式----桥接模式
- 设计模式---桥接模式
- 设计模式:桥接模式
- 设计模式 桥接模式
- 设计模式 - 桥接模式
- 设计模式--桥接模式
- 设计模式--桥接模式
- 设计模式 - 桥接模式
- 设计模式-桥接模式
- 设计模式 - 桥接模式
- 设计模式 - 桥接模式
- 【设计模式-桥接模式】
- 设计模式:桥接模式
- 【设计模式】----桥接模式
- 【设计模式】桥接模式
- 设计模式--桥接模式
- (转)人工智能的钟摆
- 机器学习-统计学习三要素浅析
- jinfo
- Math && Program && Big-man
- oracle---添加空间索引
- 设计模式-9-桥接模式
- 青蛙跳台阶
- 网络的基本设定
- opencv3.2.0在vs2015,2017中的环境配置
- lua中关于取长度问题
- How to write a good design document for peer engineers (如何写一份给工程师看的技术文档)
- android NDK如何解决Please define the NDK_PROJECT_PATH variable to point to it
- 图解FTP的主动模式与被动模式以及客户端设置
- Android Things:外设I/O接口-GPIO