zoukankan      html  css  js  c++  java
  • 桥接模式

    设计模式的意义在于:面向业务内容、业务数据结构和系统架构,高内聚低耦合、优雅的将平面逻辑立体化。

     1 package designPattern;
     2 /**
     3  * 桥接模式
     4  * @author Administrator
     5  */
     6 public class A6_BridgeTest {
     7 
     8     /**
     9      * 将抽象部分与他的实现部分分离,使他们都可以独立的变化
    10      * 适用性:
    11      * 1,你不希望在抽象和他的实现之间有一个搞定的绑定关系.
    12      * 2,类的抽象以及他的实现都应该可以通过生成子类的方法加以补充
    13      * 这时Bridge模式使你可以对不同的抽象接口和实现部分进行组合,并分别对他们进行补充
    14      * 3,对一个抽象的实现部分的修改,应对客户不产生影响,即客户的代码不必重新编译
    15      * 4,这样需要生成许多类,因为要讲对象分成2个部分
    16      * 5,你想在多个对象间共享实现(可能使用引用计数),但同时要求客户并不知道这一点
    17      */
    18     public static void main(String[] args) {
    19         PersonBridge man=new ManBridge();
    20         PersonBridge woman=new WomanBridge();
    21         
    22         Clothing manClothing=new Jacket();
    23         Clothing womanClothing=new Trouser();
    24         
    25         manClothing.personDressCloth(man);
    26         womanClothing.personDressCloth(woman);
    27         
    28         manClothing.personDressCloth(woman);
    29         womanClothing.personDressCloth(man);
    30     }
    31 }
    32 
    33 //1,abstract 定义抽象类的接口,维护一个指向Implementor类型对象的指针
    34 abstract class PersonBridge
    35 {
    36     private Clothing clothing;
    37     private String type;
    38     
    39     public Clothing getClothing() {
    40         return clothing;
    41     }
    42     public void setClothing(Clothing clothing) {
    43         this.clothing = clothing;
    44     }
    45     public String getType() {
    46         return type;
    47     }
    48     public void setType(String type) {
    49         this.type = type;
    50     }
    51     public abstract void dress();
    52 }
    53 //2,refinedAbstract 扩充由abstract定义的接口
    54 class ManBridge extends PersonBridge
    55 {
    56     public ManBridge()
    57     {
    58         setType("男人");
    59     }
    60     public void dress() {
    61         Clothing clothing=getClothing();
    62         clothing.personDressCloth(this);
    63     }
    64 }
    65 class WomanBridge extends PersonBridge
    66 {
    67     public WomanBridge()
    68     {
    69         setType("女人");
    70     }
    71     public void dress() {
    72         Clothing clothing=getClothing();
    73         clothing.personDressCloth(this);
    74     }    
    75 }
    76 //3,Impementor 定义实现类的操作,该接口不一定要与abstract接口完全一致,甚至完全可以不同
    77 //一般来说Impementor接口仅提供基本操作,而abstract则定义了基于这些基本操作的较高层次的操作
    78 abstract class Clothing
    79 {
    80     public abstract void personDressCloth(PersonBridge p);
    81 }
    82 //ConcreteImplementor 实现Implementor接口
    83 class Jacket extends Clothing
    84 {
    85     public void personDressCloth(PersonBridge p) {
    86         System.out.println(p.getType()+"穿马甲...");
    87     }    
    88 }
    89 class Trouser extends Clothing
    90 {
    91     public void personDressCloth(PersonBridge p) {
    92         System.out.println(p.getType()+"穿裙子...");
    93     }    
    94 }

    环境:JDK1.6,MAVEN,tomcat,eclipse

    源码地址:http://files.cnblogs.com/files/xiluhua/designPattern.rar

    欢迎亲们评论指教。

  • 相关阅读:
    多线程实践
    sql你server,mysql,oracle的分页语句
    BS与CS的联系与区别
    EJB与JAVA BEAN的区别
    Struts2.0 xml文件的配置(package,namespace,action)
    Q 51~60
    Q 41~50
    列表推导式
    Q 31~40
    Q 21~30
  • 原文地址:https://www.cnblogs.com/xiluhua/p/4413718.html
Copyright © 2011-2022 走看看