• 桥接模式


    桥接模式:将抽象部分与它的实现部分分离,使它们都可以独立地变化。以下是例子:

     

    public class Jacket extends Clothing{

    public void personDressCloth(Person person) {

    System.out.println(person.getType()+"穿马甲"); } }

    public class Trouser extends Clothing{

    public void personDressCloth(Person person) {
    System.out.println(person.getType()+"穿裤子");}}

    public abstract class Person {

    private Clothing clothing;
    private String type;
    public Clothing getClothing(){
    return clothing;
    }
    public void setType(String type){
    this.type = type;
    }
    public String getType(){
    return this.type;
    }
    public abstract void dress();
    }

    public class Lady extends Person{  

    public Lady(){

    setType("女人"); }

    public void dress() {

    Clothing clothing = getClothing();

    clothing.personDressCloth(this); } }

    public class Man extends Person{  

    public Man(){

    setType("男人"); }

    public void dress() {

    Clothing clothing = getClothing();

    clothing.personDressCloth(this); } }

     public class Test {

    /**
    * 将抽象部分与它的实现部分分离,使它们都可以独立地变化。
    */
    public static void main(String[] args) {
    // TODO Auto-generated method stub
    Person man = new Man();
    Person lady = new Lady();
    Clothing jacket = new Jacket();
    Clothing trouser = new Trouser();
    jacket.personDressCloth(man);
    trouser.personDressCloth(man);
    jacket.personDressCloth(lady);
    trouser.personDressCloth(lady);
    }
    }
  • 相关阅读:
    我的云之旅hadoop集群(3)
    动态域名绑定
    我的云之旅hadoop集群集成Hive(4)
    Axis2实践
    PHP Apache Mysql搭建
    JavaEE程序员必读图书大推
    我的云之旅hadoop集群集成Hbase集群(5)
    本博客总排名进入前100
    关系数据库及NoSql图书大推荐
    Last_IO_Errno: 1032
  • 原文地址:https://www.cnblogs.com/lee0oo0/p/2513410.html
Copyright © 2020-2023  润新知