Lecture 10 
Using Frameworks
Agenda 
 From Problem to Patterns 
– Implementing a simple game 
 Template Method 
 Strategy 
 Dependency Injection
Reading 
 Dependency Injection 
 Template Method Pattern 
 Strategy Pattern 
 Spring Framework (video) 
 Article by Fowler 
– Inversion of Control Containers and the Dependency 
Injection pattern
From Problem to Patterns
Framework design 
 Inheritance of framework classes 
Template Method – class Inheritance 
 Composition of framework classes 
Strategy – interface Inheritance 
Dependency Injection 
Framework 
Your Code 
Domain ?
From Problem to Pattern 
We need to design game software 
Common turn-based board games like monopoly, 
chess, backgammon, yatzy etc. 
You must propose a design
From Problem to Pattern 
Let’s make a Game Framework 
What patterns can we use?
Patterns 
 Template Method 
– Template of an algorithm 
– Based on class inheritance 
 Strategy 
– Composition of an strategy 
– Based on interface inheritance
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
What is the game algorithm? 
initialize 
while more plays 
make one turn 
print winnner 
void initializeGame(); 
boolean endOfGame(); 
void makePlay(int player); 
void printWinner();
Game Template 
extends 
Specific Game 
This is the generic 
template of the game 
play 
This is the specific details 
for this specific game
Design 
interface 
Game 
void initializeGame 
void makePlay (int player) 
boolean endOfGame 
void printWinner 
AbstractGame 
playOneGame 
(int playerCount) 
implements 
implements 
Chess 
void initializeGame 
void makePlay(int player) 
boolean endOfGame 
void printWinner
Interface for game algorithm 
package is.ru.honn.game.framework; 
public interface Game 
{ 
public void initializeGame(); 
public void makePlay(int player); 
public boolean endOfGame(); 
public void printWinner(); 
}
The Template 
package is.ru.honn.game.framework; 
public abstract class AbstractGame implements Game 
{ 
protected int playersCount; 
public final void playOneGame(int playersCount) 
{ 
this.playersCount = playersCount; 
initializeGame(); 
int j = 0; 
while (!endOfGame()) { 
makePlay(j); 
j = (j + 1) % playersCount; 
} 
printWinner(); 
} 
}
The Specific Game 
class Chess extends AbstractGame 
{ 
public void initializeGame() 
{ 
// Initialize players, put the pieces on the board 
} 
public void makePlay(int player) 
{ 
// Process a turn for the player 
} 
public boolean endOfGame() 
{ 
// Return true if in Checkmate or stalemate 
return true; 
} 
public void printWinner() 
{ 
// Display the winning player 
} 
}
Design 
interface 
Game 
void initializeGame 
void makePlay (int player) 
boolean endOfGame 
void printWinner 
AbstractGame 
playOneGame 
(int playerCount) 
implements 
implements 
Chess 
void initializeGame 
void makePlay(int player) 
boolean endOfGame 
void printWinner
Redesign 
Let’s use Strategy instead 
Why would we do that?
Strategy Pattern 
Create a template for the steps of an algorithm 
and inject the specific functionality (strategy) 
 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
Game Strategy 
implements 
Specific Game 
This is the generic 
strategy of the game 
play 
This is the specific details 
for this specific game
Design 
interface 
GameStrategy 
void initializeGame 
void makePlay (int player) 
boolean endOfGame 
void printWinner 
GamePlay 
GamaStrategy strategy 
playOneGame (int playerCount) 
implements 
ChessStrategy 
uses 
void initializeGame 
void makePlay(int player) 
boolean endOfGame 
void printWinner 
The ChessStrategy 
will be injected into 
the game (context) 
Assember
The Strategy 
package is.ru.honn.game.framework; 
public interface GameStrategy 
{ 
public void initializeGame(); 
public void makePlay(int player); 
public boolean endOfGame(); 
public void printWinner(); 
}
The Specific Strategy 
class ChessStrategy implements GameStrategy 
{ 
public void initializeGame() 
{ 
// Initialize players, put the pieces on the board 
} 
public void makePlay(int player) 
{ 
// Process a turn for the player 
} 
public boolean endOfGame() 
{ 
// Return true if in Checkmate or stalemate 
return true; 
} 
public void printWinner() 
{ 
// Display the winning player 
} 
}
The Context 
public class GamePlay 
{ 
GameStrategy strategy; 
protected int playersCount; 
public void setStrategy(GameStrategy strategy) 
{ 
this.strategy = strategy; 
} 
public final void playOneGame(int playersCount) 
{ 
this.playersCount = playersCount; 
this.strategy.initializeGame(); 
int j = 0; 
while (!this.strategy.endOfGame()) { 
this.strategy.makePlay(j); 
j = (j + 1) % playersCount; 
} 
this.strategy.printWinner(); 
} 
} 
Polymorphism
The Assembler 
GamePlay play = new GamePlay(); 
// Assign the right strategy 
play.setStrategy(new ChessStrategy()); 
What design pattern is used 
when the strategy is assigned 
to the context?
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 
Assembler 
GamePlay play = new GamePlay() 
play.setStrategy(new ChessStrategy()) 
ChessStrategy 
initializeGame… 
interface GameStrategy 
initializeGame(); 
makePlay(int player); 
endOfGame(); 
printWinner(); 
GamePlay 
create 
setStrategy(GameStrategy strategy) 
playOneGame(int playersCount) 
… 
this.strategy.initializeGame(); 
implements 
uses 
Framework 
create
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 
public class AppTest 
{ 
public static void main(String[] args) 
{ 
ApplicationContext ctx = 
new FileSystemXmlApplicationContext("app.xml"); 
 Application contexts are intended as central 
registries 
– Support of hierarchical contexts (nested) 
} 
}
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

L10 Using Frameworks

  • 1.
    Lecture 10 UsingFrameworks
  • 2.
    Agenda  FromProblem to Patterns – Implementing a simple game  Template Method  Strategy  Dependency Injection
  • 3.
    Reading  DependencyInjection  Template Method Pattern  Strategy Pattern  Spring Framework (video)  Article by Fowler – Inversion of Control Containers and the Dependency Injection pattern
  • 4.
  • 5.
    Framework design Inheritance of framework classes Template Method – class Inheritance  Composition of framework classes Strategy – interface Inheritance Dependency Injection Framework Your Code Domain ?
  • 6.
    From Problem toPattern We need to design game software Common turn-based board games like monopoly, chess, backgammon, yatzy etc. You must propose a design
  • 7.
    From Problem toPattern Let’s make a Game Framework What patterns can we use?
  • 8.
    Patterns  TemplateMethod – Template of an algorithm – Based on class inheritance  Strategy – Composition of an strategy – Based on interface inheritance
  • 9.
    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
  • 10.
    What is thegame algorithm? initialize while more plays make one turn print winnner void initializeGame(); boolean endOfGame(); void makePlay(int player); void printWinner();
  • 11.
    Game Template extends Specific Game This is the generic template of the game play This is the specific details for this specific game
  • 12.
    Design interface Game void initializeGame void makePlay (int player) boolean endOfGame void printWinner AbstractGame playOneGame (int playerCount) implements implements Chess void initializeGame void makePlay(int player) boolean endOfGame void printWinner
  • 13.
    Interface for gamealgorithm package is.ru.honn.game.framework; public interface Game { public void initializeGame(); public void makePlay(int player); public boolean endOfGame(); public void printWinner(); }
  • 14.
    The Template packageis.ru.honn.game.framework; public abstract class AbstractGame implements Game { protected int playersCount; public final void playOneGame(int playersCount) { this.playersCount = playersCount; initializeGame(); int j = 0; while (!endOfGame()) { makePlay(j); j = (j + 1) % playersCount; } printWinner(); } }
  • 15.
    The Specific Game class Chess extends AbstractGame { public void initializeGame() { // Initialize players, put the pieces on the board } public void makePlay(int player) { // Process a turn for the player } public boolean endOfGame() { // Return true if in Checkmate or stalemate return true; } public void printWinner() { // Display the winning player } }
  • 16.
    Design interface Game void initializeGame void makePlay (int player) boolean endOfGame void printWinner AbstractGame playOneGame (int playerCount) implements implements Chess void initializeGame void makePlay(int player) boolean endOfGame void printWinner
  • 17.
    Redesign Let’s useStrategy instead Why would we do that?
  • 18.
    Strategy Pattern Createa template for the steps of an algorithm and inject the specific functionality (strategy)  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
  • 19.
  • 20.
    Game Strategy implements Specific Game This is the generic strategy of the game play This is the specific details for this specific game
  • 21.
    Design interface GameStrategy void initializeGame void makePlay (int player) boolean endOfGame void printWinner GamePlay GamaStrategy strategy playOneGame (int playerCount) implements ChessStrategy uses void initializeGame void makePlay(int player) boolean endOfGame void printWinner The ChessStrategy will be injected into the game (context) Assember
  • 22.
    The Strategy packageis.ru.honn.game.framework; public interface GameStrategy { public void initializeGame(); public void makePlay(int player); public boolean endOfGame(); public void printWinner(); }
  • 23.
    The Specific Strategy class ChessStrategy implements GameStrategy { public void initializeGame() { // Initialize players, put the pieces on the board } public void makePlay(int player) { // Process a turn for the player } public boolean endOfGame() { // Return true if in Checkmate or stalemate return true; } public void printWinner() { // Display the winning player } }
  • 24.
    The Context publicclass GamePlay { GameStrategy strategy; protected int playersCount; public void setStrategy(GameStrategy strategy) { this.strategy = strategy; } public final void playOneGame(int playersCount) { this.playersCount = playersCount; this.strategy.initializeGame(); int j = 0; while (!this.strategy.endOfGame()) { this.strategy.makePlay(j); j = (j + 1) % playersCount; } this.strategy.printWinner(); } } Polymorphism
  • 25.
    The Assembler GamePlayplay = new GamePlay(); // Assign the right strategy play.setStrategy(new ChessStrategy()); What design pattern is used when the strategy is assigned to the context?
  • 26.
    Dependency Injection Removesexplicit 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
  • 27.
    Dependency Injection Assembler GamePlay play = new GamePlay() play.setStrategy(new ChessStrategy()) ChessStrategy initializeGame… interface GameStrategy initializeGame(); makePlay(int player); endOfGame(); printWinner(); GamePlay create setStrategy(GameStrategy strategy) playOneGame(int playersCount) … this.strategy.initializeGame(); implements uses Framework create
  • 28.
  • 29.
    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
  • 30.
    Overview Spring 1– Introduction
  • 31.
    Lightweight Containers Manage objects  Provide context
  • 32.
    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
  • 33.
    Spring Containers The concept – Building applications from POJOs
  • 34.
    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
  • 35.
    FileSystemXmlApplicationContext  Loadsthe context from an XML file public class AppTest { public static void main(String[] args) { ApplicationContext ctx = new FileSystemXmlApplicationContext("app.xml");  Application contexts are intended as central registries – Support of hierarchical contexts (nested) } }
  • 36.
    Summary  Frameworkpatterns – Inversion of Control and Dependency Injection – Template Method – Strategy  From problems to patterns – Game Framework  Spring framework – Bean containers – BeanFactory and ApplicationContext