Ioc Introduction

xiaoxiao2021-03-06  70

Introduction to IOC

See IoC with An Example in Pico. Changes to this Example To Use it with spring is in Spring Example.

Example for Conventional Lookup (E.G. WITH JNDI IN EJBS):

Public class foo {

Public foo () {

public string service () {

Return Barmanager.lookup ("mybar"). DOSMETHING ();

}

}

EXAMPLE OF IOC 3:

Public class foo {

Private Bar Bar; Public Foo (Bar Bar) {

THIS.BAR = BAR;

public string service () {

Return bar.dosomething ();

}

}

Types

Method-based (M) IoC: Pass dependent components to the component with every method call Interface-based (I) IoC (Type 1): Uses Interfaces like Serviceable, Configurable etc. for declaring dependencies Setter-based (S) IoC (Type 2): Uses setters for setting dependent Components Constructor-based (c) oc (Type 3): Uses Constructionors for Decilaning Dependencies

Example Method Based Ioc:

Public class foo {

Public foo () {

} Public String Service (bar bar) {

Return bar.dosomething ();

}

}

Example of IOC 1:

Public class foo imports barable {

Public foo () {

} public void dobar (Barmanager BM) {

Bar bar = (bar) bm.lookup ("mybar");

Bar.Service ();

}

}

Example of IOC 2:

Public class foo {

PRIVATE BAR BAR; public foo () {

} public void setbar (bar bar) {

THIS.BAR = BAR;

public string service () {

Return bar.dosomething ();

}

}

Disadvantages

You Declare Your Deprondencies, Some Magic Happens and They Are Resolved. Magic Makes Source Code Harden To Understand Than Lookups (Because They Are Implicit NOT Explicit)

Advantages

If you use singletons or lookups your unit tests are difficult to write (Just see all those special J2EE unit frameworks). With lookups you usually have to implement a registry that supplies MockObjects. If your lookups are static methods replacing them with MockObjects is even more Difficult. with lookups you might has to do this (if you can control the registry): // What if you can't change the create Class

// in Your Singleton or Registry? Do you need to shutrite

// the whole jndi lookup classes?

Barmanager.setBarclass ("Mybar", MockBar.class; foo foo = new foo ();

Assertequals ("ping", foo.service ());

Ioc is more Junit Test Friendly Than Lookups. With Type-3 Ioc You Just Do:

Foo foo = new foo (new mockbar ());

Assertequals ("ping", foo.service ());

WITH TYPE-2 this Would Be:

Foo foo = new foo ();

Foo.setbar (New Mockbar ());

Assertequals ("ping", foo.service ());

No external dependecies. You can develop and test your components in the enviroment you like, you do not need to use a special deployment enviroment for your components during development (like with JNDI / EJB) Easier to reuse and easy exchangeable between different IoC containers. Either Implement Simple Wrappers or Add Setters and Interfaces To Use Components from Eg Pico in Avalon and Spring

Advantages of Type-2

Beans Are Well Undrew by Java Developers, Beans Exist in Most Projects More Easily Satisfies Optional Dependencies

Disadvantages of Type-2

Dependencies can be seen from the code. What are dependencies and what are normal setters and getters? Additional setters and getters make code more noisy Needs some XML (or other) meta data which makes understanding code more difficult from looking at itAdvantages of Type-3

Stronger Contract Between Components Components CANNOT EXIST IN "Limbo" State Between Creation and when the can be used. This is more defensive

Disadvantages of Type-3

You May NEED YOUR CONSTRUCTORS (I do not :-) Inheritance Can Become More Difficult.

转载请注明原文地址:https://www.9cbs.com/read-85221.html

New Post(0)