抽象工厂角色划分
特点->比工厂方法产品种类多
模块:地图MapView模块、定位模块、导航模块、搜索模块…
工厂方法模式:只有1条流水线
创建MapView(产品)
抽象工厂模式:至少2条流水线
创建MapView(产品)
多个产品->多个抽象产品
MapView->抽象->MapViewProtocol
MapLocation->抽象->MapLocationProtocol
MapNavigation->抽象->MapNavigationProtocol
……
简单工厂->抽象->工厂方法(抽象单个产品)->抽象(抽象多个产品)->抽象工厂
分析角色?
角色一:抽象产品A->MapViewProtocol
角色二:抽象产品B->MapLocationProtocol
…
角色三:具体产品A1->BaiduMapView
角色四:具体产品A2->GaodeMapView
…
角色五:具体产品B1->BaiduMapLocation
角色六:具体产品B2->GaodeMapLocation
…
角色七:抽象工厂->MapFactoryProtocol
角色八:具体工厂A->BaiduMapFactory->这里设置第三方初始化的代码
角色九:具体工厂B->GaodeMapFactory->这里设置第三方初始化的代码
抽象工厂集成-接工厂模式例子继续
代码地址:链接:https://pan.baidu.com/s/1qI_sXpEEbommgxeR6YJ0dw 密码:nsgd
- 百度地图
- 地图显示模块
角色一:抽象产品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>
//面试时候->讲不清楚逻辑关系(过程)
//地图标准
@protocol MapViewProtocol<NSObject>
- (instancetype)initWithFrame:(CGRect)frame;
//规范->父类的引用指向子类的实例对象
-(UIView*)getView;
//...地图类型、地图语言、是否开启交通....
//先忽略...
//设定是否显示定位图层(set方法)
-(void)setShowsUserLocation:(BOOL)isShowsUserLocation;
@end
角色三:具体产品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相关所有的头文件
#import <BaiduMapAPI_Map/BMKMapView.h>//只引入所需的单个头文件
@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"
//面试时候->讲不清楚逻辑关系(过程)
//地图工厂标准
//抽象工厂非常简单
@protocol MapFactoryProtocol<NSObject>
- (instancetype)initWithAppkey:(NSString*)appkey;
//地图标准
-(id<MapViewProtocol>)getMapViewWithFrame:(CGRect)frame;
//增加流水线
-(id<MapLocationProtocol>)getMaplocation;
//导航、搜索...
@end
- 地图定位模块
角色二:抽象产品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"
//抽象产品类
@protocol MapLocationProtocol<NSObject>
//监听
-(void)setLocationDelegate:(id<MapLocationDelegateProtocol>)delegate;
//开始
-(void)startLocation;
//停止
-(void)stopLocatioin;
@end
角色五:具体产品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
抽象产品协议
//
// 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>
//抽象产品类(一个产品类)
@protocol LocationProtocol<NSObject>
//只能够获取,不能设置(暂时规定)->修改
-(CLLocation *)getLocation;
-(NSString*)getLocationID;
//其他的信息暂时放一边
@end
具体产品
//
// 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;
//不能直接创建BMKLocation
//传递
//初始化过程百度地图内部创建
}
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;
//不能直接创建BMKLocation
//传递
//初始化过程百度地图内部创建
}
return self;
}
-(CLLocation *)getLocation{
return _location.location;
}
-(NSString*)getLocationID{
return @"";
}
@end
地图回调协议
//
// 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"
//地图回调
//方式有些差别
//处理这些差别
@protocol MapLocationDelegateProtocol<NSObject>
//很多方法需要我们定义->后面再来搞->写我们自己
- (void)didUpdateUserHeading:(id<LocationProtocol>)userLocation;
@end
- 高德地图
- 地图显示模块
角色一:抽象产品A->MapViewProtocol
角色四:具体产品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->这里设置第三方初始化的代码
//
// 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
- 地图定位模块
角色二:抽象产品B->MapLocationProtocol
角色六:具体产品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
//监听
-(void)setLocationDelegate:(id<MapLocationDelegateProtocol>)delegate{
//自定义一个实现类->MapLocationDelegateProtocol
}
//开始
-(void)startLocation{
//调用高德开始定位
}
//停止
-(void)stopLocatioin{
//调用高德停止定位
}
@end
调用
//
// 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]];
//直接运行
//开发里面->很多第三方代码直接融入到了代码中->当我们更新切换的时候,你会发现很麻烦
// BMKMapView* mapView = [[BMKMapView alloc]initWithFrame:self.view.bounds];
// self.view = mapView;
//重构之后的代码
//修改内部结构
//外部调用没有任何修改
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{
//回调
//获取经度和纬度
[userLocation getLocation];
//设置->更新UI界面->最终结果实现了定位了
//UI构建(工厂模式->参数简单、构建者->参数复杂)
//数据库对象管理(配置文件)->服务器开发中非常多(spring、hebernate...)
//系统内部做了处理(SDK设计->常用)->UIView设置属性非常多->构建者
//设计到配置问题一般情况下都是使用工厂处理
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end