Tuesday Brown Bag Inversion of Control with Howard Abrams

Slides:



Advertisements
Similar presentations
COM vs. CORBA.
Advertisements

Snejina Lazarova Senior QA Engineer, Team Lead CRMTeam Dimo Mitev Senior QA Engineer, Team Lead SystemIntegrationTeam Telerik QA Academy SOAP-based Web.
Spring, Hibernate and Web Services 13 th September 2014.
9.5 Software Architecture
Pragmatic Application Building: Step by Step Jay Sissom Principal Systems Analyst Indiana University
Terms and Rules Professor Evan Korth New York University (All rights reserved)
1 Web Applications – Design Issues. 2 Data Requirements –Persistent –Consistent (no corruption even if server fails) –Concurrent updates –Transactions.
Chapter 25 More Design Patterns.
The Spring Framework: A brief introduction to Inversion of Control James Brundege
UNIT-V The MVC architecture and Struts Framework.
 1. Introduction  2. Development Life-Cycle  3. Current Component Technologies  4. Component Quality Assurance  5. Advantages and Disadvantages.
M. Taimoor Khan * Java Server Pages (JSP) is a server-side programming technology that enables the creation of dynamic,
Java Beans.
Spring Overview, Application demo -Midhila Paineni 09/23/2011 Spring Overview, Application demo9/8/20151.
Using Runtime Information for Adapting Enterprise Java Beans Application Servers Mircea Trofin *, John Murphy ** Performance Engineering Laboratory * DCU,
Basics of Web Databases With the advent of Web database technology, Web pages are no longer static, but dynamic with connection to a back-end database.
Copyright © 2012 Accenture All Rights Reserved.Copyright © 2012 Accenture All Rights Reserved. Accenture, its logo, and High Performance Delivered are.
ASP.NET and Model View Control Jesper Tørresø ITNET2 F08.
Todd Snyder Development Team Lead Infragistics Experience Design Group.
Spring core v3.x Prepared by: Nhan Le. History v3.0 Spring Expression Language Java based bean metadata v3.1 Cache Abstraction Bean Definition Profile.
CSC 142 B 1 CSC 142 Java objects: a first view [Reading: chapters 1 & 2]
PicoContainer Presented by: Jim O’Hara Ed Kausmeyer Jingming Zhang.
Last update October 18, 2004 Advanced Programming 2004 Java Beans.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
JavaBeans Components. To understand JavaBeans…  Proficient experience with the Java language required  Knowledge of classes and interfaces  Object-Oriented.
DEPENDENCY INJECTION & INVERSION OF CONTROL. WHAT’S GOING TO BE COVERED Quick intro to C# for Java developers Dependency Injection Inversion of Control.
Spring Framework. Spring Overview Spring is an open source layered Java/J2EE application framework Created by Rod Johnson Based on book “Expert one-on-one.
Introduction to Spring Matt Wheeler. Notes This is a training NOT a presentation Please ask questions Prerequisites – Introduction to Java Stack – Basic.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 2.
Comparing JavaBeans and OSGi Towards an Integration of Two Complementary Component Models HUMBERTO CERVANTES JEAN-MARIE FAVRE 09/02.
WEB BASED DATA TRANSFORMATION USING XML, JAVA Group members: Darius Balarashti & Matt Smith.
Ch 2 – Application Assembly and Deployment COSC 617 Jeff Schmitt September 14, 2006.
Copyright© , The Seasar Foundation and the others. All rights reserved. 1 Configuration files must Die!!! Yasuo Higa The Seasar Foundation/Chief.
Introduction to Component-Based Engineering Howard Abrams
Programming in Java CSCI-2220 Object Oriented Programming.
Alternative Architectures: Inversion of Control Mike Hadlow mikehadlow.blogspot.com.
Introduction to Spring Web Flow Andrew Petro Software Developer Unicon, Inc. Jasig 2011 Westminster, CO 23 May 2011 © Copyright Unicon, Inc., Some.
Cairngorm Microarchitecture. Pronunciation Cairngorm (kârn gôrm) n. yellowish-brown variety of quartz, especially found in Scottish Cairngorm mountain.
MCS 270 Spring 2014 Object-Oriented Software Development.
Introducing Allors Applications, Tools & Platform.
Simple Classes. ADTs A specification for a real world data item –defines types and valid ranges –defines valid operations on the data. Specification is.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Java EE - Dependency Injection -
Design Patterns SE464 Derek Rayside images from NetObjectives.com & Wikipedia.
Plug-in Architectures Presented by Truc Nguyen. What’s a plug-in? “a type of program that tightly integrates with a larger application to add a special.
Dependency Injection Frameworks Technion – Institute of Technology Author: Assaf Israel - Technion 2013 ©
(1) ICS 313: Programming Language Theory Chapter 11: Abstract Data Types (Data Abstraction)
1 Java Server Pages A Java Server Page is a file consisting of HTML or XML markup into which special tags and code blocks are inserted When the page is.
APACHE STRUTS ASHISH SINGH TOMAR ast2124. OUTLINE Introduction The Model-View-Controller Design Pattern Struts’ implementation of the MVC Pattern Additional.
L’origine dei mali: le dipendenze tra componenti Stefano Leli 14° Workshop DotNetMarche Venerdì 16 aprile
Dependency Injection with Guice Technion – Institute of Technology Author: Gal Lalouche - Technion 2016 ©
Apache Struts Technology A MVC Framework for Java Web Applications.
Introduction to Inversion Of Control (IOC). IOC Definition (based on Wikipedia)  Consider the way in which an object obtains references to its dependencies.
CS520 Web Programming Spring – Inversion of Control Chengyu Sun California State University, Los Angeles.
Design Patterns: MORE Examples
Abstract Factory Pattern
Java Servlets By: Tejashri Udavant..
Design Patterns C++ Java C#.
CS520 Web Programming Spring – Inversion of Control
Java Beans Sagun Dhakhwa.
Design Patterns C++ Java C#.
Distribution and components
Data Modeling II XML Schema & JAXB Marc Dumontier May 4, 2004
Abstract Factory Pattern
Intent (Thanks to Jim Fawcett for the slides)
Simple Classes in Java CSCI 392 Classes – Part 1.
Developing and testing enterprise Java applications
Towards Integrating Java EE into ProtoCom
Leveraging ColdSpring To Make Better Applications
Designing For Testability
Presentation transcript:

Tuesday Brown Bag Inversion of Control with Howard Abrams Or why the Factory Pattern is Bad 1/17/2019

Presentation Contents Theory behind Inversion of Control What it is and why you should use it How IoC has been implemented: Pico and Nano Containers Spring Framework Google’s Guice Ways to role yer own with scripts Alternatives 1/17/2019

OOP Failure Objects are seldom reusable Objects usually have direct dependencies Unit testing some times involves starting other objects … or even the entire application! Fault of “object normalization” Objects often can’t be replaced even if both versions adhere to the same interface 1/17/2019

The Problem Separation of code through an API Client is dependent on implementation Difficult to unit test the client without server Client Server Aserver s = new Aserver(); s.doIt(); public class Aserver { public doIt() { ... } 1/17/2019

Use an Interface Client works through an Interface Client is still dependent on implementation Still can’t unit test the client without server Client Server Interface Server public class Aserver implements Iserver { public doIt() { ... } Iserver s = new Aserver(); s.doIt(); interface Iserver { public doIt(); } 1/17/2019

Factory Pattern Create a Factory class Mediates between the client and server Can test the client with mock objects public class ServerFactory { boolean testing = false; public static Iserver getInstance() { if (testing) return new MockServer(); else return new Aserver(); } 1/17/2019

Inversion of Control Factory works for simple situations IoC works better for large applications Client does not retrieve a server instance Client is given a server instance public class Client { Iserver server; public void setServer (Iserver s) { this.server = s; } ... It is now clear from the Client’s API what the client code depends on. IoC is also called Dependency Injection Also called the Hollywood Principle or "Don't call us we'll call you”, since the client now does not reach out to find herself a service but instead is provided one 1/17/2019

IoC Package Layout Client ServerInterface Server ... Iserver server; void setServer( Iserver s) { server = s; } public class Aserver implements Iserver { public doIt() { ... } interface Iserver { public doIt(); } Unit Tests MockServer void testClient { Client client = new Client(); Iserver ts = new MockSvr(); Client.setServer(ts); ... } public class MockSvr implements Iserver { public doIt() { ... } 1/17/2019

Hooking up Client / Server Can simply create a binding class Dependency is now higher (application level) All bindings need to be explicit However, some bindings are obvious A client could require a MailServer instance, but if only one class that implements the interface is given, why require an explicit binding? Could create a client factory, but now you have same amount of code without IoC 1/17/2019

IoC Container Frameworks Build applications by binding independent components together Components are POJOs (esp. JavaBeans) Each framework binds them differently: Bindings using special Java code Specify bindings with XML or scripting lang Describe bindings with Java annotations 1/17/2019

Why use IoC Framworks? Modularize how dependencies between parts of your application are laced up To improve the testability of your code To improve component configuration Less code due to automatic binding Components have a chance at being reused It is common having the dependencies of your application components/modules scattered all over. 1/17/2019

Pico Container Uses Java code to do the bindings: The application goes through container: MutablePicoContainer pico = new DefaultPicoContainer(); pico.registerComponentImplementation(Client.class); pico.registerComponentImplementation(Interface.class, Server.class); Client c = (Client) pico.getComponentInstance(Client.class); See www.picocontainer.org 1/17/2019

See nanocontainer.codehaus.org Builds on top of Pico Container Component binding is not compiled: Done by class name (using reflection) Done by scripting languages (Groovy, Ruby) builder = new org.nanocontainer.script.groovy.NanoContainerBuilder() nano = builder.container { component(class:Girl) component(class:Boy) } See nanocontainer.codehaus.org 1/17/2019

Spring Framework Component bindings (and configuration) done using XML: <beans> <bean id=”myclient” class=”Client"> <property name="username" value="someone" /> </bean> <bean id=”myserver” class=”Server”/> </beans> http://www.theserverside.com/tt/articles/article.tss?l=SpringFramework http://www.theserverside.com/tt/articles/content/RodJohnsonInterview/JohnsonChapter4.pdf See www.springframework.org 1/17/2019

Google’s Guice Component bindings done via annotations: Default implementations of an Interface: public class Client { private final IServer service; @Inject public Client(IServer service) { this.service = service; } Annotations make it dependent on Java 5+ @ImplementedBy(ServiceImpl.class) public interface IServer { ... } See http://code.google.com/p/google-guice/ 1/17/2019

IoC Container Comparison Spring is the most complete / mature The XML file becomes unwieldy Has great web application solution Guice is young, but promising Requires Java 5 All code is in Java, however Nano’s scripting capabilities intrigue me Pico is only IoC and component lifecycle These are my own perspectives of writing quite a few Spring apps, but not using any of the others much at all. Spring is in the process of retiring the XML file for annontations as well (http://blog.interface21.com/main/2006/11/28/a-java-configuration-option-for-spring/) http://code.google.com/p/google-guice/wiki/SpringComparison 1/17/2019

Roll Yer Own - Scripts Bind components in scripts with Java 6 Component instantiation in one file, and bindings are in another (the script) ScriptEngineManager m = new ScriptEngineManager(); ScriptEngine engine = m.getEngineByName("jruby"); engine.getContext().setAttribute( ”client", new Client() ); engine.getContext().setAttribute( ”server", new Server() ); engine.eval(bindingScript, context); $client.setServer ( $server ); ... $client.doIt(); 1/17/2019

Other Options Plugin interfaces (built with Reflection) Components are built against a particular API Service Locator (e.g. JNDI) Built against API Distributed Configuration/deployment can be a nightmare JINI Really only makes sense if it is distributed 1/17/2019