SlideShare a Scribd company logo
Lecture 09 
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 
Framework 
Your Code 
Domain ?
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 
Framework Spring 
API Patterns JEE/.NET 
Patterns 
API Definition JEE/.NET API
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
EXERCISE 
Write down the pros and cons (benefits and drawbacks) for frameworks. 
Use two columns, benefits on the left, drawbacks right
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
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
Dependency Injection 
 Fowler’s Naive Example 
– MovieLister uses a finder class 
class MovieLister... 
Separate what varies 
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()]); 
} 
– How can we separate the finder functionality? 
REMEMBER PROGRAM TO INTERFACES PRINSIPLE?
Dependency Injection 
 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 assember will create a 
MovieLister and passing a MovieFinder interface in the 
contructor 
– 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 
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 Simpsons Movie", "", "", new Date(), contentService.addContent(new Content(1, "The Bourne Ultimatum", "", "", new Date(), contentService.addContent(new Content(1, "Rush Hour 3", "", "", 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 
Generic 
functionality 
Domain 
specific 
functionality
Template Method Pattern 
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
Strategy 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 
 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 
public interface DataHelper 
{ 
int getItemPrice(InvoiceItem item); 
int getSpendingLimit(CustomerId) throws NoSuchCustomerException; 
int palceOrder(int customerId, int total, InvoiceItem[] items); 
 Generic class uses the interface 
– Set method to inject the interface 
} 
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); 
... 
} 
}
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 
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 
✔
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

What's hot

Design Patterns For 70% Of Programmers In The World
Design Patterns For 70% Of Programmers In The WorldDesign Patterns For 70% Of Programmers In The World
Design Patterns For 70% Of Programmers In The World
Saurabh Moody
 
Software Design Patterns. Part I :: Structural Patterns
Software Design Patterns. Part I :: Structural PatternsSoftware Design Patterns. Part I :: Structural Patterns
Software Design Patterns. Part I :: Structural Patterns
Sergey Aganezov
 
Design patterns
Design patternsDesign patterns
Design patterns
Anas Alpure
 
Software Design Patterns
Software Design PatternsSoftware Design Patterns
Software Design Patterns
Satheesh Sukumaran
 
9781337102087 ppt ch10
9781337102087 ppt ch109781337102087 ppt ch10
9781337102087 ppt ch10
Terry Yoast
 
9781337102087 ppt ch14
9781337102087 ppt ch149781337102087 ppt ch14
9781337102087 ppt ch14
Terry Yoast
 
9781337102087 ppt ch08
9781337102087 ppt ch089781337102087 ppt ch08
9781337102087 ppt ch08
Terry Yoast
 
9781337102087 ppt ch13
9781337102087 ppt ch139781337102087 ppt ch13
9781337102087 ppt ch13
Terry Yoast
 
45 aop-programming
45 aop-programming45 aop-programming
45 aop-programming
daotuan85
 
Evolution of Patterns
Evolution of PatternsEvolution of Patterns
Evolution of Patterns
Chris Eargle
 
Applet in java new
Applet in java newApplet in java new
Applet in java new
Kavitha713564
 
Eclipse World 2007: Fundamentals of the Eclipse Modeling Framework
Eclipse World 2007: Fundamentals of the Eclipse Modeling FrameworkEclipse World 2007: Fundamentals of the Eclipse Modeling Framework
Eclipse World 2007: Fundamentals of the Eclipse Modeling Framework
Dave Steinberg
 
Design patterns difference between interview questions
Design patterns   difference between interview questionsDesign patterns   difference between interview questions
Design patterns difference between interview questions
Umar Ali
 
P Training Presentation
P Training PresentationP Training Presentation
P Training Presentation
Gaurav Tyagi
 
Bartlesville Dot Net User Group Design Patterns
Bartlesville Dot Net User Group Design PatternsBartlesville Dot Net User Group Design Patterns
Bartlesville Dot Net User Group Design Patterns
Jason Townsend, MBA
 
Introduction to Design Patterns in Javascript
Introduction to Design Patterns in JavascriptIntroduction to Design Patterns in Javascript
Introduction to Design Patterns in Javascript
Santhosh Kumar Srinivasan
 
Jump start to OOP, OOAD, and Design Pattern
Jump start to OOP, OOAD, and Design PatternJump start to OOP, OOAD, and Design Pattern
Jump start to OOP, OOAD, and Design Pattern
Nishith Shukla
 
Software Design Patterns
Software Design PatternsSoftware Design Patterns
Software Design Patterns
Satheesh Sukumaran
 
Design Patterns
Design PatternsDesign Patterns
Design Patterns
adil raja
 
Design pattern
Design patternDesign pattern
Design pattern
Thibaut De Broca
 

What's hot (20)

Design Patterns For 70% Of Programmers In The World
Design Patterns For 70% Of Programmers In The WorldDesign Patterns For 70% Of Programmers In The World
Design Patterns For 70% Of Programmers In The World
 
Software Design Patterns. Part I :: Structural Patterns
Software Design Patterns. Part I :: Structural PatternsSoftware Design Patterns. Part I :: Structural Patterns
Software Design Patterns. Part I :: Structural Patterns
 
Design patterns
Design patternsDesign patterns
Design patterns
 
Software Design Patterns
Software Design PatternsSoftware Design Patterns
Software Design Patterns
 
9781337102087 ppt ch10
9781337102087 ppt ch109781337102087 ppt ch10
9781337102087 ppt ch10
 
9781337102087 ppt ch14
9781337102087 ppt ch149781337102087 ppt ch14
9781337102087 ppt ch14
 
9781337102087 ppt ch08
9781337102087 ppt ch089781337102087 ppt ch08
9781337102087 ppt ch08
 
9781337102087 ppt ch13
9781337102087 ppt ch139781337102087 ppt ch13
9781337102087 ppt ch13
 
45 aop-programming
45 aop-programming45 aop-programming
45 aop-programming
 
Evolution of Patterns
Evolution of PatternsEvolution of Patterns
Evolution of Patterns
 
Applet in java new
Applet in java newApplet in java new
Applet in java new
 
Eclipse World 2007: Fundamentals of the Eclipse Modeling Framework
Eclipse World 2007: Fundamentals of the Eclipse Modeling FrameworkEclipse World 2007: Fundamentals of the Eclipse Modeling Framework
Eclipse World 2007: Fundamentals of the Eclipse Modeling Framework
 
Design patterns difference between interview questions
Design patterns   difference between interview questionsDesign patterns   difference between interview questions
Design patterns difference between interview questions
 
P Training Presentation
P Training PresentationP Training Presentation
P Training Presentation
 
Bartlesville Dot Net User Group Design Patterns
Bartlesville Dot Net User Group Design PatternsBartlesville Dot Net User Group Design Patterns
Bartlesville Dot Net User Group Design Patterns
 
Introduction to Design Patterns in Javascript
Introduction to Design Patterns in JavascriptIntroduction to Design Patterns in Javascript
Introduction to Design Patterns in Javascript
 
Jump start to OOP, OOAD, and Design Pattern
Jump start to OOP, OOAD, and Design PatternJump start to OOP, OOAD, and Design Pattern
Jump start to OOP, OOAD, and Design Pattern
 
Software Design Patterns
Software Design PatternsSoftware Design Patterns
Software Design Patterns
 
Design Patterns
Design PatternsDesign Patterns
Design Patterns
 
Design pattern
Design patternDesign pattern
Design pattern
 

Viewers also liked

L10 Using Frameworks
L10 Using FrameworksL10 Using Frameworks
L10 Using Frameworks
Ólafur Andri Ragnarsson
 
7 de kt hoc ki 2 tieng anh 9
7 de kt hoc ki 2 tieng anh 97 de kt hoc ki 2 tieng anh 9
7 de kt hoc ki 2 tieng anh 9
miyoen
 
Office 365 For Business
Office 365 For BusinessOffice 365 For Business
Office 365 For Business
gosako
 
Windows 8 Preso
Windows 8 PresoWindows 8 Preso
Windows 8 Preso
gosako
 
Hdi Presentation
Hdi PresentationHdi Presentation
Hdi Presentation
gosako
 
L23 Summary and Conclusions
L23 Summary and ConclusionsL23 Summary and Conclusions
L23 Summary and Conclusions
Ólafur Andri Ragnarsson
 
Office 365 - Afinety
Office 365 - AfinetyOffice 365 - Afinety
Office 365 - Afinety
gosako
 
Top 5 Mpn Resources
Top 5 Mpn ResourcesTop 5 Mpn Resources
Top 5 Mpn Resources
gosako
 
WPC12 Highlights
WPC12 HighlightsWPC12 Highlights
WPC12 Highlights
gosako
 
14 de thi hk1 tieng anh chuyen chu van an
14 de thi hk1 tieng anh chuyen chu van an14 de thi hk1 tieng anh chuyen chu van an
14 de thi hk1 tieng anh chuyen chu van an
miyoen
 
Disruption Rules!
Disruption Rules!Disruption Rules!
Disruption Rules!
Ólafur Andri Ragnarsson
 
L18 REST API Design
L18 REST API DesignL18 REST API Design
L18 REST API Design
Ólafur Andri Ragnarsson
 
Microsoft Partner Network Overview June 2010
Microsoft Partner Network Overview June 2010Microsoft Partner Network Overview June 2010
Microsoft Partner Network Overview June 2010
gosako
 
L04 Software Design 2
L04 Software Design 2L04 Software Design 2
L04 Software Design 2
Ólafur Andri Ragnarsson
 
Irvine Var Summit
Irvine Var SummitIrvine Var Summit
Irvine Var Summit
gosako
 
Word formation exercises
Word formation exercisesWord formation exercises
Word formation exercises
miyoen
 

Viewers also liked (17)

L10 Using Frameworks
L10 Using FrameworksL10 Using Frameworks
L10 Using Frameworks
 
7 de kt hoc ki 2 tieng anh 9
7 de kt hoc ki 2 tieng anh 97 de kt hoc ki 2 tieng anh 9
7 de kt hoc ki 2 tieng anh 9
 
Office 365 For Business
Office 365 For BusinessOffice 365 For Business
Office 365 For Business
 
Windows 8 Preso
Windows 8 PresoWindows 8 Preso
Windows 8 Preso
 
Hdi Presentation
Hdi PresentationHdi Presentation
Hdi Presentation
 
L23 Summary and Conclusions
L23 Summary and ConclusionsL23 Summary and Conclusions
L23 Summary and Conclusions
 
Office 365 - Afinety
Office 365 - AfinetyOffice 365 - Afinety
Office 365 - Afinety
 
Top 5 Mpn Resources
Top 5 Mpn ResourcesTop 5 Mpn Resources
Top 5 Mpn Resources
 
WPC12 Highlights
WPC12 HighlightsWPC12 Highlights
WPC12 Highlights
 
14 de thi hk1 tieng anh chuyen chu van an
14 de thi hk1 tieng anh chuyen chu van an14 de thi hk1 tieng anh chuyen chu van an
14 de thi hk1 tieng anh chuyen chu van an
 
Disruption Rules!
Disruption Rules!Disruption Rules!
Disruption Rules!
 
L18 REST API Design
L18 REST API DesignL18 REST API Design
L18 REST API Design
 
Microsoft Partner Network Overview June 2010
Microsoft Partner Network Overview June 2010Microsoft Partner Network Overview June 2010
Microsoft Partner Network Overview June 2010
 
L04 Software Design 2
L04 Software Design 2L04 Software Design 2
L04 Software Design 2
 
Irvine Var Summit
Irvine Var SummitIrvine Var Summit
Irvine Var Summit
 
Word formation exercises
Word formation exercisesWord formation exercises
Word formation exercises
 
U beelil jum'péel ma'alo'ob ka'ansaj
U beelil jum'péel ma'alo'ob ka'ansajU beelil jum'péel ma'alo'ob ka'ansaj
U beelil jum'péel ma'alo'ob ka'ansaj
 

Similar to L09 Frameworks

Design Pattern Mastery - Momentum Dev Con 19 Apr 2018
Design Pattern Mastery - Momentum Dev Con 19 Apr 2018Design Pattern Mastery - Momentum Dev Con 19 Apr 2018
Design Pattern Mastery - Momentum Dev Con 19 Apr 2018
Steven Smith
 
A brief overview of java frameworks
A brief overview of java frameworksA brief overview of java frameworks
A brief overview of java frameworks
MD Sayem Ahmed
 
Design Patterns
Design PatternsDesign Patterns
Design Patterns
Rafael Coutinho
 
C# Advanced L07-Design Patterns
C# Advanced L07-Design PatternsC# Advanced L07-Design Patterns
C# Advanced L07-Design Patterns
Mohammad Shaker
 
TDD And Refactoring
TDD And RefactoringTDD And Refactoring
TDD And Refactoring
Naresh Jain
 
Design Patterns - Part 1 of 2
Design Patterns - Part 1 of 2Design Patterns - Part 1 of 2
Design Patterns - Part 1 of 2
Savio Sebastian
 
L04 Software Design Examples
L04 Software Design ExamplesL04 Software Design Examples
L04 Software Design Examples
Ólafur Andri Ragnarsson
 
Lesson12 other behavioural patterns
Lesson12 other behavioural patternsLesson12 other behavioural patterns
Lesson12 other behavioural patterns
OktJona
 
Spring boot
Spring bootSpring boot
Aspect Oriented Programming
Aspect Oriented ProgrammingAspect Oriented Programming
Aspect Oriented Programming
Fernando Almeida
 
Patterns in Python
Patterns in PythonPatterns in Python
Patterns in Python
dn
 
Most Useful Design Patterns
Most Useful Design PatternsMost Useful Design Patterns
Most Useful Design Patterns
Steven Smith
 
L04 base patterns
L04 base patternsL04 base patterns
L04 base patterns
Ólafur Andri Ragnarsson
 
Repository Pattern in MVC3 Application with Entity Framework
Repository Pattern in MVC3 Application with Entity FrameworkRepository Pattern in MVC3 Application with Entity Framework
Repository Pattern in MVC3 Application with Entity Framework
Akhil Mittal
 
L06 Using Design Patterns
L06 Using Design PatternsL06 Using Design Patterns
L06 Using Design Patterns
Ólafur Andri Ragnarsson
 
Design patterns in java script, jquery, angularjs
Design patterns in java script, jquery, angularjsDesign patterns in java script, jquery, angularjs
Design patterns in java script, jquery, angularjs
Ravi Bhadauria
 
Lecture11
Lecture11Lecture11
Lecture11
artgreen
 
Building modular software with OSGi - Ulf Fildebrandt
Building modular software with OSGi - Ulf FildebrandtBuilding modular software with OSGi - Ulf Fildebrandt
Building modular software with OSGi - Ulf Fildebrandt
mfrancis
 
Design Patterns
Design PatternsDesign Patterns
Design Patterns
Ankit.Rustagi
 
Design Patterns
Design PatternsDesign Patterns
Design Patterns
imedo.de
 

Similar to L09 Frameworks (20)

Design Pattern Mastery - Momentum Dev Con 19 Apr 2018
Design Pattern Mastery - Momentum Dev Con 19 Apr 2018Design Pattern Mastery - Momentum Dev Con 19 Apr 2018
Design Pattern Mastery - Momentum Dev Con 19 Apr 2018
 
A brief overview of java frameworks
A brief overview of java frameworksA brief overview of java frameworks
A brief overview of java frameworks
 
Design Patterns
Design PatternsDesign Patterns
Design Patterns
 
C# Advanced L07-Design Patterns
C# Advanced L07-Design PatternsC# Advanced L07-Design Patterns
C# Advanced L07-Design Patterns
 
TDD And Refactoring
TDD And RefactoringTDD And Refactoring
TDD And Refactoring
 
Design Patterns - Part 1 of 2
Design Patterns - Part 1 of 2Design Patterns - Part 1 of 2
Design Patterns - Part 1 of 2
 
L04 Software Design Examples
L04 Software Design ExamplesL04 Software Design Examples
L04 Software Design Examples
 
Lesson12 other behavioural patterns
Lesson12 other behavioural patternsLesson12 other behavioural patterns
Lesson12 other behavioural patterns
 
Spring boot
Spring bootSpring boot
Spring boot
 
Aspect Oriented Programming
Aspect Oriented ProgrammingAspect Oriented Programming
Aspect Oriented Programming
 
Patterns in Python
Patterns in PythonPatterns in Python
Patterns in Python
 
Most Useful Design Patterns
Most Useful Design PatternsMost Useful Design Patterns
Most Useful Design Patterns
 
L04 base patterns
L04 base patternsL04 base patterns
L04 base patterns
 
Repository Pattern in MVC3 Application with Entity Framework
Repository Pattern in MVC3 Application with Entity FrameworkRepository Pattern in MVC3 Application with Entity Framework
Repository Pattern in MVC3 Application with Entity Framework
 
L06 Using Design Patterns
L06 Using Design PatternsL06 Using Design Patterns
L06 Using Design Patterns
 
Design patterns in java script, jquery, angularjs
Design patterns in java script, jquery, angularjsDesign patterns in java script, jquery, angularjs
Design patterns in java script, jquery, angularjs
 
Lecture11
Lecture11Lecture11
Lecture11
 
Building modular software with OSGi - Ulf Fildebrandt
Building modular software with OSGi - Ulf FildebrandtBuilding modular software with OSGi - Ulf Fildebrandt
Building modular software with OSGi - Ulf Fildebrandt
 
Design Patterns
Design PatternsDesign Patterns
Design Patterns
 
Design Patterns
Design PatternsDesign Patterns
Design Patterns
 

More from Ólafur Andri Ragnarsson

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

More from Ólafur Andri Ragnarsson (20)

Nýsköpun - Leiðin til framfara
Nýsköpun - Leiðin til framfaraNýsköpun - Leiðin til framfara
Nýsköpun - Leiðin til framfara
 
Nýjast tækni og framtíðin
Nýjast tækni og framtíðinNýjast tækni og framtíðin
Nýjast tækni og framtíðin
 
New Technology Summer 2020 Course Introduction
New Technology Summer 2020 Course IntroductionNew Technology Summer 2020 Course Introduction
New Technology Summer 2020 Course Introduction
 
L01 Introduction
L01 IntroductionL01 Introduction
L01 Introduction
 
L23 Robotics and Drones
L23 Robotics and Drones L23 Robotics and Drones
L23 Robotics and Drones
 
L22 Augmented and Virtual Reality
L22 Augmented and Virtual RealityL22 Augmented and Virtual Reality
L22 Augmented and Virtual Reality
 
L20 Personalised World
L20 Personalised WorldL20 Personalised World
L20 Personalised World
 
L19 Network Platforms
L19 Network PlatformsL19 Network Platforms
L19 Network Platforms
 
L18 Big Data and Analytics
L18 Big Data and AnalyticsL18 Big Data and Analytics
L18 Big Data and Analytics
 
L17 Algorithms and AI
L17 Algorithms and AIL17 Algorithms and AI
L17 Algorithms and AI
 
L16 Internet of Things
L16 Internet of ThingsL16 Internet of Things
L16 Internet of Things
 
L14 From the Internet to Blockchain
L14 From the Internet to BlockchainL14 From the Internet to Blockchain
L14 From the Internet to Blockchain
 
L14 The Mobile Revolution
L14 The Mobile RevolutionL14 The Mobile Revolution
L14 The Mobile Revolution
 
New Technology 2019 L13 Rise of the Machine
New Technology 2019 L13 Rise of the Machine New Technology 2019 L13 Rise of the Machine
New Technology 2019 L13 Rise of the Machine
 
L12 digital transformation
L12 digital transformationL12 digital transformation
L12 digital transformation
 
L10 The Innovator's Dilemma
L10 The Innovator's DilemmaL10 The Innovator's Dilemma
L10 The Innovator's Dilemma
 
L09 Disruptive Technology
L09 Disruptive TechnologyL09 Disruptive Technology
L09 Disruptive Technology
 
L09 Technological Revolutions
L09 Technological RevolutionsL09 Technological Revolutions
L09 Technological Revolutions
 
L07 Becoming Invisible
L07 Becoming InvisibleL07 Becoming Invisible
L07 Becoming Invisible
 
L06 Diffusion of Innovation
L06 Diffusion of InnovationL06 Diffusion of Innovation
L06 Diffusion of Innovation
 

Recently uploaded

HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAUHCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
panagenda
 
zkStudyClub - LatticeFold: A Lattice-based Folding Scheme and its Application...
zkStudyClub - LatticeFold: A Lattice-based Folding Scheme and its Application...zkStudyClub - LatticeFold: A Lattice-based Folding Scheme and its Application...
zkStudyClub - LatticeFold: A Lattice-based Folding Scheme and its Application...
Alex Pruden
 
Monitoring and Managing Anomaly Detection on OpenShift.pdf
Monitoring and Managing Anomaly Detection on OpenShift.pdfMonitoring and Managing Anomaly Detection on OpenShift.pdf
Monitoring and Managing Anomaly Detection on OpenShift.pdf
Tosin Akinosho
 
HCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAUHCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAU
panagenda
 
Skybuffer SAM4U tool for SAP license adoption
Skybuffer SAM4U tool for SAP license adoptionSkybuffer SAM4U tool for SAP license adoption
Skybuffer SAM4U tool for SAP license adoption
Tatiana Kojar
 
Columbus Data & Analytics Wednesdays - June 2024
Columbus Data & Analytics Wednesdays - June 2024Columbus Data & Analytics Wednesdays - June 2024
Columbus Data & Analytics Wednesdays - June 2024
Jason Packer
 
AWS Cloud Cost Optimization Presentation.pptx
AWS Cloud Cost Optimization Presentation.pptxAWS Cloud Cost Optimization Presentation.pptx
AWS Cloud Cost Optimization Presentation.pptx
HarisZaheer8
 
Presentation of the OECD Artificial Intelligence Review of Germany
Presentation of the OECD Artificial Intelligence Review of GermanyPresentation of the OECD Artificial Intelligence Review of Germany
Presentation of the OECD Artificial Intelligence Review of Germany
innovationoecd
 
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
saastr
 
JavaLand 2024: Application Development Green Masterplan
JavaLand 2024: Application Development Green MasterplanJavaLand 2024: Application Development Green Masterplan
JavaLand 2024: Application Development Green Masterplan
Miro Wengner
 
Digital Banking in the Cloud: How Citizens Bank Unlocked Their Mainframe
Digital Banking in the Cloud: How Citizens Bank Unlocked Their MainframeDigital Banking in the Cloud: How Citizens Bank Unlocked Their Mainframe
Digital Banking in the Cloud: How Citizens Bank Unlocked Their Mainframe
Precisely
 
Nordic Marketo Engage User Group_June 13_ 2024.pptx
Nordic Marketo Engage User Group_June 13_ 2024.pptxNordic Marketo Engage User Group_June 13_ 2024.pptx
Nordic Marketo Engage User Group_June 13_ 2024.pptx
MichaelKnudsen27
 
Generating privacy-protected synthetic data using Secludy and Milvus
Generating privacy-protected synthetic data using Secludy and MilvusGenerating privacy-protected synthetic data using Secludy and Milvus
Generating privacy-protected synthetic data using Secludy and Milvus
Zilliz
 
leewayhertz.com-AI in predictive maintenance Use cases technologies benefits ...
leewayhertz.com-AI in predictive maintenance Use cases technologies benefits ...leewayhertz.com-AI in predictive maintenance Use cases technologies benefits ...
leewayhertz.com-AI in predictive maintenance Use cases technologies benefits ...
alexjohnson7307
 
Best 20 SEO Techniques To Improve Website Visibility In SERP
Best 20 SEO Techniques To Improve Website Visibility In SERPBest 20 SEO Techniques To Improve Website Visibility In SERP
Best 20 SEO Techniques To Improve Website Visibility In SERP
Pixlogix Infotech
 
Main news related to the CCS TSI 2023 (2023/1695)
Main news related to the CCS TSI 2023 (2023/1695)Main news related to the CCS TSI 2023 (2023/1695)
Main news related to the CCS TSI 2023 (2023/1695)
Jakub Marek
 
Skybuffer AI: Advanced Conversational and Generative AI Solution on SAP Busin...
Skybuffer AI: Advanced Conversational and Generative AI Solution on SAP Busin...Skybuffer AI: Advanced Conversational and Generative AI Solution on SAP Busin...
Skybuffer AI: Advanced Conversational and Generative AI Solution on SAP Busin...
Tatiana Kojar
 
Serial Arm Control in Real Time Presentation
Serial Arm Control in Real Time PresentationSerial Arm Control in Real Time Presentation
Serial Arm Control in Real Time Presentation
tolgahangng
 
Programming Foundation Models with DSPy - Meetup Slides
Programming Foundation Models with DSPy - Meetup SlidesProgramming Foundation Models with DSPy - Meetup Slides
Programming Foundation Models with DSPy - Meetup Slides
Zilliz
 
WeTestAthens: Postman's AI & Automation Techniques
WeTestAthens: Postman's AI & Automation TechniquesWeTestAthens: Postman's AI & Automation Techniques
WeTestAthens: Postman's AI & Automation Techniques
Postman
 

Recently uploaded (20)

HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAUHCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
 
zkStudyClub - LatticeFold: A Lattice-based Folding Scheme and its Application...
zkStudyClub - LatticeFold: A Lattice-based Folding Scheme and its Application...zkStudyClub - LatticeFold: A Lattice-based Folding Scheme and its Application...
zkStudyClub - LatticeFold: A Lattice-based Folding Scheme and its Application...
 
Monitoring and Managing Anomaly Detection on OpenShift.pdf
Monitoring and Managing Anomaly Detection on OpenShift.pdfMonitoring and Managing Anomaly Detection on OpenShift.pdf
Monitoring and Managing Anomaly Detection on OpenShift.pdf
 
HCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAUHCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAU
 
Skybuffer SAM4U tool for SAP license adoption
Skybuffer SAM4U tool for SAP license adoptionSkybuffer SAM4U tool for SAP license adoption
Skybuffer SAM4U tool for SAP license adoption
 
Columbus Data & Analytics Wednesdays - June 2024
Columbus Data & Analytics Wednesdays - June 2024Columbus Data & Analytics Wednesdays - June 2024
Columbus Data & Analytics Wednesdays - June 2024
 
AWS Cloud Cost Optimization Presentation.pptx
AWS Cloud Cost Optimization Presentation.pptxAWS Cloud Cost Optimization Presentation.pptx
AWS Cloud Cost Optimization Presentation.pptx
 
Presentation of the OECD Artificial Intelligence Review of Germany
Presentation of the OECD Artificial Intelligence Review of GermanyPresentation of the OECD Artificial Intelligence Review of Germany
Presentation of the OECD Artificial Intelligence Review of Germany
 
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
 
JavaLand 2024: Application Development Green Masterplan
JavaLand 2024: Application Development Green MasterplanJavaLand 2024: Application Development Green Masterplan
JavaLand 2024: Application Development Green Masterplan
 
Digital Banking in the Cloud: How Citizens Bank Unlocked Their Mainframe
Digital Banking in the Cloud: How Citizens Bank Unlocked Their MainframeDigital Banking in the Cloud: How Citizens Bank Unlocked Their Mainframe
Digital Banking in the Cloud: How Citizens Bank Unlocked Their Mainframe
 
Nordic Marketo Engage User Group_June 13_ 2024.pptx
Nordic Marketo Engage User Group_June 13_ 2024.pptxNordic Marketo Engage User Group_June 13_ 2024.pptx
Nordic Marketo Engage User Group_June 13_ 2024.pptx
 
Generating privacy-protected synthetic data using Secludy and Milvus
Generating privacy-protected synthetic data using Secludy and MilvusGenerating privacy-protected synthetic data using Secludy and Milvus
Generating privacy-protected synthetic data using Secludy and Milvus
 
leewayhertz.com-AI in predictive maintenance Use cases technologies benefits ...
leewayhertz.com-AI in predictive maintenance Use cases technologies benefits ...leewayhertz.com-AI in predictive maintenance Use cases technologies benefits ...
leewayhertz.com-AI in predictive maintenance Use cases technologies benefits ...
 
Best 20 SEO Techniques To Improve Website Visibility In SERP
Best 20 SEO Techniques To Improve Website Visibility In SERPBest 20 SEO Techniques To Improve Website Visibility In SERP
Best 20 SEO Techniques To Improve Website Visibility In SERP
 
Main news related to the CCS TSI 2023 (2023/1695)
Main news related to the CCS TSI 2023 (2023/1695)Main news related to the CCS TSI 2023 (2023/1695)
Main news related to the CCS TSI 2023 (2023/1695)
 
Skybuffer AI: Advanced Conversational and Generative AI Solution on SAP Busin...
Skybuffer AI: Advanced Conversational and Generative AI Solution on SAP Busin...Skybuffer AI: Advanced Conversational and Generative AI Solution on SAP Busin...
Skybuffer AI: Advanced Conversational and Generative AI Solution on SAP Busin...
 
Serial Arm Control in Real Time Presentation
Serial Arm Control in Real Time PresentationSerial Arm Control in Real Time Presentation
Serial Arm Control in Real Time Presentation
 
Programming Foundation Models with DSPy - Meetup Slides
Programming Foundation Models with DSPy - Meetup SlidesProgramming Foundation Models with DSPy - Meetup Slides
Programming Foundation Models with DSPy - Meetup Slides
 
WeTestAthens: Postman's AI & Automation Techniques
WeTestAthens: Postman's AI & Automation TechniquesWeTestAthens: Postman's AI & Automation Techniques
WeTestAthens: Postman's AI & Automation Techniques
 

L09 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 Framework Your Code Domain ?
  • 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 Framework Spring API Patterns JEE/.NET Patterns API Definition JEE/.NET API
  • 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. EXERCISE Write down the pros and cons (benefits and drawbacks) for frameworks. Use two columns, benefits on the left, drawbacks right
  • 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. 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
  • 17. 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
  • 18. Dependency Injection  Fowler’s Naive Example – MovieLister uses a finder class class MovieLister... Separate what varies 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()]); } – How can we separate the finder functionality? REMEMBER PROGRAM TO INTERFACES PRINSIPLE?
  • 19. Dependency Injection  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.
  • 20. Dependency Injection  An assembler (or container) is used to create an implementation – Using constructor injection, the assember will create a MovieLister and passing a MovieFinder interface in the contructor – Using setter injection, the assembler will create MovieLister and then all the setFinder setter method to provide the MovieFinder interface
  • 21. 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; }
  • 23. 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); } }
  • 24. Example  ContentFinder interface public interface ContentFinder { List<Content> find(String pattern); }
  • 25. 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; } }
  • 26. 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 Simpsons Movie", "", "", new Date(), contentService.addContent(new Content(1, "The Bourne Ultimatum", "", "", new Date(), contentService.addContent(new Content(1, "Rush Hour 3", "", "", 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
  • 28. 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
  • 30. 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); ... } }
  • 31. Template Method Pattern AbstractOrderEJB placeOrder () abstract getItemPrice() abstract getSpendingLimit() abstract placeOrder() MyOrderEJB getItemPrice() getSpendingLimit() placeOrder() extends Generic functionality Domain specific functionality
  • 32. Template Method Pattern public class MyOrderEJB extends AbstractOrderEJB { ... int getItemPrice(int[] i) { ... } int getSpendingLimit(int customerId) { ... } int placeOrder(int customerId, int total, int items) { ... } }
  • 33. 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
  • 34. Strategy 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
  • 35. 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
  • 37. Strategy Pattern  Interface for specific functionality public interface DataHelper { int getItemPrice(InvoiceItem item); int getSpendingLimit(CustomerId) throws NoSuchCustomerException; int palceOrder(int customerId, int total, InvoiceItem[] items);  Generic class uses the interface – Set method to inject the interface } private DataHelper dataHelper; public void setDataHelper(DataHelper newDataHelper) { this.dataHelper = newDataHelper; } DEPENDENCY INJECTION
  • 38. 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); ... } }
  • 39. 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
  • 40. 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 ✔
  • 41. 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

Editor's Notes

  1. 2
  2. 3
  3. 5
  4. 6
  5. 7
  6. 9
  7. 10
  8. 13