Adapter (适配器模式)
生活随笔
收集整理的這篇文章主要介紹了
Adapter (适配器模式)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
適配器模式:
將一個類的接口轉換成客戶希望的另外一個接口。Adapter模式使得原本由于接口不兼容而不能一起工作的那些類可以一起工作。
?
有兩種適配器模式:
1)類適配器 (通過繼承的方式)
2)對象適配器 (采取對象組合的模式)
?
-------------------------- 類適配器 -----------------------------
Target.java
package com.adapter ; public interface Target { public void method() ; }
?
被適配器類
Adaptee.java
package com.adapter ; public class Adaptee { public void method2() { System.out.println("Adapter-->method2()") ; } }?
適配器類
Adapter.java
?
Client.java
package com.adapter ; public class Client { public static void main(String[] args) { Target t = new Adapter() ; t.method() ; } }
?
-------------------------- 對象適配器 -----------------------------
?
Target.java
package com.adapter ; public interface Target { public void method() ; }
?
被適配器類
Adaptee.java
package com.adapter ; public class Adaptee { public void method2() { System.out.println("Adapter-->method2()") ; } }?
適配器類
Adapter.java
?
Client.java
?
package com.adapter ; public class Client { public static void main(String[] args) { Adaptee adaptee = new Adaptee() ; Target t = new Adapter(adaptee) ; t.method() ; } }
?
?
?
?
?
?
?
總結
以上是生活随笔為你收集整理的Adapter (适配器模式)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 打开适配器并捕获数据包
- 下一篇: Linux 初级常用指令