Use objection for modular iOS Project Development

Source: Internet
Author: User

Use objection for modular iOS Project Development

Objection is a lightweight dependency injection framework. Inspired by Guice, Google Wallet is also used in this project. Dependency injection is a design pattern for Object-Oriented Programming to reduce the coupling between codes. It is usually implemented based on interfaces. That is to say, an object does not need to be new, but is obtained through the relevant controller. Laravel, the most popular PHP framework in 2013, is a typical example.

Assume that there is a button in ViewControllerA. view, and a ViewControllerB is pushed after you click it. The simplest syntax is as follows:
  1. -(Void) viewDidLoad
  2. {
  3. [Super viewDidLoad];
  4. UIButton * button = [UIButton buttonWithType: UIButtonTypeSystem];
  5. Button. frame = CGRectMake (100,100,100, 30 );
  6. [Button setTitle: @ "Button" forState: UIControlStateNormal];
  7. [Button addTarget: self action: @ selector (buttonTapped) forControlEvents: UIControlEventTouchUpInside];
  8. [Self. view addSubview: button];
  9. }
  10.  
  11. -(Void) buttonTapped
  12. {
  13. ViewControllerB * vc = [[ViewControllerB alloc] init];
  14. [Self. navigationController pushViewController: vc animated: YES];
  15. }
In this case, ViewControllerA needs to import ViewControllerB, that is, the dependency on ViewControllerB. The more dependent things, the more difficult it is to maintain, and the loop dependency problem may also occur. objection can handle these problems. The implementation method is: first define a protocol, and then register the class corresponding to this protocol through objection. When necessary, you can obtain the object corresponding to this protocol. The user does not need to care which Class is actually used, but all the methods and attributes are available (specified in the Protocol ). This removes the dependency on a specific Class. This is what we usually call "interface-oriented programming 」.
  1. JSObjectionInjector * injector = [JSObjection defaultInjector]; // [1]
  2. UIViewController * Vc = [injector getObject: @ protocol (ViewControllerAProtocol)]; // [2]
  3. Vc. backgroundColor = [UIColor lightGrayColor]; // [3]
  4. UINavigationController * nc = [[UINavigationController alloc] initWithRootViewController: vc];
  5. Self. window. rootViewController = nc;
[1] Get the default injector, which has already registered ViewControllerAProtocol. [2] Get the Object corresponding to ViewControllerAProtocol. [3] After obtaining the VC, set some of its attributes, such as backgroundColor. Because this attribute is defined in ViewControllerAProtocol, there is no warning. ViewControllerA is not referenced here. Let's take a look at how ViewControllerAProtocol registers in injector. Here it involves Module, and Protocol registration is completed in Module. Module only needs to inherit the Class JSObjectionModule.
  1. @ Interface ViewControllerAModule: JSObjectionModule
  2. @ End
  3.  
  4. @ Implementation ViewControllerAModule
  5. -(Void) configure
  6. {
  7. [Self bindClass: [ViewControllerA class] toProtocol: @ protocol (ViewControllerAProtocol)];
  8. }
  9. @ End
The binding operation is performed in the configure method. This method is automatically triggered when it is added to the injector.
  1. JSObjectionInjector * injector = [JSObjection defaultInjector]; // [1]
  2. Injector = injector? : [JSObjection createInjector]; // [2]
  3. Injector = [injector withModule: [[ViewControllerAModule alloc] init]; // [3]
  4. [JSObjection setDefaultInjector: injector]; // [4]
[1] Get the default injector [2] if the default injector does not exist, create a new [3] and register our Module [4] to this injector. Set this injector as the default injector. This code can be directly put into the + (void) load for execution, this avoids importing various modules in AppDelegate. Because we cannot directly obtain the corresponding Class, we must define the exposed methods and attributes in the protocol, and then the Class must implement the Protocol.
  1. @ Protocol ViewControllerAProtocol
  2. @ Property (nonatomic) NSUInteger currentIndex;
  3. @ Property (nonatomic) UIColor * backgroundColor;
  4. @ End
  5.  
  6. @ Interface ViewControllerA: UIViewController
  7. @ End
After objection is used to implement dependency injection, SRP (Single Responsibility Principle) can be better implemented. The code is simpler, the mood is better, and the life is better. For Pinterest, the following page can be divided into three sections. Different sections can be taken care of by different people and then stringed together to avoid the emergence of Mess View Controller. In general, this lib is quite reliable. It has been maintained for more than two years and some projects are in use. It will also be helpful for improving the efficiency of development members, you can try it.

CocoaChina is the world's largest Chinese developer community for Apple. It regularly pushes various exciting R & D tutorial resources and tools on a daily basis to introduce app marketing experience and the latest enterprise recruitment and outsourcing information, the latest developments and training information of the Cocos2d engine and Cocos Studio development kit. Follow these steps to get to know the latest product and service trends!


Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.