SlideShare a Scribd company logo
HÖNNUN OG SMÍÐI HUGBÚNAÐAR 2015
L07 FRAMEWORKS
Agenda
Why frameworks?
Framework patterns
•Inversion of Control and Dependency Injection
•Template Method
•Strategy
From problems to patterns
•Game Framework
Spring framework
•Bean containers
•BeanFactory and ApplicationContext
Reading
Dependency Injection
Template Method Pattern
Strategy Pattern
Spring Framework (video)
Article by Fowler
Inversion of Control Containers and the
Dependency Injection pattern
Resources
▪ Spring Framework homepage
– http://www.springframework.org
▪ Reference Documentation
– http://www.springframework.org/docs/reference/index.html
– Also in PDF format
Why Frameworks?
Why use Frameworks?
▪ Frameworks can increase productivity
– We can create our own framework
– We can use some third party framework
▪ Frameworks implement general functionality
– We use the framework to implement our business logic
Framework design
▪ Inheritance of framework classes
▪ Composition of framework classes
▪ Implementation of framework interfaces
▪ Dependency Injection
?Your
Domain Code
Framework
Using Frameworks
▪ Frameworks are concrete, not abstract
– Design patterns are conceptual, frameworks provide
building blocks
▪ Frameworks are higher-level
– Built on design patterns
▪ Frameworks are usually general or technology-specific
▪ Good frameworks are simple to use, yet powerful
Abstractions
▪ From API to Frameworks
API Definition JEE/.NET API
API Patterns JEE/.NET
Patterns
Framework Spring
Open Source Frameworks
▪ Web Frameworks
– Jakarta Struts, WebWork, Maverick, Play!
▪ Database Frameworks
– Hibernate, JDO, TopLink
▪ General Framework
– Spring, Expresso, PicoContainer, Avalon
▪ Platform Frameworks
– JEE
Where do Frameworks Come From?
▪ Who spends their time writing frameworks?
▪ If they give them away, how can anyone make money?
▪ Companies that use frameworks, have their developers work
on them
▪ Give the code, sell the training and consulting
Write	
  down	
  the	
  pros	
  and	
  cons	
  (benefits	
  and	
  drawbacks)	
  for	
  frameworks.	
  
Use	
  two	
  columns,	
  benefits	
  on	
  the	
  left,	
  drawbacks	
  right
EXERCISE
Pros and Cons
▪ Pros
– Productivity
– Well know application
models and patterns
– Tested functionality
– Connection of different
components
– Use of open standards
▪ Cons
– Can be complicated,
learning curve
– Dependant on frameworks,
difficult to change
– Difficult to debug and find
bugs
– Performance problems can
be difficult
– Can be bought by an evil
company
Framework Patterns
Separation of Concerns
▪ One of the main challenge of frameworks is to provide
separation of concerns
– Frameworks deal with generic functionality
– Layers of code
▪ Frameworks need patterns to combine generic and domain
specific functionality
The Hollywood Principle
▪ “Don’t call us, we’ll call you”
▪ Your program does not call the framework, it’s the framework
that controls the execution of your program
TRADITIONAL HOLLYWOOD CALL
Framework
Handler Handler
Framework
Program Program
Inversion of Control (IoC)
▪ Your application runs in a container (framework)
▪ Container manages the life-cycle of your object and provides
context
▪ The framework has the control
Framework Patterns
▪ Useful patterns when building a framework:
– Dependency Injection: remove dependencies by injecting
them (sometimes called Inversion of Control)
– Template Method: extend a generic class and provide
specific functionality
– Strategy: Implement an interface to provide specific
functionality
Dependency Injection
Removes explicit dependence on specific application code by
injecting depending classes into the framework
▪ Objects and interfaces are injected into the classes that to
the work
▪ Two types of injection
▪ Setter injection: using set methods
▪ Constructor injection: using constructors
▪ Fowler’s Naive Example
– MovieLister uses a finder class
– How can we separate the finder functionality?
class MovieLister...
public Movie[] moviesDirectedBy(String arg) {
List allMovies = finder.findAll();
for (Iterator it = allMovies.iterator(); it.hasNext();) {
Movie movie = (Movie) it.next();
if (!movie.getDirector().equals(arg)) it.remove();
}
return (Movie[])allMovies.toArray(new Movie[allMovies.size()]);
}
REMEMBER PROGRAM TO INTERFACES PRINCIPLE?
Dependency Injection
Separate	
  what	
  varies
▪ Fowler’s Naive Example
– Let’s make an interface, MovieFinder
– MovieLister is still dependent on particular MovieFinder
implementation
public interface MovieFinder {
List findAll();
}
class MovieLister...
private MovieFinder finder;
public MovieLister() {
finder = new MovieFinderImpl("movies1.txt");
}
Argh!	
  
Not	
  cool.
Dependency Injection
▪ An assembler (or container) is used to create an implementation
– Using constructor injection, the assembler will create a
MovieLister and passing a MovieFinder interface in the
contractor
– Using setter injection, the assembler will create

MovieLister and then all the setFinder setter 

method to provide the

MovieFinder interface
Dependency Injection
▪ Example setter injection
class MovieLister...
private MovieFinder finder;
public void setFinder(MovieFinder finder) {
this.finder = finder;
}
class MovieFinderImpl...
public void setFilename(String filename)
this.filename = filename;
}
Dependency Injection
Dependency Injection
SEPARATED INTERFACE
Example
▪ ContentLister
public class ContentLister
{
private ContentFinder contentFinder;
public void setContentFinder(ContentFinder contentFinder)
{
this.contentFinder = contentFinder;
}
public List<Content> find(String pattern)
{
return contentFinder.find(pattern);
}
}
Example
▪ ContentFinder interface
public interface ContentFinder
{
List<Content> find(String pattern);
}
Example
▪ SimpleContentFinder – implementation
public class SimpleContentFinder implements ContentFinder
{
...
public List<Content> find(String pattern)
{
List<Content> contents = contentService.getContents();
List<Content> newList = new ArrayList<Content>();
for(Content c : contents)
{
if (c.getTitle().toLowerCase().contains(pattern))
{
newList.add(c);
}
}
return newList;
}
}
Example
▪ TestContentLister - Testcase
public class TestContentLister extends TestCase {
public void testContentLister () {
ServiceFactoryserviceFactory = new ServiceFactory();
ContentServicecontentService = (ContentService)

serviceFactory.getService("contentService");
contentService.addContent(new Content(1, ”The hundred-foot Journey", "", "", new Dat
contentService.addContent(new Content(1, ”Life of Crime", "", "", new Date(), ""));
contentService.addContent(new Content(1, ”The November Man", "", "", new Date(), "")
ContentFindercontentFinder = new 

SimpleContentFinder(contentService);
ContentListercontentLister = new ContentLister();
contentLister.setContentFinder(contentFinder);
List<Content>searchResults = contentLister.find("simpsons");
for (Content c : searchResults) { System.out.println(c); }
}
}
Magic stuff
Example
Template Method Pattern
Create a template for steps of an algorithm and let subclasses
extend to provide specific functionality
▪ We know the steps in an algorithm and the order
– We don’t know specific functionality
▪ How it works
– Create an abstract superclass that can be extended for the
specific functionality
– Superclass will call the abstract methods when needed
Template Method Pattern
Template Method Pattern
public class AbstractOrderEJB
{
public final Invoice placeOrder(int customerId,
InvoiceItem[] items)
throws NoSuchCustomerException, SpendingLimitViolation
{
int total = 0;
for (int i=0; i < items.length; i++)
{
total += getItemPrice(items[i]) * items[i].getQuantity();
}
if (total >getSpendingLimit(customerId))
{
...
}
else if (total > DISCOUNT_THRESHOLD) ...
int invoiceId = placeOrder(customerId, total, items);
...
}
}
Template Method Pattern
AbstractOrderEJB
placeOrder ()
abstract getItemPrice()
abstract getSpendingLimit()
abstract placeOrder()
MyOrderEJB
getItemPrice()
getSpendingLimit()
placeOrder()
extends
Domain
specific
functionality
Generic
functionality
public class MyOrderEJB extends AbstractOrderEJB
{
...
int getItemPrice(int[] i)
{
...
}
int getSpendingLimit(int customerId)
{
...
}
int placeOrder(int customerId, int total, int items)
{
...
}
}
Template Method Pattern
▪ When to Use it
– For processes where steps are know but some steps need
to be changed
– Works if same team is doing the abstract and the concrete
class
▪ When Not to Use it
– The concrete class is forced to inherit, limits possibilities
– Developer of the concrete class must understand the
abstract calls
– If another team is doing the concrete class as this creates
too much communication load between teams
Template Method Pattern
Create a template for the steps of an algorithm 

and inject the specific functionality
▪ Implement an interface to provide specific functionality
▪ Algorithms can be selected on-the-fly at runtime depending on
conditions
▪ Similar as Template Method but uses interface inheritance
Strategy Pattern
Strategy Pattern
▪ How it works
▪ Create an interface to use in the generic algorithm
▪ Implementation of the interface provides the specific
functionality
▪ Framework class has reference to the interface an
▪ Setter method for the interface
Strategy Pattern
Strategy Pattern
▪ Interface for specific functionality
▪ Generic class uses the interface
– Set method to inject the interface
public interface DataHelper
{
int getItemPrice(InvoiceItem item);
int getSpendingLimit(CustomerId) throws NoSuchCustomerException;
int palceOrder(int customerId, int total, InvoiceItem[] items);
}
private DataHelper dataHelper;
public void setDataHelper(DataHelper newDataHelper)
{
this.dataHelper = newDataHelper;
}
DEPENDENCY INJECTION
Strategy Pattern
public class OrderEJB
{
public final Invoice placeOrder(int customerId, InvoiceItem[] items)
throws NoSuchCustomerException, SpendingLimitViolation
{
int total = 0;
for (int i=0; i < items.length; i++)
{
total += this.dataHelper.getItemPrice(items[i]) *
items[i].getQuantity();
}
if (total >this.dataHelper.getSpendingLimit(customerId))
{...
}
else if (total > DISCOUNT_THRESHOLD) ...
int invoiceId = this.dataHelper.placeOrder(customerId,
total, items);
...
}
}
We are building framework for games. It turns out that all the games are
similar so we create an abstract class for basic functionality that does not
change, and then extend that class for each game.What pattern is this?
A) Layer Supertype
B) Template Method
C) Strategy
D) Dependency Injection
QUIZ
We are building framework for games. It turns out that all the games are
similar so we create an abstract class for basic functionality that does not
change, and then extend that class for each game.What pattern is this?
A) Layered Supertype
B) Template Method
C) Strategy
D) Dependency Injection
QUIZ
✔
Spring Framework
Lightweight Containers
▪ Assemble components from different projects into a
cohesive application
– Wiring is done with “Inversion of Control”
– Provide life-cycle management of objects
– Provide context
Overview
Spring 1 – Introduction
Lightweight Containers
▪ Manage objects
▪ Provide context
Spring Containers
▪ Lightweight containers
– Provides life-cycle management and other services
▪ BeanFactory
– Simple factory interface for creating beans
▪ ApplicationContext
– Extends BeanFactory and adds some functionality for application
context
▪ Packages
– org.springframework.beans
– org.springframework.context
– Refer to Spring 3
Spring Containers
▪ The concept
– Building applications from POJOs
Using BeanFactory
BeanFactory
<beans>
<bean id="person" class="Person">
<property name="name">
<value>Olafur Andri</value>
</property>
<property name="email">
<value>andri@ru.is</value>
</property>
</bean>
</beans>
read, parse
create
Person
The Bean Factory uses
setter injection to create the
person object
FileSystemXmlApplicationContext
▪ Loads the context from an XML file
▪ Application contexts are intended as central registries
– Support of hierarchical contexts (nested)
public class AppTest
{
public static void main(String[] args)
{
ApplicationContext ctx = 

new FileSystemXmlApplicationContext("app.xml");
}
}
Summary
▪ Framework patterns
– Inversion of Control and Dependency Injection
– Template Method
– Strategy
▪ From problems to patterns
– Game Framework
▪ Spring framework
– Bean containers
– BeanFactory and ApplicationContext

More Related Content

PPTX
L05 Frameworks
Ólafur Andri Ragnarsson
 
PPTX
[DSBW Spring 2009] Unit 07: WebApp Design Patterns & Frameworks (1/3)
Carles Farré
 
PPTX
L06 process design
Ólafur Andri Ragnarsson
 
PPTX
Spring jdbc
Harshit Choudhary
 
PPTX
Session 45 - Spring - Part 3 - AOP
PawanMM
 
PPSX
Spring - Part 1 - IoC, Di and Beans
Hitesh-Java
 
PPTX
Session 38 - Core Java (New Features) - Part 1
PawanMM
 
PPTX
Session 41 - Struts 2 Introduction
PawanMM
 
L05 Frameworks
Ólafur Andri Ragnarsson
 
[DSBW Spring 2009] Unit 07: WebApp Design Patterns & Frameworks (1/3)
Carles Farré
 
L06 process design
Ólafur Andri Ragnarsson
 
Spring jdbc
Harshit Choudhary
 
Session 45 - Spring - Part 3 - AOP
PawanMM
 
Spring - Part 1 - IoC, Di and Beans
Hitesh-Java
 
Session 38 - Core Java (New Features) - Part 1
PawanMM
 
Session 41 - Struts 2 Introduction
PawanMM
 

What's hot (20)

PPTX
9781337102087 ppt ch11
Terry Yoast
 
PPTX
Session 43 - Spring - Part 1 - IoC DI Beans
PawanMM
 
PPTX
Session 42 - Struts 2 Hibernate Integration
PawanMM
 
PDF
Spring aop
Hamid Ghorbani
 
PPTX
Spring AOP Introduction
b0ris_1
 
PPTX
9781337102087 ppt ch13
Terry Yoast
 
PPTX
9781337102087 ppt ch10
Terry Yoast
 
PDF
Write readable tests
Marian Wamsiedel
 
PDF
Testing Web Apps with Spring Framework 3.2
Rossen Stoyanchev
 
PPTX
Spring framework part 2
Skillwise Group
 
PPSX
Struts 2 - Hibernate Integration
Hitesh-Java
 
PDF
Testing Spring MVC and REST Web Applications
Sam Brannen
 
PPTX
Architectural Design Pattern: Android
Jitendra Kumar
 
PPT
Struts,Jsp,Servlet
dasguptahirak
 
PDF
Struts2
Rajiv Gupta
 
PDF
Apache maven, a software project management tool
Renato Primavera
 
PDF
intellimeet
Ankur Chauhan
 
PDF
Apache DeltaSpike the CDI toolbox
Antoine Sabot-Durand
 
PDF
Ee java lab assignment 4
Kuntal Bhowmick
 
PDF
S313937 cdi dochez
Jerome Dochez
 
9781337102087 ppt ch11
Terry Yoast
 
Session 43 - Spring - Part 1 - IoC DI Beans
PawanMM
 
Session 42 - Struts 2 Hibernate Integration
PawanMM
 
Spring aop
Hamid Ghorbani
 
Spring AOP Introduction
b0ris_1
 
9781337102087 ppt ch13
Terry Yoast
 
9781337102087 ppt ch10
Terry Yoast
 
Write readable tests
Marian Wamsiedel
 
Testing Web Apps with Spring Framework 3.2
Rossen Stoyanchev
 
Spring framework part 2
Skillwise Group
 
Struts 2 - Hibernate Integration
Hitesh-Java
 
Testing Spring MVC and REST Web Applications
Sam Brannen
 
Architectural Design Pattern: Android
Jitendra Kumar
 
Struts,Jsp,Servlet
dasguptahirak
 
Struts2
Rajiv Gupta
 
Apache maven, a software project management tool
Renato Primavera
 
intellimeet
Ankur Chauhan
 
Apache DeltaSpike the CDI toolbox
Antoine Sabot-Durand
 
Ee java lab assignment 4
Kuntal Bhowmick
 
S313937 cdi dochez
Jerome Dochez
 
Ad

Viewers also liked (14)

PDF
L09 Process Design
Ólafur Andri Ragnarsson
 
PDF
L08 Unit Testing
Ólafur Andri Ragnarsson
 
PDF
L12 Visualizing Architecture
Ólafur Andri Ragnarsson
 
PDF
L22 Design Principles
Ólafur Andri Ragnarsson
 
PDF
L11 Service Design and REST
Ólafur Andri Ragnarsson
 
PDF
L16 Documenting Software
Ólafur Andri Ragnarsson
 
PDF
L17 Data Source Layer
Ólafur Andri Ragnarsson
 
PDF
L15 Organising Domain Layer
Ólafur Andri Ragnarsson
 
PDF
L18 Object Relational Mapping
Ólafur Andri Ragnarsson
 
PDF
L10 Architecture Considerations
Ólafur Andri Ragnarsson
 
PDF
L21 Architecture and Agile
Ólafur Andri Ragnarsson
 
PDF
L20 Scalability
Ólafur Andri Ragnarsson
 
PDF
L19 Application Architecture
Ólafur Andri Ragnarsson
 
PPTX
Commercial track 2_UDP Solution Selling Made Simple
arcserve data protection
 
L09 Process Design
Ólafur Andri Ragnarsson
 
L08 Unit Testing
Ólafur Andri Ragnarsson
 
L12 Visualizing Architecture
Ólafur Andri Ragnarsson
 
L22 Design Principles
Ólafur Andri Ragnarsson
 
L11 Service Design and REST
Ólafur Andri Ragnarsson
 
L16 Documenting Software
Ólafur Andri Ragnarsson
 
L17 Data Source Layer
Ólafur Andri Ragnarsson
 
L15 Organising Domain Layer
Ólafur Andri Ragnarsson
 
L18 Object Relational Mapping
Ólafur Andri Ragnarsson
 
L10 Architecture Considerations
Ólafur Andri Ragnarsson
 
L21 Architecture and Agile
Ólafur Andri Ragnarsson
 
L20 Scalability
Ólafur Andri Ragnarsson
 
L19 Application Architecture
Ólafur Andri Ragnarsson
 
Commercial track 2_UDP Solution Selling Made Simple
arcserve data protection
 
Ad

Similar to L07 Frameworks (20)

PPTX
L09 Frameworks
Ólafur Andri Ragnarsson
 
PDF
Building modular software with OSGi - Ulf Fildebrandt
mfrancis
 
PPTX
Design Pattern Mastery - Momentum Dev Con 19 Apr 2018
Steven Smith
 
PDF
L05 Design Patterns
Ólafur Andri Ragnarsson
 
PDF
Handlebars and Require.js
Ivano Malavolta
 
PPTX
Most Useful Design Patterns
Steven Smith
 
PDF
L04 Software Design Examples
Ólafur Andri Ragnarsson
 
PDF
Creating Modular Test-Driven SPAs with Spring and AngularJS
Gunnar Hillert
 
PPTX
2009 Dotnet Information Day: More effective c#
Daniel Fisher
 
PDF
Sitecore development approach evolution – destination helix
Peter Nazarov
 
PDF
Diving Into Xamarin.Forms
Catapult New Business
 
PDF
C# Advanced L07-Design Patterns
Mohammad Shaker
 
PPTX
Software Architecture and Design Patterns Notes.pptx
VivekanandaGN2
 
PPTX
Creational Design Patterns.pptx
Sachin Patidar
 
PDF
BMO - Intelligent Projects with Maven
Mert Çalışkan
 
PDF
Java EE 6 CDI Integrates with Spring & JSF
Jiayun Zhou
 
PPT
Introduction to design_patterns
amitarcade
 
PDF
Gof design pattern
naveen kumar
 
PPTX
ASP.NET MVC Best Practices malisa ncube
Malisa Ncube
 
PPTX
Spring boot
NexThoughts Technologies
 
L09 Frameworks
Ólafur Andri Ragnarsson
 
Building modular software with OSGi - Ulf Fildebrandt
mfrancis
 
Design Pattern Mastery - Momentum Dev Con 19 Apr 2018
Steven Smith
 
L05 Design Patterns
Ólafur Andri Ragnarsson
 
Handlebars and Require.js
Ivano Malavolta
 
Most Useful Design Patterns
Steven Smith
 
L04 Software Design Examples
Ólafur Andri Ragnarsson
 
Creating Modular Test-Driven SPAs with Spring and AngularJS
Gunnar Hillert
 
2009 Dotnet Information Day: More effective c#
Daniel Fisher
 
Sitecore development approach evolution – destination helix
Peter Nazarov
 
Diving Into Xamarin.Forms
Catapult New Business
 
C# Advanced L07-Design Patterns
Mohammad Shaker
 
Software Architecture and Design Patterns Notes.pptx
VivekanandaGN2
 
Creational Design Patterns.pptx
Sachin Patidar
 
BMO - Intelligent Projects with Maven
Mert Çalışkan
 
Java EE 6 CDI Integrates with Spring & JSF
Jiayun Zhou
 
Introduction to design_patterns
amitarcade
 
Gof design pattern
naveen kumar
 
ASP.NET MVC Best Practices malisa ncube
Malisa Ncube
 

More from Ólafur Andri Ragnarsson (20)

PDF
Nýsköpun - Leiðin til framfara
Ólafur Andri Ragnarsson
 
PDF
Nýjast tækni og framtíðin
Ólafur Andri Ragnarsson
 
PDF
New Technology Summer 2020 Course Introduction
Ólafur Andri Ragnarsson
 
PDF
L01 Introduction
Ólafur Andri Ragnarsson
 
PDF
L23 Robotics and Drones
Ólafur Andri Ragnarsson
 
PDF
L22 Augmented and Virtual Reality
Ólafur Andri Ragnarsson
 
PDF
L20 Personalised World
Ólafur Andri Ragnarsson
 
PDF
L19 Network Platforms
Ólafur Andri Ragnarsson
 
PDF
L18 Big Data and Analytics
Ólafur Andri Ragnarsson
 
PDF
L17 Algorithms and AI
Ólafur Andri Ragnarsson
 
PDF
L16 Internet of Things
Ólafur Andri Ragnarsson
 
PDF
L14 From the Internet to Blockchain
Ólafur Andri Ragnarsson
 
PDF
L14 The Mobile Revolution
Ólafur Andri Ragnarsson
 
PDF
New Technology 2019 L13 Rise of the Machine
Ólafur Andri Ragnarsson
 
PDF
L12 digital transformation
Ólafur Andri Ragnarsson
 
PDF
L10 The Innovator's Dilemma
Ólafur Andri Ragnarsson
 
PDF
L09 Disruptive Technology
Ólafur Andri Ragnarsson
 
PDF
L09 Technological Revolutions
Ólafur Andri Ragnarsson
 
PDF
L07 Becoming Invisible
Ólafur Andri Ragnarsson
 
PDF
L06 Diffusion of Innovation
Ólafur Andri Ragnarsson
 
Nýsköpun - Leiðin til framfara
Ólafur Andri Ragnarsson
 
Nýjast tækni og framtíðin
Ólafur Andri Ragnarsson
 
New Technology Summer 2020 Course Introduction
Ólafur Andri Ragnarsson
 
L01 Introduction
Ólafur Andri Ragnarsson
 
L23 Robotics and Drones
Ólafur Andri Ragnarsson
 
L22 Augmented and Virtual Reality
Ólafur Andri Ragnarsson
 
L20 Personalised World
Ólafur Andri Ragnarsson
 
L19 Network Platforms
Ólafur Andri Ragnarsson
 
L18 Big Data and Analytics
Ólafur Andri Ragnarsson
 
L17 Algorithms and AI
Ólafur Andri Ragnarsson
 
L16 Internet of Things
Ólafur Andri Ragnarsson
 
L14 From the Internet to Blockchain
Ólafur Andri Ragnarsson
 
L14 The Mobile Revolution
Ólafur Andri Ragnarsson
 
New Technology 2019 L13 Rise of the Machine
Ólafur Andri Ragnarsson
 
L12 digital transformation
Ólafur Andri Ragnarsson
 
L10 The Innovator's Dilemma
Ólafur Andri Ragnarsson
 
L09 Disruptive Technology
Ólafur Andri Ragnarsson
 
L09 Technological Revolutions
Ólafur Andri Ragnarsson
 
L07 Becoming Invisible
Ólafur Andri Ragnarsson
 
L06 Diffusion of Innovation
Ólafur Andri Ragnarsson
 

Recently uploaded (20)

PPT
Activate_Methodology_Summary presentatio
annapureddyn
 
PPTX
Can You Build Dashboards Using Open Source Visualization Tool.pptx
Varsha Nayak
 
PPTX
Maximizing Revenue with Marketo Measure: A Deep Dive into Multi-Touch Attribu...
bbedford2
 
PPTX
Presentation about Database and Database Administrator
abhishekchauhan86963
 
PDF
10 posting ideas for community engagement with AI prompts
Pankaj Taneja
 
PPTX
Odoo Integration Services by Candidroot Solutions
CandidRoot Solutions Private Limited
 
PPTX
The-Dawn-of-AI-Reshaping-Our-World.pptxx
parthbhanushali307
 
PPTX
classification of computer and basic part of digital computer
ravisinghrajpurohit3
 
PPTX
slidesgo-unlocking-the-code-the-dynamic-dance-of-variables-and-constants-2024...
kr2589474
 
PDF
An Experience-Based Look at AI Lead Generation Pricing, Features & B2B Results
Thomas albart
 
PDF
On Software Engineers' Productivity - Beyond Misleading Metrics
Romén Rodríguez-Gil
 
PDF
New Download FL Studio Crack Full Version [Latest 2025]
imang66g
 
PDF
What to consider before purchasing Microsoft 365 Business Premium_PDF.pdf
Q-Advise
 
PDF
advancepresentationskillshdhdhhdhdhdhhfhf
jasmenrojas249
 
PPTX
Role Of Python In Programing Language.pptx
jaykoshti048
 
PPTX
TRAVEL APIs | WHITE LABEL TRAVEL API | TOP TRAVEL APIs
philipnathen82
 
PDF
Applitools Platform Pulse: What's New and What's Coming - July 2025
Applitools
 
PDF
Exploring AI Agents in Process Industries
amoreira6
 
PPTX
Contractor Management Platform and Software Solution for Compliance
SHEQ Network Limited
 
PDF
Protecting the Digital World Cyber Securit
dnthakkar16
 
Activate_Methodology_Summary presentatio
annapureddyn
 
Can You Build Dashboards Using Open Source Visualization Tool.pptx
Varsha Nayak
 
Maximizing Revenue with Marketo Measure: A Deep Dive into Multi-Touch Attribu...
bbedford2
 
Presentation about Database and Database Administrator
abhishekchauhan86963
 
10 posting ideas for community engagement with AI prompts
Pankaj Taneja
 
Odoo Integration Services by Candidroot Solutions
CandidRoot Solutions Private Limited
 
The-Dawn-of-AI-Reshaping-Our-World.pptxx
parthbhanushali307
 
classification of computer and basic part of digital computer
ravisinghrajpurohit3
 
slidesgo-unlocking-the-code-the-dynamic-dance-of-variables-and-constants-2024...
kr2589474
 
An Experience-Based Look at AI Lead Generation Pricing, Features & B2B Results
Thomas albart
 
On Software Engineers' Productivity - Beyond Misleading Metrics
Romén Rodríguez-Gil
 
New Download FL Studio Crack Full Version [Latest 2025]
imang66g
 
What to consider before purchasing Microsoft 365 Business Premium_PDF.pdf
Q-Advise
 
advancepresentationskillshdhdhhdhdhdhhfhf
jasmenrojas249
 
Role Of Python In Programing Language.pptx
jaykoshti048
 
TRAVEL APIs | WHITE LABEL TRAVEL API | TOP TRAVEL APIs
philipnathen82
 
Applitools Platform Pulse: What's New and What's Coming - July 2025
Applitools
 
Exploring AI Agents in Process Industries
amoreira6
 
Contractor Management Platform and Software Solution for Compliance
SHEQ Network Limited
 
Protecting the Digital World Cyber Securit
dnthakkar16
 

L07 Frameworks

  • 1. HÖNNUN OG SMÍÐI HUGBÚNAÐAR 2015 L07 FRAMEWORKS
  • 2. Agenda Why frameworks? Framework patterns •Inversion of Control and Dependency Injection •Template Method •Strategy From problems to patterns •Game Framework Spring framework •Bean containers •BeanFactory and ApplicationContext
  • 3. Reading Dependency Injection Template Method Pattern Strategy Pattern Spring Framework (video) Article by Fowler Inversion of Control Containers and the Dependency Injection pattern
  • 4. Resources ▪ Spring Framework homepage – http://www.springframework.org ▪ Reference Documentation – http://www.springframework.org/docs/reference/index.html – Also in PDF format
  • 6. Why use Frameworks? ▪ Frameworks can increase productivity – We can create our own framework – We can use some third party framework ▪ Frameworks implement general functionality – We use the framework to implement our business logic
  • 7. Framework design ▪ Inheritance of framework classes ▪ Composition of framework classes ▪ Implementation of framework interfaces ▪ Dependency Injection ?Your Domain Code Framework
  • 8. Using Frameworks ▪ Frameworks are concrete, not abstract – Design patterns are conceptual, frameworks provide building blocks ▪ Frameworks are higher-level – Built on design patterns ▪ Frameworks are usually general or technology-specific ▪ Good frameworks are simple to use, yet powerful
  • 9. Abstractions ▪ From API to Frameworks API Definition JEE/.NET API API Patterns JEE/.NET Patterns Framework Spring
  • 10. Open Source Frameworks ▪ Web Frameworks – Jakarta Struts, WebWork, Maverick, Play! ▪ Database Frameworks – Hibernate, JDO, TopLink ▪ General Framework – Spring, Expresso, PicoContainer, Avalon ▪ Platform Frameworks – JEE
  • 11. Where do Frameworks Come From? ▪ Who spends their time writing frameworks? ▪ If they give them away, how can anyone make money? ▪ Companies that use frameworks, have their developers work on them ▪ Give the code, sell the training and consulting
  • 12. Write  down  the  pros  and  cons  (benefits  and  drawbacks)  for  frameworks.   Use  two  columns,  benefits  on  the  left,  drawbacks  right EXERCISE
  • 13. Pros and Cons ▪ Pros – Productivity – Well know application models and patterns – Tested functionality – Connection of different components – Use of open standards ▪ Cons – Can be complicated, learning curve – Dependant on frameworks, difficult to change – Difficult to debug and find bugs – Performance problems can be difficult – Can be bought by an evil company
  • 15. Separation of Concerns ▪ One of the main challenge of frameworks is to provide separation of concerns – Frameworks deal with generic functionality – Layers of code ▪ Frameworks need patterns to combine generic and domain specific functionality
  • 16. The Hollywood Principle ▪ “Don’t call us, we’ll call you” ▪ Your program does not call the framework, it’s the framework that controls the execution of your program TRADITIONAL HOLLYWOOD CALL Framework Handler Handler Framework Program Program
  • 17. Inversion of Control (IoC) ▪ Your application runs in a container (framework) ▪ Container manages the life-cycle of your object and provides context ▪ The framework has the control
  • 18. Framework Patterns ▪ Useful patterns when building a framework: – Dependency Injection: remove dependencies by injecting them (sometimes called Inversion of Control) – Template Method: extend a generic class and provide specific functionality – Strategy: Implement an interface to provide specific functionality
  • 19. Dependency Injection Removes explicit dependence on specific application code by injecting depending classes into the framework ▪ Objects and interfaces are injected into the classes that to the work ▪ Two types of injection ▪ Setter injection: using set methods ▪ Constructor injection: using constructors
  • 20. ▪ Fowler’s Naive Example – MovieLister uses a finder class – How can we separate the finder functionality? class MovieLister... public Movie[] moviesDirectedBy(String arg) { List allMovies = finder.findAll(); for (Iterator it = allMovies.iterator(); it.hasNext();) { Movie movie = (Movie) it.next(); if (!movie.getDirector().equals(arg)) it.remove(); } return (Movie[])allMovies.toArray(new Movie[allMovies.size()]); } REMEMBER PROGRAM TO INTERFACES PRINCIPLE? Dependency Injection Separate  what  varies
  • 21. ▪ Fowler’s Naive Example – Let’s make an interface, MovieFinder – MovieLister is still dependent on particular MovieFinder implementation public interface MovieFinder { List findAll(); } class MovieLister... private MovieFinder finder; public MovieLister() { finder = new MovieFinderImpl("movies1.txt"); } Argh!   Not  cool. Dependency Injection
  • 22. ▪ An assembler (or container) is used to create an implementation – Using constructor injection, the assembler will create a MovieLister and passing a MovieFinder interface in the contractor – Using setter injection, the assembler will create
 MovieLister and then all the setFinder setter 
 method to provide the
 MovieFinder interface Dependency Injection
  • 23. ▪ Example setter injection class MovieLister... private MovieFinder finder; public void setFinder(MovieFinder finder) { this.finder = finder; } class MovieFinderImpl... public void setFilename(String filename) this.filename = filename; } Dependency Injection
  • 25. Example ▪ ContentLister public class ContentLister { private ContentFinder contentFinder; public void setContentFinder(ContentFinder contentFinder) { this.contentFinder = contentFinder; } public List<Content> find(String pattern) { return contentFinder.find(pattern); } }
  • 26. Example ▪ ContentFinder interface public interface ContentFinder { List<Content> find(String pattern); }
  • 27. Example ▪ SimpleContentFinder – implementation public class SimpleContentFinder implements ContentFinder { ... public List<Content> find(String pattern) { List<Content> contents = contentService.getContents(); List<Content> newList = new ArrayList<Content>(); for(Content c : contents) { if (c.getTitle().toLowerCase().contains(pattern)) { newList.add(c); } } return newList; } }
  • 28. Example ▪ TestContentLister - Testcase public class TestContentLister extends TestCase { public void testContentLister () { ServiceFactoryserviceFactory = new ServiceFactory(); ContentServicecontentService = (ContentService)
 serviceFactory.getService("contentService"); contentService.addContent(new Content(1, ”The hundred-foot Journey", "", "", new Dat contentService.addContent(new Content(1, ”Life of Crime", "", "", new Date(), "")); contentService.addContent(new Content(1, ”The November Man", "", "", new Date(), "") ContentFindercontentFinder = new 
 SimpleContentFinder(contentService); ContentListercontentLister = new ContentLister(); contentLister.setContentFinder(contentFinder); List<Content>searchResults = contentLister.find("simpsons"); for (Content c : searchResults) { System.out.println(c); } } } Magic stuff
  • 30. Template Method Pattern Create a template for steps of an algorithm and let subclasses extend to provide specific functionality ▪ We know the steps in an algorithm and the order – We don’t know specific functionality ▪ How it works – Create an abstract superclass that can be extended for the specific functionality – Superclass will call the abstract methods when needed
  • 32. Template Method Pattern public class AbstractOrderEJB { public final Invoice placeOrder(int customerId, InvoiceItem[] items) throws NoSuchCustomerException, SpendingLimitViolation { int total = 0; for (int i=0; i < items.length; i++) { total += getItemPrice(items[i]) * items[i].getQuantity(); } if (total >getSpendingLimit(customerId)) { ... } else if (total > DISCOUNT_THRESHOLD) ... int invoiceId = placeOrder(customerId, total, items); ... } }
  • 33. Template Method Pattern AbstractOrderEJB placeOrder () abstract getItemPrice() abstract getSpendingLimit() abstract placeOrder() MyOrderEJB getItemPrice() getSpendingLimit() placeOrder() extends Domain specific functionality Generic functionality
  • 34. public class MyOrderEJB extends AbstractOrderEJB { ... int getItemPrice(int[] i) { ... } int getSpendingLimit(int customerId) { ... } int placeOrder(int customerId, int total, int items) { ... } } Template Method Pattern
  • 35. ▪ When to Use it – For processes where steps are know but some steps need to be changed – Works if same team is doing the abstract and the concrete class ▪ When Not to Use it – The concrete class is forced to inherit, limits possibilities – Developer of the concrete class must understand the abstract calls – If another team is doing the concrete class as this creates too much communication load between teams Template Method Pattern
  • 36. Create a template for the steps of an algorithm 
 and inject the specific functionality ▪ Implement an interface to provide specific functionality ▪ Algorithms can be selected on-the-fly at runtime depending on conditions ▪ Similar as Template Method but uses interface inheritance Strategy Pattern
  • 37. Strategy Pattern ▪ How it works ▪ Create an interface to use in the generic algorithm ▪ Implementation of the interface provides the specific functionality ▪ Framework class has reference to the interface an ▪ Setter method for the interface
  • 39. Strategy Pattern ▪ Interface for specific functionality ▪ Generic class uses the interface – Set method to inject the interface public interface DataHelper { int getItemPrice(InvoiceItem item); int getSpendingLimit(CustomerId) throws NoSuchCustomerException; int palceOrder(int customerId, int total, InvoiceItem[] items); } private DataHelper dataHelper; public void setDataHelper(DataHelper newDataHelper) { this.dataHelper = newDataHelper; } DEPENDENCY INJECTION
  • 40. Strategy Pattern public class OrderEJB { public final Invoice placeOrder(int customerId, InvoiceItem[] items) throws NoSuchCustomerException, SpendingLimitViolation { int total = 0; for (int i=0; i < items.length; i++) { total += this.dataHelper.getItemPrice(items[i]) * items[i].getQuantity(); } if (total >this.dataHelper.getSpendingLimit(customerId)) {... } else if (total > DISCOUNT_THRESHOLD) ... int invoiceId = this.dataHelper.placeOrder(customerId, total, items); ... } }
  • 41. We are building framework for games. It turns out that all the games are similar so we create an abstract class for basic functionality that does not change, and then extend that class for each game.What pattern is this? A) Layer Supertype B) Template Method C) Strategy D) Dependency Injection QUIZ
  • 42. We are building framework for games. It turns out that all the games are similar so we create an abstract class for basic functionality that does not change, and then extend that class for each game.What pattern is this? A) Layered Supertype B) Template Method C) Strategy D) Dependency Injection QUIZ ✔
  • 44. Lightweight Containers ▪ Assemble components from different projects into a cohesive application – Wiring is done with “Inversion of Control” – Provide life-cycle management of objects – Provide context
  • 45. Overview Spring 1 – Introduction
  • 46. Lightweight Containers ▪ Manage objects ▪ Provide context
  • 47. Spring Containers ▪ Lightweight containers – Provides life-cycle management and other services ▪ BeanFactory – Simple factory interface for creating beans ▪ ApplicationContext – Extends BeanFactory and adds some functionality for application context ▪ Packages – org.springframework.beans – org.springframework.context – Refer to Spring 3
  • 48. Spring Containers ▪ The concept – Building applications from POJOs
  • 49. Using BeanFactory BeanFactory <beans> <bean id="person" class="Person"> <property name="name"> <value>Olafur Andri</value> </property> <property name="email"> <value>[email protected]</value> </property> </bean> </beans> read, parse create Person The Bean Factory uses setter injection to create the person object
  • 50. FileSystemXmlApplicationContext ▪ Loads the context from an XML file ▪ Application contexts are intended as central registries – Support of hierarchical contexts (nested) public class AppTest { public static void main(String[] args) { ApplicationContext ctx = 
 new FileSystemXmlApplicationContext("app.xml"); } }
  • 51. Summary ▪ Framework patterns – Inversion of Control and Dependency Injection – Template Method – Strategy ▪ From problems to patterns – Game Framework ▪ Spring framework – Bean containers – BeanFactory and ApplicationContext