【設計模式】6、適配器模式

来源:http://www.cnblogs.com/cutter-point/archive/2016/02/17/5196246.html
-Advertisement-
Play Games

抽象類 1 package com.shejimoshi.structural.Adapter; 2 3 4 /** 5 * 功能:適配器模式 6 * 將一個類的介面轉換成客戶希望的另外一個介面。adapter模式使得原本由於介面不相容而不能一起工作的那些類可以一起工作 7 * 適用性:你想使用一個


 

抽象類

 1 package com.shejimoshi.structural.Adapter;
 2 
 3 
 4 /**
 5  * 功能:適配器模式
 6  *         將一個類的介面轉換成客戶希望的另外一個介面。adapter模式使得原本由於介面不相容而不能一起工作的那些類可以一起工作
 7  * 適用性:你想使用一個已經存在的類,而它的介面不符合你的需求
 8  *          你想創建一個可以復用的類,該類可以與其他不相關的類或不可預見的類協同工作
 9  *         你想使用一些已經存在的子類,但是不可能對每一個都進行子類化以匹配它們的介面。對象適配器可以適配它的類介面
10  * 時間:2016年2月17日下午5:43:46
11  * 作者:cutter_point
12  */
13 public abstract class Player
14 {
15     protected String name;    //運動員名字
16     
17     public Player(String name)
18     {
19         this.name = name;
20     }
21     
22     //進攻
23     public abstract void attack();
24     //防守
25     public abstract void defense();
26 }

 

前鋒

 1 package com.shejimoshi.structural.Adapter;
 2 
 3 
 4 /**
 5  * 功能:這個是我們的前鋒類,這個我們不需要適配
 6  * 時間:2016年2月17日下午5:50:01
 7  * 作者:cutter_point
 8  */
 9 public class Forwards extends Player
10 {
11 
12     public Forwards(String name)
13     {
14         super(name);
15     }
16 
17     @Override
18     public void attack()
19     {
20         System.out.println("前鋒" + name + "進攻");
21     }
22 
23     @Override
24     public void defense()
25     {
26         System.out.println("前鋒" + name + "防守");
27     }
28 
29 }

 

中鋒

 1 package com.shejimoshi.structural.Adapter;
 2 
 3 
 4 /**
 5  * 功能:我們的中鋒類
 6  * 時間:2016年2月17日下午5:54:50
 7  * 作者:cutter_point
 8  */
 9 public class Center extends Player
10 {
11 
12     public Center(String name)
13     {
14         super(name);
15     }
16 
17     @Override
18     public void attack()
19     {
20         System.out.println("中鋒" + name + "進攻");
21     }
22 
23     @Override
24     public void defense()
25     {
26         System.out.println("中鋒" + name + "防守");
27     }
28 
29 }

 

 

 

後衛

 1 package com.shejimoshi.structural.Adapter;
 2 
 3 
 4 /**
 5  * 功能:後衛
 6  * 時間:2016年2月17日下午5:56:12
 7  * 作者:cutter_point
 8  */
 9 public class Guards extends Player
10 {
11 
12     public Guards(String name)
13     {
14         super(name);
15     }
16 
17     @Override
18     public void attack()
19     {
20         System.out.println("後衛" + name + "進攻");
21     }
22 
23     @Override
24     public void defense()
25     {
26         System.out.println("後衛" + name + "防守");
27     }
28 
29 }

 

翻譯者,我們的適配器類

 1 package com.shejimoshi.structural.Adapter;
 2 
 3 
 4 /**
 5  * 功能:翻譯者,為了適配外籍中鋒
 6  * 時間:2016年2月17日下午6:00:36
 7  * 作者:cutter_point
 8  */
 9 public class Translator extends Player
10 {
11     
12     private ForeignCenter fc = new ForeignCenter();
13     
14     public Translator(String name)
15     {
16         super(name);
17         fc.setName(name);
18     }
19 
20     @Override
21     public void attack()
22     {
23         fc.ForeignAttack();
24     }
25 
26     @Override
27     public void defense()
28     {
29         fc.foreignDefense();
30     }
31 
32 }

 

被適配類,外籍中鋒

 1 package com.shejimoshi.structural.Adapter;
 2 
 3 
 4 /**
 5  * 功能:外籍中鋒
 6  * 時間:2016年2月17日下午5:57:09
 7  * 作者:cutter_point
 8  */
 9 public class ForeignCenter
10 {
11     private String name;
12     
13     //預設構造函數
14     public ForeignCenter()
15     {}
16     
17     public ForeignCenter(String name)
18     {
19         this.name = name;
20     }
21     
22     //進攻
23     public void ForeignAttack()
24     {
25         System.out.println("foreignCenter" + name + "attack !!");
26     }
27     
28     //防守
29     public void foreignDefense()
30     {
31         System.out.println("foreignCenter" + name + "defense !!");
32     }
33 
34     public String getName()
35     {
36         return name;
37     }
38 
39     public void setName(String name)
40     {
41         this.name = name;
42     }
43     
44 }

 

測試代碼:

 1 package com.shejimoshi.structural.Adapter;
 2 
 3 
 4 /**
 5  * 功能:適配器模式的使用
 6  * 時間:2016年2月17日下午6:03:03
 7  * 作者:cutter_point
 8  */
 9 public class Test
10 {
11     public static void main(String[] args)
12     {
13         Player z = new Forwards("周星馳");
14         z.attack();
15         
16         Player y = new Center("姚明");
17         y.attack();
18         
19         //使用翻譯者作為適配器使用外籍中鋒類
20         Player cp = new Translator("cutter_point");
21         
22         cp.attack();
23         cp.defense();
24         
25     }
26 }

顯示結果:

1 前鋒周星馳進攻
2 中鋒姚明進攻
3 foreignCentercutter_pointattack !!
4 foreignCentercutter_pointdefense !!

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 註:本文主要記錄自《深入分析java web技術內幕》"第四章 javac編譯原理" 1、javac作用 將*.java源代碼文件轉化為*.class文件 2、編譯流程 流程: 詞法分析器:將源碼轉換為Token流 將源代碼劃分成一個個Token(Token包含的元素類型看3.2) 語法分析器:將T
  • IO是輸入和輸出的簡稱,在實際的使用時,輸入和輸出是有方向的。就像現實中兩個人之間借錢一樣,例如A借錢給B,相對於A來說是借出,而相對於B來說則是借入。所以在程式中提到輸入和輸出時,也需要區分清楚是相對的內容。 在 程式中,輸入和輸出都是相對於當前程式而言的,例如從硬碟上讀取一個配置文件的內容到程式
  • 一、SSH框架的概念 SSH框架是java web開發流行的一個開源集合框架,SSH框架包含了Struts框架,Spring框架和Hibernate框架。SSH框架可以用於構建靈活、易於擴展的多層Web應用程式。 二、SSH框架的工作原理 SSH框架中的Struts負責攔截用戶請求,正常情況下用戶請
  • 1、路由是程式的方法和URL的一一映射。 在配置文件里,把經常訪問的路由放在前面,可以提高路由匹配的效率。 2、路由匹配的兩種方式 Annotation 允許在方法的上面用註釋定義方法運行狀態的功能 class UserController extends Controller{ /** * @Ro
  • lazy概念:要用到的時候,再去載入,對於關聯的集合來說,只有當訪問到的時候,才去載入它所關聯的集合,比如一個user對應很多許可權,只有當user.getRights()的時候,才發出select right的語句,在訪問到rights之前,rights是一個PersisitSet對於實體類來說,只
  • 1 <?php 2 // 正確地顯示覆數 3 if(!function_exists('_plurals_format')) 4 { 5 /** 6 * 正確的使用複數 7 * @access public 8 * @author zhaoyingnan 2016-02-17 11:53 9 * @
  • pyextend - python extend lib accepts(exception=TypeError, **types) 參數: exception: 檢查失敗時的拋出異常類型 **types: 待檢查的k-v參數 **types參數支持 a=int : 待測函數參數a必須為 int 類
一周排行
    -Advertisement-
    Play Games
  • 前言 本文介紹一款使用 C# 與 WPF 開發的音頻播放器,其界面簡潔大方,操作體驗流暢。該播放器支持多種音頻格式(如 MP4、WMA、OGG、FLAC 等),並具備標記、實時歌詞顯示等功能。 另外,還支持換膚及多語言(中英文)切換。核心音頻處理採用 FFmpeg 組件,獲得了廣泛認可,目前 Git ...
  • OAuth2.0授權驗證-gitee授權碼模式 本文主要介紹如何筆者自己是如何使用gitee提供的OAuth2.0協議完成授權驗證並登錄到自己的系統,完整模式如圖 1、創建應用 打開gitee個人中心->第三方應用->創建應用 創建應用後在我的應用界面,查看已創建應用的Client ID和Clien ...
  • 解決了這個問題:《winForm下,fastReport.net 從.net framework 升級到.net5遇到的錯誤“Operation is not supported on this platform.”》 本文內容轉載自:https://www.fcnsoft.com/Home/Sho ...
  • 國內文章 WPF 從裸 Win 32 的 WM_Pointer 消息獲取觸摸點繪製筆跡 https://www.cnblogs.com/lindexi/p/18390983 本文將告訴大家如何在 WPF 裡面,接收裸 Win 32 的 WM_Pointer 消息,從消息裡面獲取觸摸點信息,使用觸摸點 ...
  • 前言 給大家推薦一個專為新零售快消行業打造了一套高效的進銷存管理系統。 系統不僅具備強大的庫存管理功能,還集成了高性能的輕量級 POS 解決方案,確保頁面載入速度極快,提供良好的用戶體驗。 項目介紹 Dorisoy.POS 是一款基於 .NET 7 和 Angular 4 開發的新零售快消進銷存管理 ...
  • ABP CLI常用的代碼分享 一、確保環境配置正確 安裝.NET CLI: ABP CLI是基於.NET Core或.NET 5/6/7等更高版本構建的,因此首先需要在你的開發環境中安裝.NET CLI。這可以通過訪問Microsoft官網下載並安裝相應版本的.NET SDK來實現。 安裝ABP ...
  • 問題 問題是這樣的:第三方的webapi,需要先調用登陸介面獲取Cookie,訪問其它介面時攜帶Cookie信息。 但使用HttpClient類調用登陸介面,返回的Headers中沒有找到Cookie信息。 分析 首先,使用Postman測試該登陸介面,正常返回Cookie信息,說明是HttpCli ...
  • 國內文章 關於.NET在中國為什麼工資低的分析 https://www.cnblogs.com/thinkingmore/p/18406244 .NET在中國開發者的薪資偏低,主要因市場需求、技術棧選擇和企業文化等因素所致。歷史上,.NET曾因微軟的閉源策略發展受限,儘管後來推出了跨平臺的.NET ...
  • 在WPF開發應用中,動畫不僅可以引起用戶的註意與興趣,而且還使軟體更加便於使用。前面幾篇文章講解了畫筆(Brush),形狀(Shape),幾何圖形(Geometry),變換(Transform)等相關內容,今天繼續講解動畫相關內容和知識點,僅供學習分享使用,如有不足之處,還請指正。 ...
  • 什麼是委托? 委托可以說是把一個方法代入另一個方法執行,相當於指向函數的指針;事件就相當於保存委托的數組; 1.實例化委托的方式: 方式1:通過new創建實例: public delegate void ShowDelegate(); 或者 public delegate string ShowDe ...