Abstract Factory of iOS Design Patterns

Source: Internet
Author: User

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];



















Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.