+ All Categories
Home > Technology > L10 Using Frameworks

L10 Using Frameworks

Date post: 06-Feb-2015
Category:
Upload: olafur-andri-ragnarsson
View: 102 times
Download: 2 times
Share this document with a friend
Description:
Höldum áfram með umræðu um ramma eða frameworks. Í þessum fyrirlestri skoðum við vandamál sem má leysa með ramma.
36
Lecture 10 Using Frameworks
Transcript
Page 1: L10 Using Frameworks

Lecture 10Using Frameworks

Page 2: L10 Using Frameworks

Agenda From Problem to Patterns

– Implementing a simple game Template Method Strategy Dependency Injection

Page 3: L10 Using Frameworks

Reading Dependency Injection Template Method Pattern Strategy Pattern Spring Framework (video) Article by Fowler

– Inversion of Control Containers and the Dependency Injection pattern

Page 4: L10 Using Frameworks

From Problem to Patterns

Page 5: L10 Using Frameworks

Framework design Inheritance of framework classes

Template Method – class Inheritance Composition of framework classes

Strategy – interface InheritanceDependency Injection

FrameworkYour CodeDomain ?

Page 6: L10 Using Frameworks

From Problem to PatternWe need to design game software

Common turn-based board games like monopoly, chess, backgammon, yatzy etc.

You must propose a design

Page 7: L10 Using Frameworks

From Problem to PatternLet’s make a Game Framework

What patterns can we use?

Page 8: L10 Using Frameworks

Patterns Template Method

– Template of an algorithm– Based on class inheritance

Strategy– Composition of an strategy– Based on interface inheritance

Page 9: L10 Using Frameworks

Template Method PatternCreate 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

Page 10: L10 Using Frameworks

What is the game algorithm?

initialize

while more playsmake one turn

print winnner

void initializeGame();

boolean endOfGame();

void makePlay(int player);

void printWinner();

Page 11: L10 Using Frameworks

Game Template

Specific Game

extends

This is the generictemplate of the game play

This is the specific detailsfor this specific game

Page 12: L10 Using Frameworks

Design

interface Game

void initializeGamevoid makePlay (int player)boolean endOfGamevoid printWinner

AbstractGame

playOneGame (int playerCount)

imp

lem

ents

Chess

void initializeGamevoid makePlay(int player)boolean endOfGamevoid printWinner

implements

Page 13: L10 Using Frameworks

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();}

Page 14: L10 Using Frameworks

The Templatepackage 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(); }}

Page 15: L10 Using Frameworks

The Specific Gameclass 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 }}

Page 16: L10 Using Frameworks

Design

interface Game

void initializeGamevoid makePlay (int player)boolean endOfGamevoid printWinner

AbstractGame

playOneGame (int playerCount)

imp

lem

ents

Chess

void initializeGamevoid makePlay(int player)boolean endOfGamevoid printWinner

implements

Page 17: L10 Using Frameworks

RedesignLet’s use Strategy instead

Why would we do that?

Page 18: L10 Using Frameworks

Strategy PatternCreate 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

Page 19: L10 Using Frameworks

Strategy Pattern

Page 20: L10 Using Frameworks

Game Strategy

Specific Game

implements

This is the genericstrategy of the game play

This is the specific detailsfor this specific game

Page 21: L10 Using Frameworks

Design

interfaceGameStrategy

void initializeGamevoid makePlay (int player)boolean endOfGamevoid printWinner

GamePlay

GamaStrategy strategy

playOneGame (int playerCount)

ChessStrategy

void initializeGamevoid makePlay(int player)boolean endOfGamevoid printWinner

implements

uses

The ChessStrategywill be injected into the game (context)

Assember

Page 22: L10 Using Frameworks

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();}

Page 23: L10 Using Frameworks

The Specific Strategyclass 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 }}

Page 24: L10 Using Frameworks

The Contextpublic 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

Page 25: L10 Using Frameworks

The Assembler

GamePlay play = new GamePlay(); // Assign the right strategyplay.setStrategy(new ChessStrategy());

What design pattern is used when the strategy is assigned to the context?

Page 26: L10 Using Frameworks

Dependency InjectionRemoves 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

Page 27: L10 Using Frameworks

Dependency InjectionAssembler

GamePlay play = new GamePlay() play.setStrategy(new ChessStrategy())

ChessStrategy

initializeGame…

interface GameStrategy

initializeGame();makePlay(int player);endOfGame();printWinner();

GamePlay

setStrategy(GameStrategy strategy)

playOneGame(int playersCount) … this.strategy.initializeGame();

createimplements

uses

Framework

create

Page 28: L10 Using Frameworks

Spring Framework

Page 29: L10 Using Frameworks

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

Page 30: L10 Using Frameworks

Overview

Spring 1 – Introduction

Page 31: L10 Using Frameworks

Lightweight Containers Manage objects Provide context

Page 32: L10 Using Frameworks

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

Page 33: L10 Using Frameworks

Spring Containers The concept

– Building applications from POJOs

Page 34: L10 Using Frameworks

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

PersonThe Bean Factory usessetter injection to create theperson object

Page 35: L10 Using Frameworks

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"); }}

Page 36: L10 Using Frameworks

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


Recommended