Java之Builder模式(並用OC實現了這種模式)

本人在學習Java,直接先學習Netty框架,由於Netty框架是業界最流行的NIO框架之一,在學習的過程當中,瞭解到Netty服務端啓動須要先建立服務器啓動輔助類ServerBootstrap,它提供了一系列的方法用於設置服務器端啓動相關的參數。然而在建立ServerBootstrap實例時,發現ServerBootstrap只有一個無參的構造函數,事實上它須要與多個其它組件或者類交互。ServerBootstrap構造函數沒有參數的緣由是由於它的參數太多了,並且將來也可能會發生變化。Netty創造者爲了解決這個問題,就引入了Builder模式。java

 

學習到這裏,在IOS開發中,我雖然也研習過23中經常使用的設計模式,可是我卻從沒聽過的Builder模式,因而就開始了探索Builder模式,並嘗試用OC語言去模仿實現這種模式。說不定之後在iOS開發中會用到呢。設計模式

 

Java的實例源碼:服務器

 1 public class DoDoContact {
 2 
 3     private final int    age;
 4     private final int    safeID;
 5     private final String name;
 6     private final String address;
 7  
 8     public int getAge() {
 9         return age;
10     }
11  
12     public int getSafeID() {
13         return safeID;
14     }
15  
16     public String getName() {
17         return name;
18     }
19  
20     public String getAddress() {
21         return address;
22     }
23  
24     public static class Builder {
25         private int    age     = 0;
26         private int    safeID  = 0;
27         private String name    = null;
28         private String address = null;
29         
30         public Builder(String name) {
31             this.name = name;
32         }
33  
34         public Builder age(int val) {
35             age = val;
36             return this;
37         }
38  
39         public Builder safeID(int val) {
40             safeID = val;
41             return this;
42         }
43  
44         public Builder address(String val) {
45             address = val;
46             return this;
47         }
48  
49         public DoDoContact build() {
50             return new DoDoContact(this);
51         }
52     }
53  
54     private DoDoContact(Builder b) {
55         age = b.age;
56         safeID = b.safeID;
57         name = b.name;
58         address = b.address;
59  
60     }
61 }

java的main函數框架

1 public class Test2{
2     public static void main(String[] args) {
3         DoDoContact ddc = new DoDoContact.Builder("Ace").age(10)
4                 .address("beijing").build();
5         System.out.println("name=" + ddc.getName() + "age =" + ddc.getAge()
6                 + "address" + ddc.getAddress());
7     }
8 }

有木有發如今main函數中,建立一個對象,卻能夠動態的傳入參數。函數

下面使用OC來實現:學習

DoDoContact.hui

 1 #import <Foundation/Foundation.h>
 2 
 3 @class Builder;
 4 
 5 @interface DoDoContact : NSObject
 6 
 7 @property (nonatomic,assign) NSInteger age;
 8 @property (nonatomic,assign) NSInteger safeID;
 9 @property (nonatomic,strong) NSString *name;
10 @property (nonatomic,strong) NSString *address;
11 
12 + (Builder*)getBulder;
13 
14 @end

DoDoContact.mthis

 1 #import "DoDoContact.h"
 2 
 3 
 4 @interface Builder ()
 5 
 6 @property (nonatomic,assign) NSInteger age;
 7 @property (nonatomic,assign) NSInteger safeID;
 8 @property (nonatomic,strong) NSString *name;
 9 @property (nonatomic,strong) NSString *address;
10 
11 @end
12 
13 
14 @interface DoDoContact ()
15 
16 
17 @end
18 
19 @implementation DoDoContact
20 
21 - (instancetype)initWith:(Builder*)builder
22 {
23     self = [super init];
24     if (self) {
25         self.age = builder.age;
26         self.safeID = builder.safeID;
27         self.name = builder.name;
28         self.address = builder.address;
29     }
30     return self;
31 }
32 
33 + (instancetype)getDoDoContactWith:(Builder*)builder{
34     DoDoContact *doDoContact = [[DoDoContact alloc] initWith:builder];
35     return doDoContact;
36 }
37 
38 + (Builder*)getBulder{
39     return [Builder new];
40 }
41 
42 @end
43 
44 
45 
46 @implementation Builder
47 
48 - (instancetype)init
49 {
50     self = [super init];
51     if (self) {
52         _age = 0;
53         _safeID = 0;
54         _name = NULL;
55         _address = NULL;
56     }
57     return self;
58 }
59 
60 - (Builder*)age:(NSInteger)age{
61     self.age = age;
62     return self;
63 }
64 
65 - (Builder*)safeID:(NSInteger)safeID{
66     self.safeID = safeID;
67     return self;
68 }
69 
70 - (Builder*)name:(NSString*)name{
71     self.name = name;
72     return self;
73 }
74 - (Builder*)address:(NSString*)address{
75     self.address = address;
76     return self;
77 }
78 
79 - (DoDoContact*)getDoDoContact{
80     return [DoDoContact getDoDoContactWith:self];
81 }
82 
83 
84 @end

再來看建立DoDoContact對象的方式,本人這裏無心中用的是IOS工程因此直接在ViewController類中建立了。atom

雖然也是能夠動態傳入參數,可是OC的語言風格卻很差看。可是用Swift能夠,畢竟Swift也模仿了java的class類。spa

相關文章
相關標籤/搜索