定义:用于创建一系列有关联或相互依存的对象,而不必指定其具体的类核创建细节
类型:对象创建
注:
抽象工厂 工厂方法
------------------------------------------
通过对象组合创建抽象产品 通过类继承创建抽象产品
创建多系列产品 创建一种产品
必须修改父类的接口才能支持新的产品 子类化创建者并重载工厂方法以创建新产品
------------------------------------------
类图:
#import <Foundation/Foundation.h> #import <UIKit/UIKit.h> @interface AbstractFactory : NSObject +(AbstractFactory *)factory; - (UIView *)customView; @end
#import "AbstractFactory.h" #import "AbstractFactoryA.h" #import "AbstractFactoryB.h" @implementation AbstractFactory +(AbstractFactory *)factory{ #if defined (USE_A) return [[AbstractFactoryA alloc]init]; #elif defined (USE_B) return [[AbstractFactoryB alloc]init]; #else return nil; #endif } - (UIView *)customView{ return nil; } @end
#import "AbstractFactory.h" @interface AbstractFactoryA : AbstractFactory @end
#import "AbstractFactoryA.h" @implementation AbstractFactoryA - (UIView *)customView{ return [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 100)]; } @end
#import "AbstractFactory.h" @interface AbstractFactoryB : AbstractFactory @end
#import "AbstractFactoryB.h" @implementation AbstractFactoryB - (UIView *)customView{ return [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 100, 100)]; } @end