Design patterns are an essential knowledge of program promotion, and here's how iOS implements abstract factory design patterns. This article is a look at the OC programming of the Abstract factory this chapter after writing, if you do not understand the principle can look at the book.
TestView.h first create a view
testview.h// abstractfactory//// Created by du Jia Li on 11/10/14.// Copyright (c) 2014 DuPont. All rights reserved.//#import <UIKit/UIKit.h> @interface testview:uiview@end
testview.m
testview.m// abstractfactory//// Created by du Jia Li on 11/10/14.// Copyright (c) 2014 DuPont. All rights reserved.//#import ' TestView.h ' @implementation testview-(ID) initWithFrame: (cgrect) frame{ if (self = [ Super Initwithframe:frame]) { Self.backgroundcolor = [Uicolor redcolor]; } return self;} @end
Next, create two classes testfactory, testbrandingfactory whichTestfactory inherits Testbrandingfactory. The specific implementation is as follows:
TestBrandingFactory.h
testbrandingfactory.h// abstractfactory//// Created by du Jia Li on 11/10/14.// Copyright (c) 2014 DuPont. All rights reserved.//#import <Foundation/Foundation.h> #import <UIKit/UIKit.h> @interface testbrandingfactory:nsobject+ (Testbrandingfactory *) factory;-(UIView *) Createtestview: (CGRect) frame; @end
Testbrandingfactory.m
testbrandingfactory.m// abstractfactory//// Created by du Jia Li on 11/10/14.// Copyright (c) 2014 DuPont. All rights reserved.//#import "TestBrandingFactory.h" #import "TestFactory.h" @implementation testbrandingfactory+ ( Testbrandingfactory *) factory{ return [[Testfactory alloc] init];} -(UIView *) Createtestview: (cgrect) frame{ return nil;} @end
TestFactory.h
testfactory.h// abstractfactory//// Created by du Jia Li on 11/10/14.// Copyright (c) 2014 DuPont. All rights reserved.//#import "TestBrandingFactory.h" @interface testfactory:testbrandingfactory@end
Testfactory.m
testfactory.m// abstractfactory//// Created by du Jia Li on 11/10/14.// Copyright (c) 2014 DuPont. All rights reserved.//#import "TestFactory.h" #import "TestView.h" @implementation testfactory-(UIView *) Createtestview: (cgrect) frame{ return [[TestView alloc] initwithframe:frame];} @end
Finally post the implementation
Testbrandingfactory * tmp = [testbrandingfactory factory]; UIView *v = [tmp createtestview:cgrectmake (+, +, +)]; [Self.view Addsubview:v];
Abstract Factory of IOS design mode