Similar to the Spring framework to implement dependency injection in OC, springoc

Source: Internet
Author: User

Similar to the Spring framework to implement dependency injection in OC, springoc

If you have used the Spring framework in Java EE development before, you must be familiar with dependency injection. Dependency Injection (DI: Dependency Injection) is one of the implementation methods of IoC: Inversion of Control, and the other is Dependency Lookup (DL: Dependency Lookup ). Of course, this method is mainly used in the Spring framework to control dependency injection in inversion. Of course, in addition to dependency injection, there is also an important concept in the Spring framework, that is, Aspect-oriented programming (AOP ).

In short, dependency injection is used to inject dependency objects into the class, while Aspect-oriented programming is used to add code segments to the class. In this blog, we mainly talk about dependency injection. We will introduce the content of AOP and dependency search in later blogs if you have the opportunity.

This blog is not implemented in Java, but in Objective-C. The specific method to implement dependency injection is to use the reflection mechanism. In this blog, we will use the OC reflection mechanism to see how dependency injection is implemented in iOS development. Of course, in the Spring framework of Java, JavaBean is configured in the XML file. This blog uses PList files commonly used in iOS development to store things similar to JavaBean, that is, we use PList files to replace XML files in this blog. Let's talk about theme.

 

I. Implementation Mechanism of dependency Injection

Dependency injection sounds very tall. In fact, after truly understanding the working principle, there is nothing. The code examples used in this blog talk with us about"Rule Mode. For a blog on the previous rule mode, go to Strategy Pattern in "crossing FireWire". Of course, our previous example is implemented using Swift. This blog uses OC. Although the language is not available, the idea is consistent. In the "Policy Model", we provide different weapon policies for the inaccessible officers through the policy model. This blog still uses this idea, but we follow the"Dependency InjectionTo provide different weapons and strategies for different officers.

Below is the class diagram of the example used in this blog. WeaponType is the parent class of all weapons. It plays the role of "Weapon interface. All weapons are inherited from WeaponType. The Character role class depends on the WeaponType weapon interface class. All Character and WeaponType have dependencies.

  

 

"Dependency injection" literally refers to injecting dependencies. That is, the dependency object is injected into the corresponding class. In the preceding example, Character depends on the WeaponType interface. If "dependency injection" is used to solve this layer of dependencyReflection mechanism("Runtime") dynamically injects the WeaponType subclass object into the dependent attribute corresponding to Character. The dependency information required for reflection is read from the PList file. Of course, Java is configured from XML, which is "dependency injection ".

In this blog, we will fully implement this instance based on the dependencies of the above class diagrams. Of course, in actual implementation, the core content we use is "interface-oriented programming", "object-oriented polymorphism", "reflection mechanism", and "PList File Reading and operation ". The following sections describe them one by one.

 

 

Ii. directory structure of the sample project

Next, let's give an overview of the engineering structure of the instances used in this blog, that is, to understand the sample projects involved in this blog. The directory structure of the project below is the directory structure of the examples involved in this blog. The Weapon folder stores the Weapon interfaces and weapons involved in the Weapon policy. The Character folder stores the directory corresponding to the weapon user. The PList file stores the dependent information of the Character class dependent on the WeaponType interface. The Relation class is responsible for reading the dependency information in the PList file. Based on the dependency information,Reflection mechanismTo the corresponding class.

  

 

 

3. Content in the PList File

The PList file in this blog is similar to the XML used to configure JavaBean in the Spring framework. Of course, the structure and form of the PList file stored in this blog is different from that in XML in Spring, but its function is the same and is used to describe the dependency between classes.

Below is the content in the PList file involved in this example. From the file below, we can see that three classes are stored, one is the Lieutenant (Lieutenant) class, the other is the Captain (Captain) class, and the last is Soldier (Soldier) class. Each class is also the Java JavaBean. The Relation class can instantiate the object of the corresponding class according to the reflection mechanism based on the information provided by these classes in the PList file.

Take Lieutenant as an example. In the PList file, Lieutenant corresponds to this class. From the information of Lieutenant, Lieutenant objects are instantiated by the Character class, however, you need to assign an HK48Weapon class to the Character object dependency attribute weapon during instantiation. Of course, this series of Instantiation and assignment actions are completed by the reflection mechanism. The specific implementation will be provided later.

  

 

 

4. Create a class through the PList File

The Relation class is to assign values to load the content of the corresponding PList file, and then generate the object of the corresponding class based on the loaded content using the reflection mechanism. The following code snippet loads the above PList file content of the Relation class, and then generates the corresponding class object for the content. The following code snippet creates the Lieutenant object, Captain object, and Soldier object based on the context provided by Relation. The details are as follows.

  

After an object is created based on the context, the fire method is called. Because the context of each object is different, that is, the injection dependency object is different, the fire method execution result is also different. Below is the result of the above Code, as shown below:

  

 

 

 

5. Use reflection to inject dependent objects

Next we will look at how to use the reflection mechanism to inject dependent objects, that is, the specific implementation of the Relation class.

1. Provide plist files through initialization

Below is the constructor of the Relation class. The constructor has a parameter plistFileName, which is a plist file used to store context-dependent information. When the Relation class instantiates an object, after receiving the object, it will load the context information in the file, that is, the content in our plist file. The Code is as follows.

  

 

The following code snippet is the core code of this blog. Based on the context information provided in the PList file, the corresponding object is generated and the dependency object is injected to the corresponding properties of the object. Of course, the following describes how to set the dependent object through the Setter method. After setting, the injected dependent object is returned. The details are as follows.

  

 

Today's blog will come here first. In the future, we will find a suitable time to talk about dependency search and Aspect-Oriented Programming. The github link below is the source code sharing link for the Demo involved in this blog, as shown below.

Source code github share link: https://github.com/lizelu/DependencyInjectionForiOS

 

Related Article

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.