Date post: | 27-Jun-2015 |
Category: |
Technology |
Upload: | thuydang |
View: | 760 times |
Download: | 4 times |
www.exoplatform.com
Copyright 2011 eXo Platform
IoC & AOPIoC & AOP
Vu Cong ThanhVi Quoc Hanh
Social team
2www.exoplatform.com
Copyright 2011 eXo Platform
AgendaAgenda
IoC - Introduction IoC – Definition & Benefits IoC – Concepts IoC – Design your own IoC. Demo AOP - Introduction AOP - Definition & Benefits AOP - Concepts AOP - How it works? Demo
www.exoplatform.com
Copyright 2011 eXo Platform
Inversion Of Control Inversion Of Control (IoC)(IoC)
4www.exoplatform.com
Copyright 2011 eXo Platform
IoC – Introduction - “Dependencies”
5www.exoplatform.com
Copyright 2011 eXo Platform
IoC – Introduction – Dependency Problems
»Code is tightly coupled
»Difficult to isolate when testing
»Difficult to maintain.
6www.exoplatform.com
Copyright 2011 eXo Platform
IoC – Introduction - WITHOUT IoC
»Construction by Hand.
»Factory Pattern.
7www.exoplatform.com
Copyright 2011 eXo Platform
IoC – Definition & Benefits
»Hollywood principle “Don't call us, we'll call you.”
»Creating, Assembling and Wiring the dependencies into Object graph.
»Type of Dependency Injector:
– Constructor
– Setter
– Method
– Field(Guice)
8www.exoplatform.com
Copyright 2011 eXo Platform
IoC – Definition & Benefits
»Pros:
– Loosely Coupled
– Increases Tesability
– Separates component clearly.
– Allows for use of Inversion of Control Container.
»Cons:
– Increases code complexity
– Difficult to understand at First
– Complicate Debugging at First
– Complicates following Code Flow.
9www.exoplatform.com
Copyright 2011 eXo Platform
IoC – Concepts – Time launcher
»Constructed and prepared before it can be used.
– In the init lifecycle stage of a web application.
– On startup of a desktop program
10www.exoplatform.com
Copyright 2011 eXo Platform
IoC – Concepts– Time launcher
»Constructed and prepared before it can be used.
– On demand, every time it is needed
– Lazily, when it is first needed
11www.exoplatform.com
Copyright 2011 eXo Platform
IoC – Concepts – Metadata Configuration
»XML configuration file.
»Annotations(@Injector, @Singleton, …)
»Invoking into a programmatic API.
12www.exoplatform.com
Copyright 2011 eXo Platform
IoC – Concepts - Scope
Fixed duration of time or method calls in which an object exist. General-purpose scopes: Singleton and No-scope.
»Web Application:
– Request, Session and Conversation scope
– Transaction scope
13www.exoplatform.com
Copyright 2011 eXo Platform
IoC - Concepts - Benefit of Scope
»Lets injector manage the latent state of your objects
»Ensures that your services get new instances of dependencies as needed
»Implicitly separates state by context.
14www.exoplatform.com
Copyright 2011 eXo Platform
Design your own IoC
Source Code:
Guice: google-guice.googlecode.com/svn/trunk/
Vbox: https://github.com/thanhvc/etk-vbox
15www.exoplatform.com
Copyright 2011 eXo Platform
Design your own IoC – Injector Diagram
16www.exoplatform.com
Copyright 2011 eXo Platform
Design your own IoC – Context Diagram
17www.exoplatform.com
Copyright 2011 eXo Platform
Design your own IoC – State Diagram
StartConstructing Finish
States: Constructing, Start and Finishhttp://www.slideshare.net/Thuy_Dang/regular-expression-made-by-to-minh-hoang-portal-team
18www.exoplatform.com
Copyright 2011 eXo Platform
Design your own IoC – State Diagram(Cont)
www.exoplatform.com
Copyright 2011 eXo Platform
DEMODEMO
www.exoplatform.com
Copyright 2011 eXo Platform
Aspect Oriented Aspect Oriented Programming (AOP)Programming (AOP)
21www.exoplatform.com
Copyright 2011 eXo Platform
AgendaAgenda
What is IOC? IOC – Time of Injector IOC – Pros & Cons IOC - Scope Demo AOP - Introduction AOP - Definition & Benefits AOP - Concepts AOP - How it works? Demo
22www.exoplatform.com
Copyright 2011 eXo Platform
INTRODUCTION - OOP
»A set of objects that interact with each other
23www.exoplatform.com
Copyright 2011 eXo Platform
INTRODUCTION - AOP
»View the system as a set of concerns
24www.exoplatform.com
Copyright 2011 eXo Platform
INTRODUCTION – CONCERNS
»A particular role, concept or area of interest
25www.exoplatform.com
Copyright 2011 eXo Platform
INTRODUCTION - CROSSCUTTING
26www.exoplatform.com
Copyright 2011 eXo Platform
INTRODUCTION - CROSSCUTTING CONCERNS
»System level requirements that crosscut many core module level requirements
27www.exoplatform.com
Copyright 2011 eXo Platform
INTRODUCTION – CROSSCUTTING CONCERNS PROBLEMS
»Code tangling, Code scattering, Less code reuse, More difficult evolution...
»Face problem in design when requirements growth
28www.exoplatform.com
Copyright 2011 eXo Platform
AOP - DEFINITION
»Lets you implement individual concerns in a loosely coupled way and combine these implementations to form the final system.
»Complements object oriented programming.
» Is not about “patching” pieces of code. About performing an action systematically upon recognition of a behavior in the code. behavior in the code.
29www.exoplatform.com
Copyright 2011 eXo Platform
AOP - BENEFITS
»Modularized implementation of crosscutting concerns behavior in the code.
»Easier-to-evolve systems - extensibility behavior in the code.
30www.exoplatform.com
Copyright 2011 eXo Platform
AOP - BENEFITS
»Late binding of design decision behavior in the code.
»More code reuse
31www.exoplatform.com
Copyright 2011 eXo Platform
AOP - CONCEPTS
» Aspect: The merger of advice and pointcuts. All information about where, when and what job is to be done.
» Advice: The job of an aspect. Defines what and when of an aspect.
» Pointcut: Pointcuts help narrow down the joinpoints advised by an aspect. Define the where of an aspect.
» Jointpoint: A point in the execution of an application where an aspect can be
plugged in. All the opportunities for advice to be applied. behavior in the code.
32www.exoplatform.com
Copyright 2011 eXo Platform
AOP - CONCEPTS
» Target: Object being advised.
» Proxy: The object created after applying advice to the target object.
» Weaving: Process of applying aspects to a target object to created a new proxied object code.
33www.exoplatform.com
Copyright 2011 eXo Platform
AOP – HOW IT WORKS
» Decorator Pattern
» Proxy Pattern Control access to an object
Allows for object level access control by acting as a pass through entity or a placeholder object. co
d
.
34www.exoplatform.com
Copyright 2011 eXo Platform
AOP – HOW IT WORKS
35www.exoplatform.com
Copyright 2011 eXo Platform
AOP – BEHIND THE SCENCES
Aspect
Object_BObject_A
advice
Object Oriented Flow
Aspect Oriented Flow
pointcut = method_B
Target Object = Object_B
Wea
ving
jointpoint = method invocation
Object Oriented Flow
36www.exoplatform.com
Copyright 2011 eXo Platform
AOP – ENGINES & FRAMEWORKS
» JDK
» CGLIB really create new class through bytecode manipulation code.
» ASPECTJ
» JBOSS AOP
» SPRING FRAMEWORK c
…........ode.
Engines
Frameworks (Java)
37www.exoplatform.com
Copyright 2011 eXo Platform
AOP vs OOP - CONCLUSION
Object Oriented Aspect Oriented
Class – code unit that encapsulates methods and attributes.
Aspect – code unit that encapsulates pointcuts, advice, and attributes.
Method signatures – define the entry points for the execution of method bodies.
Pointcut – define the set of entry points (triggers) in which advice is executed.
Method bodies – implementations of the primary concerns.
Advice – implementations of the cross cutting concerns.
Compiler – converts source code into object code.
Weaver – instruments code (source or object) with advice.
www.exoplatform.com
Copyright 2011 eXo Platform
DEMODEMO
www.exoplatform.com
Copyright 2011 eXo Platform
Thank you!