1. 程式人生 > >java23中設計模式之適配器模式

java23中設計模式之適配器模式

print 鍵盤 分享 rri sys 完成 out img closed

技術分享

技術分享

技術分享

技術分享

技術分享

技術分享
package com.bjsxt.adapter;

/**
 * 被適配的類
 * (相當於例子中的,PS/2鍵盤)
 * @author Administrator
 *
 */
public class Adaptee {
    
    public void request(){
        System.out.println("可以完成客戶請求的需要的功能!");
    }
}
Adaptee 技術分享
package com.bjsxt.adapter;

/**
 * 客戶端類
 * (相當於例子中的筆記本,只有USB接口)
 * @author Administrator
 *
 
*/ public class Client { public void test1(Target t){ t.handleReq(); } public static void main(String[] args) { Client c = new Client(); Adaptee a = new Adaptee(); // Target t = new Adapter(); Target t = new Adapter2(a); c.test1(t); } }
Client 技術分享
package com.bjsxt.adapter;

public interface Target {
    void handleReq();
}
Target 技術分享
package com.bjsxt.adapter;

/**
 * 適配器 (類適配器方式)
 * (相當於usb和ps/2的轉接器)
 * @author Administrator
 *
 */
public class Adapter extends Adaptee implements Target {
    
    
    @Override
    public void handleReq() {
        
super.request(); } }
adapter 類適配器 技術分享
package com.bjsxt.adapter;

/**
 * 適配器 (對象適配器方式,使用了組合的方式跟被適配對象整合)
 * (相當於usb和ps/2的轉接器)
 * @author Administrator
 *
 */
public class Adapter2  implements Target {
    
    private Adaptee adaptee;
    
    @Override
    public void handleReq() {
        adaptee.request();
    }

    public Adapter2(Adaptee adaptee) {
        super();
        this.adaptee = adaptee;
    }
    
    
    
}
Adapter 對象適配器

java23中設計模式之適配器模式