Inversion Of Control

587 views

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
587
On SlideShare
0
From Embeds
0
Number of Embeds
7
Actions
Shares
0
Downloads
18
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide
  • About myself and company- my aim in this presentation is to make you aware of IoC , by illustrating the design problem and learn how we can resolve the problem utilizing frameworks that implments IoC
  • what does it actually mean? Some of us may first end up with very puzzled. -- You go the bar and ask for the beer. You don’t tell bartender what kind of beer. So, bartender come back with bud light, coor light or heineken. -Question is what part of aspect of control are being inverted. - If your goal is to build is house, you shouldn’t be worried about how to manufacture hardware and other materials. They will be provided when needed. -- IoC works same way, A component such as DataStore can specify that it requires a DAO, and later be passed one when it is needed. If the DataStore specified that it wished to use a database-aware component, it would be totally controlling the type of object it would be using. However, by allowing the container to choose one for it, it is delegating its control. (Hence “ Inversion of Control”)-- Travelling by air you or your colleagues in an organization, need to arrange following…Decide the destination, arrival date and time.Call up airline agency for flight bookingCall up rental/cab agency for scheduling pickup/drop off --- Company decided to use another agencies, you and all other colleague has to relearn how to make contact and their mechanisms. --- New protocol: company created an administrative department, you are use IVR system where you simply state destination, dates and time, your flight reservations are made, cab is scheduled for time and ticket delivered to you. Now if preferred agencies were changed, administration dept. would become aware of a change, would perhaps readjust its workflow and IVR system could be reprogrammed but way we interact with IVR remains same. –see how dependency injection is done here!!
  • The biggest problem is that CameraMan class controls the creation of StillCamera object Moreover direct reference to StillCamera class leads to tight coupling between CameraMan and StillCamera
  • Free from object reference but still tight with control of creating StillCamera object.
  • This is where we completely inversion the control of creating the objects referred by the components by deligating lookup functionality and injecting dependencies to Container class- Hence, container class implemented the Inversion of control. Container is responsible for injecting the necessary resources into each component.Note as early I have stated, the component has to communicate with another component to accomplish the job in hand. A common issue to deal with is how to wire together different elements. So, at somepoint somehow somewhere, there should would be a component that creates and manages components: such components is call container. We created Container class to dump all the complexities and make other components reusable and lightweighted.
  • By using container framework that implements IoC, I mean that follow dependency pattern, we achieved three major advantanges.
  • Spring framework implements IoC principle by using Dependency Injection pattern. Here in example we are using xml-based Spring container to create, manage and inject the dependency components/objectsOnce, container take care of all contructions and managemnt of objects/component, we can focus more on implementations: The solution shifts the object creation control from CameraMan class to some one else.
  • The basic idea for the Dependency Injection is to have a separate component/object, an assembler, that populates a field in the CameraMan class with an appropriate implementation for the camera interface.
  • As stated before, IoC is the design principle and DI is the design pattern, way to implement the IoC. There are three ways of implementing IoC using DIOther way of implementations: Factory patternService locator, Sun’s core Java EE design patterns
  • The most popular one. container injects the dependency via setter method declared in a component.
  • Here dependencies are inject via a constructor. Support by most IoC container including Spring. avoids problem posed by setter injection, as all dependencies has to be provided in the constructor arguments list, so impossible to miss. once injected, can’t be modified, preventing careless modification problem. Shortcoming: JVM unable to add default constructor, explicit definition required if needed. when dependencies is high, long list of constructor arguments reduce readablity of code.
  • Among three types of injection, interface injection is rarely or seldom used. To apply it, component must implement a particular interface defined by the container, so that the container can inject dependencies via the interface. ShortComing: requires all the components must implement a particular interface for container to inject dependencies. Since interface is container specific, components have to rely on the container and cannot be reused outside its scope
  • Inversion Of Control

    1. 1. Inversion of Control<br />a design principle<br />Rupesh Bhochhibhoya<br />Software Engineer,<br />Telogical Systems<br />
    2. 2. Inversion of Control, in real life!<br /><ul><li> Go to bar, ask for beer!! …sense of reverse control.
    3. 3. Hollywood Principle: Don’t call us, we’ll call you.
    4. 4. Business trips in an organization… example from TheServerSide.com [1]</li></ul>Dependency Injection, DI: the word used interchangeably<br />
    5. 5. Inversion of Control, in software Context<br /><ul><li>In OO Design, components/objects are desired to be reusable.
    6. 6. However, components depend with one another to deliver the solution.
    7. 7. Such dependencies among components results the tight coupling.</li></ul>D<br />
    8. 8. Inversion of Control, in software Context …<br />publicclassCameraMan {<br /> StillCamera sCamera;<br /> public void shoot() {<br />sCamera = new StillCamera();<br />sCamera.action();<br /> }<br />}<br />publicclass StillCamera{<br />publicvoid action() {<br />System.out.println(“ take Picture");<br /> } <br />}<br />publicclassVideoCamera{<br />publicvoid action() {<br />System.out.println(“video");<br /> } <br />} <br />
    9. 9. Inversion of Control, in software Context …<br />publicclassCameraMan {<br /> private Camera camera;<br /> public void shoot(){<br /> camera = = new StillCamera();<br />camera.action();<br />}<br />}<br />publicinterface Camera {<br />publicvoidaction(); <br />}<br />publicclass StillCamera implements Camera {<br />@Override<br />publicvoid action() {<br />System.out.println(“shoot still picture");<br /> } <br />}<br />
    10. 10. Inversion of Control, in software Context …<br />publicclassCameraMan {<br /> private Camera camera;<br /> public void setCamera(Camera camera) {<br />this.camera = camera;<br /> }<br /> public void shoot(){<br />camera.action();<br />}<br />}<br />publicclassMainClass {<br /> public static void main(String[] args) {<br /> Container container = new Container();<br />CameraMancMan =container.getComponent(“cameraman”);<br />cMan.shoot();<br />}<br />}<br />publicclass Container {<br />private Map<String, Object> components;<br />public Container(){<br />components = newHashMap<String, Object>();<br />StillCamerasCamera = newStillCamera();<br />components.put("stillcamera", sCamera);<br />CameraMancMan = newCameraMan();<br />cMan.setCamera(sCamera);<br />components.put("cameraman", cMan);<br />}<br />public Object getComponent(String id){<br />returncomponents.get(id);<br />}<br />}<br />publicinterface Camera {<br />publicvoidaction(); <br />}<br />publicclass StillCamera implements Camera {<br />@Override<br /> publicvoid action() {<br />System.out.println(“shoot still picture");<br /> } <br />}<br />
    11. 11. Inversion of Control, in software Context …<br />Container: actx<br />Component dependency resolution<br /><ul><li>Among components
    12. 12. Within component</li></ul>Configuration management<br />Lifecycle management<br />
    13. 13. Inversion of Control, in software Context …<br />Using Spring framework <br />publicclassMainClass {<br /> public static void main(String[] args) {<br />ApplicationContentactx = newClassPathXMLApplicationContent(“spring.xml”);<br />CameraMancMan = (CameraMan)actx.getBean(“cameraman”);<br />cMan.click();<br />}<br />}<br />spring.xml<br /><?xml version="1.0" encoding="UTF-8"?><br /><beans xmlns =http://www.springframework.org/schema/beans xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance" <br />xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"><br /><bean id = “cameraman” class = “CameraMan” ><br /><property name= “camera” ref=“stillcamera” /><br /></bean><br /><bean id=“stillcamera” class=“StillCamera” /><br /></beans><br />
    14. 14. Inversion of Control, in software Context …<br /><<interface>><br />Camera<br /><<Assembler>><br />Container<br />CameraMan<br />spring.xml<br /><creates><br />StillCamera<br /><creates><br />
    15. 15. Ways to implement IoC<br />IoC<br />DI<br />Constructor injection<br />Setter injection<br />Interface injection<br />
    16. 16. Ways to implement IoC<br />publicclassCameraMan {<br /> private Camera camera;<br /> public void setCamera(Camera camera) {<br />this.camera = camera;<br /> }<br />}<br />IoC<br />publicclass Container {<br />private Map<String, Object> components;<br />public Container(){<br />components = newHashMap<String, Object>();<br />StillCamerasCamera = newStillCamera();<br />components.put("stillcamera", sCamera);<br />CameraMancMan = newCameraMan();<br />cMan.setCamera(sCamera);<br />components.put("cameraman", cMan);<br />}<br />public Object getComponent(String id){<br />returncomponents.get(id);<br />}<br />}<br />DI<br />Setter injection<br />Interface injection<br />Constructor injection<br />Spring.xml<br />…<br /><bean id = “cameraman” class = “CameraMan” ><br /><property name= “camera” ref=“stillcamera” /><br /></bean><br /><bean id=“stillcamera” class=“StillCamera” /><br />…<br />
    17. 17. Ways to implement IoC<br />publicclassCameraMan {<br /> private Camera camera;<br />public CameraMan(Camera cam){<br />this.camera = cam;<br /> }<br />}<br />IoC<br />publicclass Container {<br />private Map<String, Object> components;<br />public Container(){<br />components = newHashMap<String, Object>();<br />StillCamerasCamera = newStillCamera();<br />components.put("stillcamera", sCamera);<br />CameraMancMan = new CameraMan(sCamera);components.put("cameraman", cMan);<br />}<br />public Object getComponent(String id){<br />returncomponents.get(id);<br />}<br />}<br />DI<br />Constructor injection<br />…<br /><bean id = “cameraman” class = “CameraMan” ><br /> <constructor-arg> <br /> <bean ref=“stillcamera"/><br /> </constructor-arg><br /> </bean><br /><bean id=“stillcamera” class=“StillCamera” /><br />…<br />Setter injection<br />Interface injection<br />Spring.xml<br />
    18. 18. Ways to implement IoC<br />publicinterfaceInjectable{<br />publicvoidinject(Map<String, Object> component);<br />}<br />IoC<br />publicclassCameraMan implementsInjectable {<br /> private Camera camera;<br /> public void inject(Map<String, Object> components){<br /> camera = (Camera) components.get(“stillcamera”);<br /> }<br />}<br />DI<br />publicclass Container {<br />private Map<String, Object> components;<br />public Container(){<br />components = newHashMap<String, Object>();<br />StillCamerasCamera = newStillCamera();<br />components.put("stillcamera", sCamera);<br />CameraMancMan = newCameraMan();<br />cMan.inject(components);<br />}<br />public Object getComponent(String id){<br />returncomponents.get(id);<br />}<br />}<br />Setter injection<br />Interface injection<br />Constructor injection<br />
    19. 19. [1] http://www.theserverside.com/news/1321158/A-beginners-guide-to-Dependency-Injection<br />Questions ?<br />

    ×