Abstract Factory of iOS Design Patterns
Design Patterns are essential to program improvement. Here we will talk about how iOS implements the abstract factory design patterns. This article is written after reading the abstract factory chapter of oc programming. If you do not understand the principles, you can look at that book.
TestView. h first creates a view
//// TestView. h // AbstractFactory /// Created by du jia on 11/10/14. // Copyright (c) 2014 du jia. all rights reserved. // # import
@ Interface TestView: UIView @ end
TestView. m
//// TestView. m // AbstractFactory /// Created by du jia on 11/10/14. // Copyright (c) 2014 du jia. 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, we will create two classes: TestFactory and TestBrandingFactory. TestFactory inherits TestBrandingFactory. The specific implementation is as follows:
TestBrandingFactory. h
//// TestBrandingFactory. h // AbstractFactory /// Created by du jia on 11/10/14. // Copyright (c) 2014 du jia. all rights reserved. // # import
# Import
@ Interface TestBrandingFactory: NSObject + (TestBrandingFactory *) factory;-(UIView *) createTestView :( CGRect) frame; @ end
TestBrandingFactory. m
//// TestBrandingFactory. m // AbstractFactory /// Created by du jia on 11/10/14. // Copyright (c) 2014 du jia. 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 on 11/10/14. // Copyright (c) 2014 du jia. all rights reserved. // # import "TestBrandingFactory. h "@ interface TestFactory: TestBrandingFactory @ end
TestFactory. m
//// TestFactory. m // AbstractFactory /// Created by du jia on 11/10/14. // Copyright (c) 2014 du jia. all rights reserved. // # import "TestFactory. h "# import" TestView. h "@ implementation TestFactory-(UIView *) createTestView :( CGRect) frame {return [[TestView alloc] initWithFrame: frame];} @ end
Finally, paste the implementation
TestBrandingFactory * tmp = [TestBrandingFactory factory]; UIView *v = [tmp createTestView:CGRectMake(50, 110, 100, 50)]; [self.view addSubview:v];