Java之Builder模式(并用OC实现了这种模式)
本人在学习Java,直接先学习Netty框架,因为Netty框架是业界最流行的NIO框架之一,在学习的过程中,了解到Netty服务端启动需要先创建服务器启动辅助类ServerBootstrap,它提供了一系列的方法用于设置服务器端启动相关的参数。然而在创建ServerBootstrap实例时,发现ServerBootstrap只有一个无参的构造函数,事实上它需要与多个其它组件或者类交互。ServerBootstrap构造函数没有参数的原因是因为它的参数太多了,而且未来也可能会发生变化。Netty创造者为了解决这个问题,就引入了Builder模式。
学习到这里,在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.h
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.m
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类中创建了。
虽然也是可以动态传入参数,但是OC的语言风格却不好看。但是用Swift可以,毕竟Swift也模仿了java的class类。