DHROID-IOC Advanced (interface, object injection)

Source: Internet
Author: User

Below to the interface object injected into the thaw bar, now is our focus, this is the core idea of the IOC, the above is the Android auxiliary
1.5 Object Dependency Issues
Let's take a look at the importance of object object dependency, many students may only learn Android did not learn Java EE, and have not heard the strong spring
For example, in the Dhnet network module of our Dhroid Library, the dialog box is automatically displayed when the network is loaded, but we cannot use the default dialog box, and the actual project box is basically a custom
We don't know what the dialog looks like in our library, but we have to use it again.
At this point we can interface-oriented programming we define an interface Idialog

 Public Interface idialog{publicabstractvoid  Showtoastshort (context context, String s);  Public Abstract Dialog ShowDialog (context context, string s, String s1, Dialogcallback dialogcallback);  Public Abstract int I, string s, String s1, Dialogcallback dialogcallback); // wait for some calling method ...}

I'm going to use it when I need to use a dialog box like the following

// Code Acquisition IDialog D=ioccontainer.getshare (). Get (IDialog.  Class); // or the attribute is annotated in addition @InjectIDialog Dialoger;

So that we can get the Idialog implementation class, of course, this still needs to be configured
The following is the configuration written in application Dialogimpl.class is our specific implementation class

// Configure the dialog object, which is the interface configuration notation // you can write your own dialog object in the project, and then configure it here, using the default configuration provided Ioccontainer.getshare (). Bind (Dialogimpl.  Class). to (IDialog.  Class)// This is a singleton . Scope (Instancescope.scope_singleton);

The above binds Dialogimpl.class to the Idialog.class setting scope of Instancescope.scope_singleton, (Dialogimpl.class is a default implementation)

The following Ioccontainer knowledge

The IOC needs to be initialized with the application first

// initialization of the IOC Ioccontainer.getshare (). InitApplication (this); then Ioccontainer.getshare (). Bind (Concrete implementation Class). to (target class or excuse)  // This is a singleton . Scope (SCOPE)

Binding to an interface is not a nuisance, it's a good example.

See below how to bind to an object class

Class a{ Public voidTest () {}} Class AA Extend a{ Public voidTest () {}}//ConfigurationIoccontainer.getshare (). bind (AA.class). to (A.class). scope (Instancescope.scope_singleton);//after that, if you callA aa=ioccontainer.getshare (). Get (A.class)//or@InjectA AA;//The object to get is a real class of AA, not an instance of a is magical.

The following is the scope of the object

Instancescope.scope_singleton single case (also available for multiple cases) Instancescope.scope_prototype prototypes

Single case Everyone should wait, very classic design pattern,
That is, Ioccontainer.getshare (). Get (A.class) is always the same object
The prototype only calls Ioccontainer.getshare (). Get (A.class) when it is a new object,

As mentioned earlier, multiple cases are also instancescope.scope_singleton at the time of configuration, and there are only a few such objects in many cases. You can take one of those objects fixed.

var atag1=ioccontainer.getshare (). Get (A.class, "Tag1");//Take out all the objects that are marked Tag1, and if not, create a new
var atag2=ioccontainer.getshare (). Get (A.class, "Tag2");//Come up with objects that are marked Tag2.

There are two objects marked as TAG1 and Tag2.

Not yet, the following is the difficulty

We say a dhroid can de-object dependency, dhroid can be not only in the activity can be injected into the class can also
Look at the following example

class Implements injectfields{@Inject public b b; @Overridepublicvoid  injected () {  // This time the injected property already has a value of if(b.a!=null) {log.v ("Dh-info", "This is the log" );}}} A=ioccontainer.getshare (). Get (A.class);

The above-mentioned attribute of a has been assigned.
It's not finished.
We're looking at B.

class Implements injectfields{@Inject public A, @Overridepublicvoid  injected () {  // This time the injected property already has a value of if(a.b!=null) {log.v ("Dh-info", "This is the log" );}}} b b=ioccontainer.getshare (). Get (b.class);

There is also a in B, and B's A is also assigned when B is taken.
A A and B are all singleton, they depend on each other,
You can try this if you don't have to use the IOC to implement your own design patterns. It's not that easy.
All right
The Injectfields interface illustrates that this class relies on other classes
The IOC container assigns a property that he needs to assign when it gets the class.
The interface method injected is called after it and the object to which it depends are assigned
Interface, object injection it's fun.
There's a kind of injection that's injected by name, and I don't think we're going to use much.
This is the method of using the name configuration, so that you can get the object by name, using not many

Ioccontainer.getshare (). Bind (AA.  Class). Name ("Aname"). Scope (Instancescope.scope_singleton); //  // get IA a=ioccontainer.getshare (). Get ("Aname"); // or @Inject (name= "TESTMM") IA A;

This kind of situation actually uses not much, I first realizes under also has the necessity
Do you want to handle the object before the object's properties are injected?

Ioccontainer.getshare (). Bind (A.class). to (A. class ). Scope (Instancescope.scope_prototype). Perpare (new  perpareaction () {@Override  public void  perpare (Object obj) {// handled here }});

This implementation was realized a long time ago, I didn't use it, but I didn't get rid of it.

Here are a few of the pre-defined Android objects

Notificationmanager. class Activitymanager. class Packagemanager. class Assetmanager.clas

Some of the above classes can also be taken with IOC, try it yourself.

about how classes are constructed
The default container is a method created with an empty construction method.
Of course, if you need a context in your construction method
Such as

class a{ public A (context context) {//}}

The IOC will send a global CONTEXTG past
By default, the IOC object's attribute injection will only be injected into the class under the Net.duohuo.dhroid package, and under your project package, if not enough (you have created your own library)
The following configuration is required (this is to add efficiency)
const.ioc_instal_pkg=["packages that can be injected"];

About class inheritance issues
When inheriting a class
Only common attributes in the parent class, that is, the properties of public can be injected

DHROID-IOC Advanced (interface, object injection)

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.