抽象工廠角色劃分
特點(diǎn)->比工廠方法產(chǎn)品種類多
模塊:地圖MapView模塊土至、定位模塊枝秤、導(dǎo)航模塊、搜索模塊…
工廠方法模式:只有1條流水線
創(chuàng)建MapView(產(chǎn)品)
抽象工廠模式:至少2條流水線
創(chuàng)建MapView(產(chǎn)品)
多個(gè)產(chǎn)品->多個(gè)抽象產(chǎn)品
MapView->抽象->MapViewProtocol
MapLocation->抽象->MapLocationProtocol
MapNavigation->抽象->MapNavigationProtocol
……
簡單工廠->抽象->工廠方法(抽象單個(gè)產(chǎn)品)->抽象(抽象多個(gè)產(chǎn)品)->抽象工廠
分析角色艘绍?
角色一:抽象產(chǎn)品A->MapViewProtocol
角色二:抽象產(chǎn)品B->MapLocationProtocol
…
角色三:具體產(chǎn)品A1->BaiduMapView
角色四:具體產(chǎn)品A2->GaodeMapView
…
角色五:具體產(chǎn)品B1->BaiduMapLocation
角色六:具體產(chǎn)品B2->GaodeMapLocation
…
角色七:抽象工廠->MapFactoryProtocol
角色八:具體工廠A->BaiduMapFactory->這里設(shè)置第三方初始化的代碼
角色九:具體工廠B->GaodeMapFactory->這里設(shè)置第三方初始化的代碼
抽象工廠集成-接工廠模式例子繼續(xù)
代碼地址:鏈接:https://pan.baidu.com/s/1qI_sXpEEbommgxeR6YJ0dw 密碼:nsgd
- 百度地圖
- 地圖顯示模塊
角色一:抽象產(chǎn)品A->MapViewProtocol
//
// MapViewProtocol.h
// Dream_20180702_Factory_LBS_Demo
//
// Created by Dream on 2018/7/2.
// Copyright ? 2018年 Tz. All rights reserved.
//
#import <UIKit/UIKit.h>
//面試時(shí)候->講不清楚邏輯關(guān)系(過程)
//地圖標(biāo)準(zhǔn)
@protocol MapViewProtocol<NSObject>
- (instancetype)initWithFrame:(CGRect)frame;
//規(guī)范->父類的引用指向子類的實(shí)例對(duì)象
-(UIView*)getView;
//...地圖類型晦雨、地圖語言、是否開啟交通....
//先忽略...
//設(shè)定是否顯示定位圖層(set方法)
-(void)setShowsUserLocation:(BOOL)isShowsUserLocation;
@end
角色三:具體產(chǎn)品A1->BaiduMapView
//
// BaiduMapView.h
// Dream_20180702_Factory_LBS_Demo
//
// Created by Dream on 2018/7/2.
// Copyright ? 2018年 Tz. All rights reserved.
//
#import <UIKit/UIKit.h>
#import "MapViewProtocol.h"
@interface BaiduMapView : NSObject<MapViewProtocol>
@end
//
// BaiduMapView.m
// Dream_20180702_Factory_LBS_Demo
//
// Created by Dream on 2018/7/2.
// Copyright ? 2018年 Tz. All rights reserved.
//
#import "BaiduMapView.h"
#import <BaiduMapAPI_Base/BMKBaseComponent.h>//引入base相關(guān)所有的頭文件
#import <BaiduMapAPI_Map/BMKMapView.h>//只引入所需的單個(gè)頭文件
@interface BaiduMapView (){
BMKMapView* _mapView;
}
@end
//具體百度地圖
@implementation BaiduMapView
- (instancetype)initWithFrame:(CGRect)frame{
self = [super init];
if (self) {
//初始化百度地圖了
_mapView = [[BMKMapView alloc]initWithFrame:frame];
}
return self;
}
-(UIView*)getView{
return _mapView;
}
-(void)setShowsUserLocation:(BOOL)isShowsUserLocation{
_mapView.showsUserLocation = isShowsUserLocation;
}
@end
角色七:抽象工廠->MapFactoryProtocol
//
// MapFactoryProtocol.h
// Dream_20180702_Factory_LBS_Demo
//
// Created by Dream on 2018/7/2.
// Copyright ? 2018年 Tz. All rights reserved.
//
#import <UIKit/UIKit.h>
#import "MapViewProtocol.h"
#import "MapLocationProtocol.h"
//面試時(shí)候->講不清楚邏輯關(guān)系(過程)
//地圖工廠標(biāo)準(zhǔn)
//抽象工廠非常簡單
@protocol MapFactoryProtocol<NSObject>
- (instancetype)initWithAppkey:(NSString*)appkey;
//地圖標(biāo)準(zhǔn)
-(id<MapViewProtocol>)getMapViewWithFrame:(CGRect)frame;
//增加流水線
-(id<MapLocationProtocol>)getMaplocation;
//導(dǎo)航榔至、搜索...
@end
- 地圖定位模塊
角色二:抽象產(chǎn)品B->MapLocationProtocol
//
// MapLocationProtocol.h
// Dream_20180702_Factory_LBS_Demo
//
// Created by Dream on 2018/7/4.
// Copyright ? 2018年 Tz. All rights reserved.
//
#import <UIKit/UIKit.h>
#import "MapLocationDelegateProtocol.h"
//抽象產(chǎn)品類
@protocol MapLocationProtocol<NSObject>
//監(jiān)聽
-(void)setLocationDelegate:(id<MapLocationDelegateProtocol>)delegate;
//開始
-(void)startLocation;
//停止
-(void)stopLocatioin;
@end
角色五:具體產(chǎn)品B1->BaiduMapLocation
//
// BaiduMapLocation.h
// Dream_20180702_Factory_LBS_Demo
//
// Created by Dream on 2018/7/4.
// Copyright ? 2018年 Tz. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "MapLocationProtocol.h"
@interface BaiduMapLocation : NSObject<MapLocationProtocol>
@end
抽象產(chǎn)品協(xié)議
//
// LocationProtocol.h
// Dream_20180702_Factory_LBS_Demo
//
// Created by Dream on 2018/7/4.
// Copyright ? 2018年 Tz. All rights reserved.
//
#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
//抽象產(chǎn)品類(一個(gè)產(chǎn)品類)
@protocol LocationProtocol<NSObject>
//只能夠獲取抵赢,不能設(shè)置(暫時(shí)規(guī)定)->修改
-(CLLocation *)getLocation;
-(NSString*)getLocationID;
//其他的信息暫時(shí)放一邊
@end
具體產(chǎn)品
//
// BaiduLocation.h
// Dream_20180702_Factory_LBS_Demo
//
// Created by Dream on 2018/7/4.
// Copyright ? 2018年 Tz. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "LocationProtocol.h"
#import <BaiduMapAPI_Map/BMKMapComponent.h>
#import <BMKLocationkit/BMKLocationComponent.h>
@interface BaiduLocation : NSObject<LocationProtocol>
@end
//
// BaiduLocation.m
// Dream_20180702_Factory_LBS_Demo
//
// Created by Dream on 2018/7/4.
// Copyright ? 2018年 Tz. All rights reserved.
//
#import "BaiduLocation.h"
@interface BaiduLocation (){
BMKLocation* _location;
}
@end
@implementation BaiduLocation
- (instancetype)init:(BMKLocation*)location
{
self = [super init];
if (self) {
_location = location;
//不能直接創(chuàng)建BMKLocation
//傳遞
//初始化過程百度地圖內(nèi)部創(chuàng)建
}
return self;
}
-(CLLocation *)getLocation{
return _location.location;
}
-(NSString*)getLocationID{
return _location.locationID;
}
//其他屬性也是如此
@end
//
// BaiduUserLocatioin.h
// Dream_20180702_Factory_LBS_Demo
//
// Created by Dream on 2018/7/4.
// Copyright ? 2018年 Tz. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "LocationProtocol.h"
#import <BaiduMapAPI_Map/BMKMapComponent.h>
#import <BMKLocationkit/BMKLocationComponent.h>
@interface BaiduUserLocatioin : NSObject<LocationProtocol>
- (instancetype)init:(BMKUserLocation*)location;
@end
//
// BaiduUserLocatioin.m
// Dream_20180702_Factory_LBS_Demo
//
// Created by Dream on 2018/7/4.
// Copyright ? 2018年 Tz. All rights reserved.
//
#import "BaiduUserLocatioin.h"
@interface BaiduUserLocatioin (){
BMKUserLocation* _location;
}
@end
@implementation BaiduUserLocatioin
- (instancetype)init:(BMKUserLocation*)location
{
self = [super init];
if (self) {
_location = location;
//不能直接創(chuàng)建BMKLocation
//傳遞
//初始化過程百度地圖內(nèi)部創(chuàng)建
}
return self;
}
-(CLLocation *)getLocation{
return _location.location;
}
-(NSString*)getLocationID{
return @"";
}
@end
地圖回調(diào)協(xié)議
//
// MapLocationDelegateProtocol.h
// Dream_20180702_Factory_LBS_Demo
//
// Created by Dream on 2018/7/4.
// Copyright ? 2018年 Tz. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "LocationProtocol.h"
//地圖回調(diào)
//方式有些差別
//處理這些差別
@protocol MapLocationDelegateProtocol<NSObject>
//很多方法需要我們定義->后面再來搞->寫我們自己
- (void)didUpdateUserHeading:(id<LocationProtocol>)userLocation;
@end
- 高德地圖
- 地圖顯示模塊
角色一:抽象產(chǎn)品A->MapViewProtocol
角色四:具體產(chǎn)品A2->GaodeMapView
//
// GaodeMapView.h
// Dream_20180702_Factory_LBS_Demo
//
// Created by Dream on 2018/7/2.
// Copyright ? 2018年 Tz. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "MapViewProtocol.h"
@interface GaodeMapView : NSObject<MapViewProtocol>
@end
//
// GaodeMapView.m
// Dream_20180702_Factory_LBS_Demo
//
// Created by Dream on 2018/7/2.
// Copyright ? 2018年 Tz. All rights reserved.
//
#import "GaodeMapView.h"
#import <MAMapKit/MAMapKit.h>
#import <AMapFoundationKit/AMapFoundationKit.h>
@interface GaodeMapView (){
MAMapView* _mapView;
}
@end
//具體高德地圖
@implementation GaodeMapView
- (instancetype)initWithFrame:(CGRect)frame{
self = [super init];
if (self) {
//初始化高德地圖了
_mapView = [[MAMapView alloc] initWithFrame:frame];
}
return self;
}
-(UIView*)getView{
return _mapView;
}
-(void)setShowsUserLocation:(BOOL)isShowsUserLocation{
_mapView.showsUserLocation = isShowsUserLocation;
}
@end
角色七:抽象工廠->MapFactoryProtocol
角色九:具體工廠B->GaodeMapFactory->這里設(shè)置第三方初始化的代碼
//
// GaodeMapFactory.h
// Dream_20180702_Factory_LBS_Demo
//
// Created by Dream on 2018/7/2.
// Copyright ? 2018年 Tz. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "MapFactoryProtocol.h"
@interface GaodeMapFactory : NSObject<MapFactoryProtocol>
@end
//
// GaodeMapFactory.m
// Dream_20180702_Factory_LBS_Demo
//
// Created by Dream on 2018/7/2.
// Copyright ? 2018年 Tz. All rights reserved.
//
#import "GaodeMapFactory.h"
#import "GaodeMapView.h"
#import <MAMapKit/MAMapKit.h>
#import <AMapFoundationKit/AMapFoundationKit.h>
#import "GaodeMapLocation.h"
@implementation GaodeMapFactory
- (instancetype)initWithAppkey:(NSString*)appkey{
self = [super init];
if (self) {
[AMapServices sharedServices].apiKey = appkey;
}
return self;
}
-(id<MapViewProtocol>)getMapViewWithFrame:(CGRect)frame{
return [[GaodeMapView alloc] initWithFrame:frame];
}
-(id<MapLocationProtocol>)getMaplocation{
return [[GaodeMapLocation alloc] init];
}
@end
- 地圖定位模塊
角色二:抽象產(chǎn)品B->MapLocationProtocol
角色六:具體產(chǎn)品B2->GaodeMapLocation
//
// GaodeMapLocation.h
// Dream_20180702_Factory_LBS_Demo
//
// Created by Dream on 2018/7/4.
// Copyright ? 2018年 Tz. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "MapLocationProtocol.h"
@interface GaodeMapLocation : NSObject<MapLocationProtocol>
@end
//
// GaodeMapLocation.m
// Dream_20180702_Factory_LBS_Demo
//
// Created by Dream on 2018/7/4.
// Copyright ? 2018年 Tz. All rights reserved.
//
#import "GaodeMapLocation.h"
@implementation GaodeMapLocation
//監(jiān)聽
-(void)setLocationDelegate:(id<MapLocationDelegateProtocol>)delegate{
//自定義一個(gè)實(shí)現(xiàn)類->MapLocationDelegateProtocol
}
//開始
-(void)startLocation{
//調(diào)用高德開始定位
}
//停止
-(void)stopLocatioin{
//調(diào)用高德停止定位
}
@end
調(diào)用
//
// ViewController.m
// Dream_20180702_Factory_LBS_Demo
//
// Created by Dream on 2018/7/2.
// Copyright ? 2018年 Tz. All rights reserved.
//
#import "ViewController.h"
#import "GaodeMapFactory.h"
#import "BaiduMapFactory.h"
#import "MapEngine.h"
@interface ViewController ()<MapLocationDelegateProtocol>
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// id<MapFactoryProtocol> factory = [[BaiduMapFactory alloc] init];
// id<MapViewProtocol> mapView = [factory getMapViewWithFrame:self.view.frame];
// [self.view addSubview:[mapView getView]];
// MapEngine* engine = [[MapEngine alloc] init];
// id<MapFactoryProtocol> factory = [engine getFactoryWithFrame:self.view.frame];
// id<MapViewProtocol> mapView = [factory getMapViewWithFrame:self.view.frame];
// [self.view addSubview:[mapView getView]];
//直接運(yùn)行
//開發(fā)里面->很多第三方代碼直接融入到了代碼中->當(dāng)我們更新切換的時(shí)候,你會(huì)發(fā)現(xiàn)很麻煩
// BMKMapView* mapView = [[BMKMapView alloc]initWithFrame:self.view.bounds];
// self.view = mapView;
//重構(gòu)之后的代碼
//修改內(nèi)部結(jié)構(gòu)
//外部調(diào)用沒有任何修改
MapEngine* engine = [[MapEngine alloc] init];
id<MapFactoryProtocol> factory = [engine getFactory];
id<MapViewProtocol> mapView = [factory getMapViewWithFrame:self.view.frame];
[self.view addSubview:[mapView getView]];
id<MapLocationProtocol> location = [factory getMaplocation];
[location startLocation];
[location stopLocatioin];
[location setLocationDelegate:self];
}
- (void)didUpdateUserHeading:(id<LocationProtocol>)userLocation{
//回調(diào)
//獲取經(jīng)度和緯度
[userLocation getLocation];
//設(shè)置->更新UI界面->最終結(jié)果實(shí)現(xiàn)了定位了
//UI構(gòu)建(工廠模式->參數(shù)簡單唧取、構(gòu)建者->參數(shù)復(fù)雜)
//數(shù)據(jù)庫對(duì)象管理(配置文件)->服務(wù)器開發(fā)中非常多(spring铅鲤、hebernate...)
//系統(tǒng)內(nèi)部做了處理(SDK設(shè)計(jì)->常用)->UIView設(shè)置屬性非常多->構(gòu)建者
//設(shè)計(jì)到配置問題一般情況下都是使用工廠處理
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end