iOS設計模式 介面卡

2021-09-07 01:27:11 字數 3745 閱讀 6058

ios設計模式 - 介面卡

1. 為了讓客戶端盡可能的通用,我們使用介面卡模式來隔離客戶端與外部引數的聯絡,只讓客戶端與介面卡通訊.

2. 本教程實現了介面卡模式的類介面卡與物件介面卡兩種模式,各有優缺點.

3. 如果對物件導向基本原理以及設計模式基本原理不熟悉,本教程會變得難以理解.原始碼

* 名字 */

@property (nonatomic, strong) nsstring *name;/**

* 線條顏色 */

@property (nonatomic, strong) uicolor *linecolor;/**

* **號碼 */

@property (nonatomic, strong) nsstring *phonenumber;/**

* 載入資料(實現了businesscardadapterprotocol協議的資料)

* * @param data 實現了businesscardadapterprotocol協議的資料 */

- (void)loaddata:(id

)data;

@end

//

//adapter

////

created by youxianming on 15/7/25.

////

#import

"businesscardview.h

"@inte***ce

businesscardview ()

@property (nonatomic, strong) uilabel *namelabel;

@property (nonatomic, strong) uiview *lineview;

@property (nonatomic, strong) uilabel *phonenumberlabel;

@end

@implementation

businesscardview

#pragma mark - 初始化

-(instancetype)initwithframe:(cgrect)frame

return

self;

}- (void

)setup

- (void)loaddata:(id

)data

#pragma mark - 重寫setter,getter方法

@synthesize name =_name;

@synthesize linecolor =_linecolor;

@synthesize phonenumber =_phonenumber;

- (void)setname:(nsstring *)name

- (nsstring *)name

- (void)setlinecolor:(uicolor *)linecolor

- (uicolor *)linecolor

- (void)setphonenumber:(nsstring *)phonenumber

- (nsstring *)phonenumber

@end

//

//normalproblem

////

created by youxianming on 15/7/25.

////

#import

#import

#import

"businesscardadapterprotocol.h

"@inte***ce businesscardadapter : nsobject /*

* * 輸入物件 */

@property (nonatomic, weak)

iddata;/**

* 與輸入物件建立聯絡

* * @param data 輸入的物件

* * @return 例項物件 */

- (instancetype)initwithdata:(id

)data;

@end

//

//normalproblem

////

created by youxianming on 15/7/25.

////

#import

"businesscardadapter.h

"@implementation

businesscardadapter

- (instancetype)initwithdata:(id

)data

return

self;

}- (nsstring *)name

- (uicolor *)linecolor

- (nsstring *)phonenumber

@end

//

//normalproblem

////

created by youxianming on 15/7/25.

////

#import

@protocol businesscardadapterprotocol - (nsstring *)name;

- (uicolor *)linecolor;

- (nsstring *)phonenumber;

@end

分析

這是基於businesscardview構建出來的必不可少的抽象介面卡以及乙個協議,通過繼承抽象介面卡來實現具體的介面卡,協議是用來統一介面.

物件介面卡與類介面卡.

客戶端(businesscardview)只與介面卡進行通訊,它不關心資料來源 normalmodel 與 specialmodel 的業務邏輯

此處的抽象是核心所在

iOS設計模式 介面卡模式

2016年系統的學習了ios的設計模式,在整理文件時發現有幾篇總結因為當時事情耽誤沒有發出,近幾天會逐漸發布,藉此在複習一下ios的設計模式。相信在以後的工作中會用到。下面補上介面卡模式的內容 1.介紹 介面卡模式主要應用於 希望復用一些現存的類,但是介面又與復用環境要求不一致的情況 在遺留 復用 ...

設計模式 介面卡模式 類介面卡 物件介面卡

乙個小例子,便於理解,上 這是我們造的。現在想用這個方法。public class adaptee 類介面卡。對我們想要的方法封裝一下,target就能像之前一樣,呼叫request方法即可。public class adapter1 extends adaptee implements targe...

設計模式 介面卡模式(類介面卡和物件介面卡)

如果去歐洲國家去旅遊的話,他們的插座如下圖最左邊,是歐洲標準。而我們使用的插頭如下圖最右邊的。因此我們的膝上型電腦,手機在當地不能直接充電。所以就需要乙個插座轉換器,轉換器第1面插入當地的插座,第2麵供我們充電,這樣使得我們的插頭在當地能使用。生活中這樣的例子很多,手機充電器 將220v轉換為5v的...