iOS design mode-Abstract factory
Schematic diagram
Description
1. Abstract factory refers to the provision of an interface that creates a series of related or interdependent objects without specifying their specific classes
2. If multiple classes have the same behavior, but the actual implementation is different, it may be necessary for an abstract type to be inherited as its parent class, which defines the common behavior that all related concrete classes will share.
Source
Https://github.com/YouXianMing/AbstractFactoryPattern
////BrandingFactory.h//Abstractfactorypattern////Created by youxianming on 15/8/2.//Copyright (c) 2015 youxianming. All rights reserved.//#import<Foundation/Foundation.h>#import<UIKit/UIKit.h>@interfaceBrandingfactory:nsobject/** * Abstract Factory Method * * @return Specific factory*/+ (Brandingfactory *) factory;/** * Brandedview produced by the Factory (constructed by the specific factory) * * @return production of good Brandedview*/-(UIView *) Brandedview;/** * Brandedmainbutton produced by the Factory (constructed by the specific factory) * * @return production of good Brandedmainbutton*/-(UIButton *) Brandedmainbutton;@end
////brandingfactory.m//Abstractfactorypattern////Created by youxianming on 15/8/2.//Copyright (c) 2015 youxianming. All rights reserved.//#import "BrandingFactory.h"#import "AcmeBrandingFactory.h"#import "SierraBrandingFactory.h"@implementationbrandingfactory+ (Brandingfactory *) Factory {if([[Selfclass] Issubclassofclass:[acmebrandingfactoryclass]]) { return[AcmebrandingfactoryNew]; } Else if([[Selfclass] Issubclassofclass:[sierrabrandingfactoryclass]]) { return[SierrabrandingfactoryNew]; } Else { returnNil; }}-(UIView *) Brandedview {returnNil;}-(UIButton *) Brandedmainbutton {returnNil;}@end
// // AcmeBrandingFactory.h// abstractfactorypattern / /// Created by youxianming on 15/8/2. // Copyright (c) 2015 youxianming. All rights reserved. // #import " BrandingFactory.h " @interface acmebrandingfactory:brandingfactory @end
////acmebrandingfactory.m//Abstractfactorypattern////Created by youxianming on 15/8/2.//Copyright (c) 2015 youxianming. All rights reserved.//#import "AcmeBrandingFactory.h"@implementationacmebrandingfactory-(UIView *) Brandedview {NSLog (@"Acmebrandedview"); returnNil;}-(UIButton *) Brandedmainbutton {NSLog (@"Acmebrandedmainbutton"); returnNil;}@end
// // SierraBrandingFactory.h// abstractfactorypattern/ /// Created by youxianming on 15/8/2. // Copyright (c) 2015 youxianming. All rights reserved. // #import " BrandingFactory.h " @interface sierrabrandingfactory:brandingfactory @end
////sierrabrandingfactory.m//Abstractfactorypattern////Created by youxianming on 15/8/2.//Copyright (c) 2015 youxianming. All rights reserved.//#import "SierraBrandingFactory.h"@implementationsierrabrandingfactory-(UIView *) Brandedview {NSLog (@"Sierrabrandedview"); returnNil;}-(UIButton *) Brandedmainbutton {NSLog (@"Sierrabrandedmainbutton"); returnNil;}@end
Analysis
Schematic diagram of the relationship
iOS design mode-Abstract factory