我在我的信标扫描模块中引入了“<strong>Factory Pattern”。我提到了 http://crosbymichael.com/objective-c-design-patterns-factory.html
在我的工厂类中,两种信标模式在接口类“<strong>PCGoogleBeacon.h”和“<strong>PCAppleBeacon.h”之间切换。
//工厂的头文件
typedef enum beaconMode {
iBeacon,
Eddystone
} BeaconMode;
@interface PCBeaconFinder : NSObject
+(id) searchForBeaconMode:(BeaconMode) beaconMode;
@end
//工厂的实现
+(id) searchForBeaconMode:(BeaconMode) beaconMode
{
switch (beaconMode ) {
case iBeacon:
return [PCAppleBeacon new];
break;
case Eddystone:
return [PCGoogleBeacon new];
break;
default: NSLog(@"UNKOWN BEACON MODE");
}
}
在我的接口类的实现文件中。
//Header file
@protocol PCGetBeacon <NSObject>
-(void) scanBeaconsWithUUID:(NSString *) beaconId;
@end
//在实现文件中。— 模式一的实施
#import "PCAppleBeacon.h"
@implementation PCAppleBeacon
-(void) scanBeaconsWithUUID:(NSString *) beaconId {
self.proximityContentManager = [[ProximityContentManager alloc]
initWithBeaconIDs:@[
[[BeaconID alloc] initWithUUIDString:beaconId major:0 minor:0]
]
beaconContentFactory:[EstimoteCloudBeaconDetailsFactory new]];
self.proximityContentManager.delegate = self;
[self.proximityContentManager startContentUpdates];
NSLog(@"----------- > iBeacon Implementation Called ");
}
//iBeacon Delegates goes here …
@end
// 在同一个文件中——模式2的实现
#import "PCGoogleBeacon.h"
@implementation PCGoogleBeacon
-(void) scanBeaconsWithUUID:(NSString *) beaconId {
_scanner.delegate = self;
[_scanner startScanning];
NSLog(@"----------- > EDDYSTONE Implementation Called ");
}
//EDDYSTONE Delegates goes here …
@end
一切安好。能够从 MainController 切换,
id beaconFinderObject = [PCBeaconFinder searchForBeaconMode:iBeacon]; //or ‘Eddystone’ for Google beacon interface.
[beaconFinderObject scanBeaconsWithUUID:@"B0702880-A295-A8AB-F734-031A98A512DE"];
但是为什么不调用相应类的代表。?
注意:信标在范围内。