DI, AOP, Strategies- Patterns around Spring DI, AOP, Strategies Patterns around Spring.

Slides:



Advertisements
Similar presentations
Apache Struts Technology
Advertisements

Yii – How Power Comes Introduction, OOP & Design Patterns Presented at: Nextbridge Multan Center Aug 25, 2011.
Spring, Hibernate and Web Services 13 th September 2014.
Chapter 10 THINKING IN OBJECTS 1 Object Oriented programming Instructor: Dr. Essam H. Houssein.
Component Patterns – Architecture and Applications with EJB copyright © 2001, MATHEMA AG Component Patterns Architecture and Applications with EJB JavaForum.
Road Map Introduction to object oriented programming. Classes
June 1, 2000 Object Oriented Programming in Java (95-707) Java Language Basics 1 Lecture 3 Object Oriented Programming in Java Language Basics Classes,
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
Architectural Design Principles. Outline  Architectural level of design The design of the system in terms of components and connectors and their arrangements.
Design Patterns Module Name - Object Oriented Modeling By Archana Munnangi S R Kumar Utkarsh Batwal ( ) ( ) ( )
Design Patterns academy.zariba.com 1. Lecture Content 1.What are Design Patterns? 2.Creational 3.Structural 4.Behavioral 5.Architectural 6.Design Patterns.
1 An introduction to design patterns Based on material produced by John Vlissides and Douglas C. Schmidt.
What Is a Factory Pattern?.  Factories are classes that create or construct something.  In the case of object-oriented code languages, factories construct.
A Framework for Smart Proxies and Interceptors in RMI Nuno Santos P. Marques, L. Silva CISUC, University of Coimbra, Portugal
The Spring Framework: A brief introduction to Inversion of Control James Brundege
UNIT-V The MVC architecture and Struts Framework.
REFACTORING Lecture 4. Definition Refactoring is a process of changing the internal structure of the program, not affecting its external behavior and.
Introduction to the Spring Framework By: Nigusse A. Duguma Kansas State university Department of Computer Science Nov 20, 2007.
Spring Overview, Application demo -Midhila Paineni 09/23/2011 Spring Overview, Application demo9/8/20151.
SWE 316: Software Design and Architecture – Dr. Khalid Aljasser Objectives Lecture 11 : Frameworks SWE 316: Software Design and Architecture  To understand.
An Introduction to Software Architecture
Design patterns. What is a design pattern? Christopher Alexander: «The pattern describes a problem which again and again occurs in the work, as well as.
EJB Framework.  As we know, EJB is the center of the J2EE architecture that provides a sturdy framework for building enterprise applications. The major.
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.
An Introduction to Design Patterns. Introduction Promote reuse. Use the experiences of software developers. A shared library/lingo used by developers.
SAMANVITHA RAMAYANAM 18 TH FEBRUARY 2010 CPE 691 LAYERED APPLICATION.
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.
An Ontological Framework for Web Service Processes By Claus Pahl and Ronan Barrett.
A Presentation By V AIBHAV S AHARAN Web-enHanced Information Management COMS E6125.
Chapter 8 Object Design Reuse and Patterns. Object Design Object design is the process of adding details to the requirements analysis and making implementation.
Design Patterns Gang Qian Department of Computer Science University of Central Oklahoma.
Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. Introduction to Spring Remoting Simplifying.
Design Principle & Patterns by A.Surasit Samaisut Copyrights : All Rights Reserved.
Distribution and components. 2 What is the problem? Enterprise computing is Large scale & complex: It supports large scale and complex organisations Spanning.
Programming in Java CSCI-2220 Object Oriented Programming.
Object Oriented Software Development
Design Patterns -- Omkar. Introduction  When do we use design patterns  Uses of design patterns  Classification of design patterns  Creational design.
Introducing Allors Applications, Tools & Platform.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
 Objects versus Class  Three main concepts of OOP ◦ Encapsulation ◦ Inheritance ◦ Polymorphism  Method ◦ Parameterized ◦ Value-Returning.
Java EE Patterns Dan Bugariu.  What is Java EE ?  What is a Pattern ?
Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. Spring MVC Essentials Getting started.
Design Patterns Software Engineering CS 561. Last Time Introduced design patterns Abstraction-Occurrence General Hierarchy Player-Role.
Spring and DWR Frameworks for Rich Web Enterprise Application Thomas Wiradikusuma Presentation to the 20 th.
© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/3 JSP Application Models.
Kansas City Java User’s Group Jason W. Bedell July 12, 2006
问题 Code scattering Blocks of duplicated code Blocks of complementary code, and different modules implementing complementary parts of the concern Code.
Component Patterns – Architecture and Applications with EJB copyright © 2001, MATHEMA AG Component Patterns Architecture and Applications with EJB Markus.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
Basic Concepts and Definitions
Text TCS INTERNAL Oracle PL/SQL – Introduction. TCS INTERNAL PL SQL Introduction PLSQL means Procedural Language extension of SQL. PLSQL is a database.
Author: DoanNX Time: 45’.  OOP concepts  OOP in Java.
Kyung Hee University Class Diagramming Notation OOSD 담당조교 석사과정 이정환.
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
L’origine dei mali: le dipendenze tra componenti Stefano Leli 14° Workshop DotNetMarche Venerdì 16 aprile
CLASSIFICATION OF DESIGN PATTERNS Hladchuk Maksym.
Leveraging ColdSpring to build a robust Flex applications Chris Scott, Cynergy Systems.
Callbacks and Interceptors. Contents  Session Beans Life Cycle  Interceptors.
Unit - 3 OBJECT ORIENTED DESIGN PROCESS AND AXIOMS
Chapter 5:Design Patterns
MPCS – Advanced java Programming
Introduction to Design Patterns
Inheritance and Polymorphism
Behavioral Design Patterns
Magento Technical Guidelines Eugene Shakhsuvarov, Software Magento
JAsCo an Aspect-Oriented approach tailored for
An Introduction to Software Architecture
SAMANVITHA RAMAYANAM 18TH FEBRUARY 2010 CPE 691
Designing For Testability
Aspect Oriented Programming
Presentation transcript:

DI, AOP, Strategies- Patterns around Spring DI, AOP, Strategies Patterns around Spring

DI, AOP, Strategies- Patterns around Spring What is not Covered? This is NOT a session on the Spring Framework itself just on the patterns around it such as Dependency Injection and AOP.

DI, AOP, Strategies- Patterns around Spring Contents  The drift towards Lightweight Architecture to address enterprise concerns.  SRP, DRY and Layered architectures.  Contexts & Strategies.  Class dependencies.  Avoiding Dependencies.  Dependency Injection  Horizontal Concerns  AOP – Definition, Patterns & Techniques. What is not covered?  Coding details – Only conceptual details and patterns are covered.  Spring offshoots such as transactions, Spring MVC, JMX, DAO framework etc.

DI, AOP, Strategies- Patterns around Spring Heavyweight Architectures  Involvement of a container for creating, destroying and executing components.  Tight coupling of horizontal concerns with container services. Ex: Transactions, Caching, Persistence, Remoting  Tight coupling with container interfaces and exception handling.  Emphasis on implementations rather than interfaces.

DI, AOP, Strategies- Patterns around Spring Lightweight Architectures  Emphasis on Core Object Oriented Analysis & Design rather than implementations.  Highly testable designs.  Containers not required for testing or executing the code.  Implementations distributable as a library (jar file) as opposed to a complicated artifact that requires a hosting container (such as an ejb-jar, a war or ear file)  Core classes and components should not have any awareness of the fact that they are being deployed by a container.  A layered architecture that would scale to increasing complexity along with the application.

DI, AOP, Strategies- Patterns around Spring Sample Application

DI, AOP, Strategies- Patterns around Spring SRP, DRY & Layered Architectures.  The Single Responsibility Principle (SRP) states that one class should only implement one responsibility. Avoids brittleness since the class should only change due to one reason.  The opposite of the SRP is also true – i.e. one concern should be implemented by only one class. This is the principle called “DRY” (Don’t Repeat Yourself)  SRP and DRY lead to highly flexible and reusable application designs.  Applications are layered with one layer communicating to the next. Layering is a form of information hiding extended to embrace a group of classes rather than a single class. Layers communicate with each other through the use of interfaces.  A group of concerns are implemented by a layer.  The SRP and DRY applied to layers mandate that each layer should address ONE and ONLY ONE set of concerns.

DI, AOP, Strategies- Patterns around Spring Contexts & Strategies Viewed in this context, all classes can be divided broadly into the following:

DI, AOP, Strategies- Patterns around Spring Contexts  These are objects that carry state from one layer to another.  This category includes both Data Transfer Objects and Domain Objects in a system. (Example: Account, Customer, HttpServletRequest, LoginRequest or SearchCriteria )  Contain Information about the domain or software system – a bunch of fields with getters and setters.  Need to be instantiated for every request.  May contain methods that enable certain operations. For instance typically would have an implementation for the equals(), compareTo() and hashCode() methods.  Cannot contain operations specific to a layer since these transcend layers. For instance, cannot have a writeToDatabase() method.

DI, AOP, Strategies- Patterns around Spring Contexts - Some Features  Contexts are lightweight since they don’t contain operations and hence have no layering dependencies. (Ex: They don’t need a database connection)  They implement interfaces that deal with identity, ordering and govern their ability to be serialized across the wires but don’t need any interface encapsulation for their functionality. (They have no functionality except to store state)  Context objects can have dependencies on other context objects to form complex object graphs. Ex: Account object may have a customer object.

DI, AOP, Strategies- Patterns around Spring Strategies  Implementation of the GoF Strategy pattern.  Encapsulate a family of algorithms in a separate class. The choice of a particular algorithm is made by choosing the right strategy.  Strategies require a context to act on.

DI, AOP, Strategies- Patterns around Spring Strategies - Examples  These are the providers of the services.  These exist in various layers from presentation to business tiers.  In presentation tier, these act as controllers.  In Business tier, these are POJOs capturing business rules and logic.

DI, AOP, Strategies- Patterns around Spring Strategies & Dependencies  The SRP and DRY applied to strategies mandate that each strategy implement one and only one concern.  Since an application caters to multiple concerns, this implies that there are multiple strategies in an application with each dependent on the other.  Application can be conceived as a chain of strategies.  Optimal way to create Strategies - Model them as flyweights or singletons. These need to be coded as stateless for this modeling to work.  The above approach is called the “stateless singleton” model - This is the prevalent approach in the design of most applications today.

DI, AOP, Strategies- Patterns around Spring Strategies & Interface Based Design.  Strategy pattern requires alternate implementations of the same strategy to have a common ancestor. The dependent class uses the ancestor’s interface to avoid direct dependence on a particular implementation of the strategy.  In its purest form, the ancestor will be an interface. To that extent, strategy pattern works well with interface based design. (where objects are dependent on interfaces and not implementations)

DI, AOP, Strategies- Patterns around Spring Unit Testing & Interface Based Design  A popular unit testing strategy is to use mock objects for testing.  Hence for every strategy to be “mock”able, there needs to be an interface.  Strict compliance to Interface based design boosts “test”ability.

DI, AOP, Strategies- Patterns around Spring Why is Object dependency bad?  Dependencies are transitive. Hence an object that has a chain of dependencies, cannot be tested without the entire chain being materialized.  If the dependent object is utilizing a resource such as database, then the dependent object cannot be materialized without a database. This would hamper testing of the object.  An object that relies on another object may perform functionality that is not restricted to the dependent object. For instance, an object that works with a persistent store could work whether the persistent store utilizes an underlying database or an XML file. The re-use is restricted if the object “hard-codes” the persistent store to one implementation. We may have to re-write this object in its entire form to work with a persistent store that works with XML. This statement can be restated as follows: If an alternate implementation exists for a dependency, it is not possible to swap out one implementation with the other.

DI, AOP, Strategies- Patterns around Spring Different kinds of dependencies  Multiple kinds of dependencies can exist between objects. The code below illustrates them: 1 public class MyDao extends GenericDao { 2 public static final String datasourceName = “jdbc/ds”; 3 Datasource datasource = null; 4 UserSQLGenerator sqlgenerator = null; 5 public MyDao(){ 6 datasource = ServiceLocator.getDatasource(datasourceName); 7 sqlgenerator = new UserSQLGenerator(); 8 } 9 public User getUserFromLogin(String login) throws DaoException 10 // Remove spaces, trim and convert the login to lowercase 11 String normalizedLogin = StringUtils.normalize(login); 12 System.out.println(“Normalized Login is “ + normalizedLogin); } 15 }

DI, AOP, Strategies- Patterns around Spring Types Of Dependencies (Contd.)  The code illustrates the following kinds of dependencies: 1. Extension dependency. GenericDao is extended by MyDao. Hence MyDao depends on GenericDao. (line#1) 2. Field Type dependency – The field datasource is of type Datasource. (line#2) 3. Instantiation dependency – The class MyDao constructs an instance of the class SQLGenerator in its constructor (line#7). 4. Parameter type dependency – The class MyDao accepts parameter of type String in its getUserFromLogin() method. (line#9) 5. Return type - Method getUserFomLogin() returns object of type User( line #9) 6. Exception type - Method getUserFromLogin() throws an exception of type DaoException (line#9) 7. Local variable type – The local variable normalizedLogin is of type String.(#11). 8. Static field or method invocation – StringUtils normalize() method is statically invoked.(line#11). The static field “out” of System class is invoked (line#12)

DI, AOP, Strategies- Patterns around Spring Avoiding Dependencies - Composition  The extension dependency should be introduced with caution. Many times whatever can be achieved using extension can be achieved equally effectively and more flexibly using composition. This is especially true for Command objects which don’t have many fields and have stateless methods. Composition is such an accepted technique that it has been formalized into the “strategy” pattern. For example: public class Command1 { public void m1(){} } public class Command2 { Command1 command1; public void m1(){ command1.m1(); } }  Thus in trying to avoid extension we have made the dependency a field type dependency.

DI, AOP, Strategies- Patterns around Spring Avoiding Dependencies – Interface Based Design  Field type, parameter type and local variable type dependencies can be addressed by interface based design.  The problem with these dependencies is that the dependent classes are tied to the concrete implementations thereby making them un-testable and limited in their scope.  The solution is to make a new interface for the dependency.  All usages of the class would now be replaced by the interface.  The advantage is that the class can utilize any implementation of the interface.  The same technique can be used for local variable types and the parameter types.

DI, AOP, Strategies- Patterns around Spring Avoiding Dependencies – Static Dependencies  Static field or method dependencies typically should not pose problems if the static methods are used strictly for utility purposes.  Inappropriate usage of static methods and fields can introduce all the dependency problems discussed before.  For instance, the class below is commonly found in many Java projects: public class PropertyConfigurator { private static Properties properties; static { // read properties from some config files. } public static String getPropertyValue(String name) { return properties.get(name); }}

DI, AOP, Strategies- Patterns around Spring Avoiding Dependencies – Static Dependencies (Contd.)  The problem with this approach is that static methods are used from a class to take care of a “strategy” such as properties configuration.  This poses problems if the strategy needs to be replaced at a future time. Example: How do we obtain properties from a database instead of from a properties file?  The above problem would necessitate us to change the PropertiesConfigurator class. This in turn may pose other problems. For instance, what if some classes want to obtain properties from a database and others want to obtain properties from a properties file?  This problem arises due to the fact that static methods are often used when a “singleton” behavior is desired.  Moral of the story: Do not confuse statics with singletons!!!

DI, AOP, Strategies- Patterns around Spring Avoiding Dependencies – Static Dependencies (Contd.)  The class can in fact be written using instance methods as public class PropertyConfigurator implements IPropertyConfigurator { private Properties properties; PropertyConfigurator(/* Accept arguments for initialization if reqd. */) { // read properties from the initialization args. } public String getPropertyValue(String name) { return properties.get(name); } } public interface IPropertyConfigurator{ public String getPropertyValue(String name); } Using this approach dependent classes would now use the IProperyConfigurator interface.

DI, AOP, Strategies- Patterns around Spring Avoiding Dependencies – Static Dependencies (Contd.)  The static dependency would become a field type dependency.  However, static methods have their merits for utility purposes.  The methods for instance in java.lang.Math class are valid utility methods.  From all this discussion, it is seen that the Strategy pattern and Interface based design principle can in effect get rid of most dependencies – most but not all dependencies.  The biggest problem that remains is the Initialization dependency which we would see next.

DI, AOP, Strategies- Patterns around Spring Avoiding dependencies – The Instantiation problem  Instantiation is where the rubber hits the road. We have to pick an implementation to instantiate for a given environment.  Direct instantiation with the new operator is EVIL.  The Factory pattern or Service Locator pattern can help to instantiate the correct dependency for a given context. But the problem is that there may be multiple factories involved. These multiple factories should work in tandem with each other for various environments.  A good solution is to fuse all these factories together and form one giant factory. This “bean factory” would be responsible for the instantiation of the entire chain of dependencies.  The bean factory could be used by all these classes to instantiate dependent classes. This is shown in the following listing. Everything is stored in the bean factory and each one of these classes can contact the bean factory for its dependency.  If the implementation for UserService needs to be switched from UserServiceImpl to MockUserService for instance, we need to change the BeanFactory not the actual class.

DI, AOP, Strategies- Patterns around Spring Instantiation problem – bean factory public class UserDaoImpl implements UserDao { } public class UserServiceImpl implements UserService { // get hold of the bean factory somehow. BeanFactory beanFactory =.. ; public void doSomething(){ // get the dao from the bean factory. UserDao dao = beanFactory.getBean(“dao”); // use the dao to do something… } } public class UserAction implements Action { BeanFactory beanFactory = …; public void execute(){ UserService service = beanFactory.getBean(“service”); } }

DI, AOP, Strategies- Patterns around Spring Bean Factory (contd.)  Each class needs to be coded to include the Bean factory as a dependency.  This is less evil than coding for the actual dependency but it is still an annoyance.  The next “Eureka” moment arrived when it was realized that since the bean factory knows how to make different types of beans, it should also know each bean’s dependencies.  So let the bean factory provide the dependency to the class when it is creating it. Hence the Service class would be provided with the Dao and the Action class would be provided with the Service class.  This is an application of the principle called “Dependency Injection”.  The individual classes do not need to be coded anymore to do any more instantiation. But they do have to facilitate dependency injection.

DI, AOP, Strategies- Patterns around Spring Dependency Injection - Introduction  Dependency Injection principle allows a class’s dependencies to be injected into it prior to the class being used.  The class that needs to be injected should enable DI.  This is possible in one of three ways: Constructor based injection where the dependencies are injected using the constructor. Setter based injection where a setter method is used to inject the dependencies. Interface based injection where a special method in an interface is used to inject dependencies. These interfaces are typically called “aware interfaces”.

DI, AOP, Strategies- Patterns around Spring Dependency Injection - Key concepts.  Classes supporting DI have the following stages in their lifecycle. Instantiation Phase - when the class is instantiated from a factory or using the new operator and then injected with its dependencies. Runtime Phase - when the class is being used by calling its stateless methods. Destruction Phase - When the resources utilized by the class are closed or recycled for later use.  Spring had its modest origins as a Bean Factory that facilitated dependency injection.

DI, AOP, Strategies- Patterns around Spring Introduction to Spring Bean Factory  It has now bloomed to a full application framework.  The spring bean factory supports constructor and setter based injections.  It also provides for various ways of instantiating dependencies. (Using the new operator, using static factory methods and instance factory methods with any number of arguments)  Setter based injection can be “auto-wired” by name or by type.

DI, AOP, Strategies- Patterns around Spring Horizontal Concerns - Introduction  Horizontal concerns are directly related to non- functional requirements.  They touch every aspect of the program without being an end in themselves.  Examples are auditing, logging, security, persistence, transactions etc.  Horizontal concerns pervade through the application’s different layers and need to be incorporated by every one of the layers.

DI, AOP, Strategies- Patterns around Spring Horizontal Concerns - SRP and DRY  Horizontal concerns complicate the application since these concerns need to pervade through different layers of the application. This means that every class should be aware of horizontal concerns and implement them thereby violating the SRP.  Typically utility classes have been written to implement the horizontal concerns. But every class still has to call the utility class and pass the relevant parameters for it to implement this concern satisfactorily.  As concerns get added or deleted, every class still has to change. All the classes need to change even if the parameters for the methods change.  The code calling the utility classes is replicated in every other class. Hence this code violates DRY. Furthermore, it is subject to brittleness as it has to change with change in concerns.  This makes the application brittle.

DI, AOP, Strategies- Patterns around Spring Aspect Oriented Programming (AOP) – Some Techniques  Aspects are concerns that can be woven into the application without the individual classes incorporating them explicitly. Aspects hence become good candidates for the implementation of horizontal concerns.  AOP is implemented using a standard pattern of method interception. Upon every method invocation, the aspect has the ability to do some pre processing before calling the underlying class’s method. It can do post processing after the method call completes. The aspect also can optionally choose to not pass control to the underlying method.

DI, AOP, Strategies- Patterns around Spring Aspect Oriented Programming (AOP) – Some Techniques (Contd.)  The figure below illustrates the AOP flow:

DI, AOP, Strategies- Patterns around Spring AOP - Pseudo Code public class Aspect { private Object underlyingobject ; public void intercept(Object[] argspassed ) { if (based on argspassed I should not pass control to the class) { Return or throw exception. } // Do pre processing. // Underlyingobject.callUnderlyingMethod and store return value. //Do post processing. // Return or throw exception. } }

DI, AOP, Strategies- Patterns around Spring AOP - Patterns  Many method interception patterns support this interweaving that was illustrated in the previous slides.  Decorator Decorate an object with another but preserve its interface. This way instead of the object the decorator is given to a dependent class. All the methods in the underlying class can get intercepted by the decorator.  Filters or Interceptors This pattern is most useful if there is a chain of commands that have been linked into a flow. The interceptor or filter is introduced into the flow. This can intercept the method call in the flow.  Runtime Proxy A class can serve as a proxy for another class. The proxy acts like the actual class and behaves in pretty much the same way as the decorator does.

DI, AOP, Strategies- Patterns around Spring AOP – Patterns (Contd.)  Code Generation Code can be generated using either pre-processing or post processing (byte code enhancement) to incorporate aspects into compiled classes. Code generators generate sub-classes for the actual classes. These sub-classes intercept method calls and call the super() implementation after they do pre and post processing.

DI, AOP, Strategies- Patterns around Spring AOP - Implementation  The patterns that are used to implement AOP involve method interception as we have seen.  They also work only if the dependent classes are provided a “decorated” version of the dependency class.  Hence Dependency Injection is a great candidate to implement AOP.  The Spring container utilizes this technique and came up with “enhanced” versions of the dependencies when they are being injected into the dependent class.  Spring uses JDK 1.3 Interface proxy and CGLIB for construction of dynamic proxies.  Spring also supports AOP “pointcuts”. A pointcut restricts the AOP interceptors to be applied only to certain methods in certain classes. The pointcut is defined using an expression that has wild cards in it.

DI, AOP, Strategies- Patterns around Spring Conclusion  Lightweight technologies are taking the world by storm.  Objects can be either context objects or strategy objects.  Object dependencies among Strategy objects are undesirable as they hamper testing and re-usability of objects.  The following techniques are used to combat object dependencies: Interface based design. Strategy pattern. Dependency Injection.  Dependency Injection is powerful for achieving Loose coupling. Separating the object design from the command orchestration.  AOP is a powerful way of implementing horizontal concerns.  DI facilitates AOP.

DI, AOP, Strategies- Patterns around Spring Thank You