Title: MOD251 Modern Software Development Methods
1MOD251 Modern Software Development Methods
- Command Template Method - Strategy
2Content / Agenda
- Patterns intro
- Command Pattern
- Template Method Pattern
- Strategy Pattern
3What are patterns
- A pattern is a general repeatable solution to a
commonly-occurring problem. - Patterns originated as an architectural
(buildings) concept by Christopher Alexander. - There exists patterns for many areas, e.g. in
education or risk management. - Patterns are named, categorized, described in a
defined format and shared in pattern catalogs (or
books), e.g. http//hillside.net/patterns/.
4What are design patterns
- Applying patterns in software development became
popular with the book Design Patterns Elements
of Reusable Object-Oriented Software by Gamma et
al (1994), also called Gang of Four or GoF
referring to its authors. - Design patterns are patterns used in software
design. See - http//www.vico.org/pages/PatronsDisseny.html
- or
- http//en.wikipedia.org/wiki/Design_pattern_(com
puter_science) - for examples.
5Classification of Design Patterns
- Creational Patterns
- deal with initializing and configuring classes
and objects - how to create the objects
- Structural Patterns
- deal with decoupling the interface and
implementation of classes and objects - how classes and objects are composed to build
larger structures - Behavioral Patterns
- deal with dynamic interactions among societies of
classes and objects - how to manage complex control flows
(communications)
6Presentation of patterns
- I will try to use a common structure when
presenting the patterns - Synopsis a short text (and figure) describing
the pattern - Context some typically uses of the pattern
- Forces the motivation for the pattern (the
problem) - Solution a description of the pattern
- Consequences the consequences of applying the
pattern - Implementation implementation issues
7Command Pattern
8Command Pattern - Synopsis
- Encapsulate commands in objects so that you can
control their selection and sequencing, queue
them, undo them, and otherwise manipulate them.
ltltinterfacegtgt Command do() ltmoregt()
9Command Pattern - Context
- A word processing program with do / redo / undo
capabilities. All commands could implement the
same interface and be stacked to keep the
history. - A system with sensors and switches. The
configuration (wiring) could be done in a config
file. - A transaction system (e.g. payroll case study).
The command interface could have methods for
validating and for executing the transaction. - A Servlet controller that needs to choose an
action to perform based on request parameters.
10Command Pattern - Forces
- You need to control the sequencing, selection or
timing of command execution. - You need to maintain a persistent log of commands
executed. - You need to be able to add new commands in an
agile manner (OCP).
11Command Pattern - Solution
- AbstractCommand Superclass (or interface) for
all commands. - ConcreteCommand A specific command.
- Invoker Creates and invokes commands.
- CommandManager To manage a collection of
commands (optional).
12Command Pattern - Consequences
- Physical decoupling. Decoupling of procurement,
validation and operation on data. Better
modularity and possibilities for reuse. - Temporal decoupling. Decoupling in time. Commands
(operations on data) can be sequenced, persisted
and executed later. - General decoupling of client- and server-objects
by using a consistent interface! - Commands are objects. Can contain their own data
and have their own (additional) operations. - Adding new commands conforms to the OCP.
13Command Pattern - Implementation
-
- To make a good decoupling of the invoker and the
command, concrete commands should not be created
directly by the invoker. Use a factory (by
passing the name of the command) to create the
commands.
14Command Pattern Code Example
- CommandPatternExample (Eclipse Project)
15Template Method Pattern
16Template Method Pattern - Synopsis
- Write an abstract class that contains only part
of the logic needed to accomplish its purpose. - Organize the class so that its concrete
(template) methods call abstract methods where
the missing logic would have appeared. - Provide the missing logic in subclass methods
that override the abstract methods.
17Template Method Pattern - Context
- A reusable logon class that
- Prompts the user for a user ID and password
- Authenticates the user ID and password
- Reports to the UI the logon operation progress
- Notifies the application that logon is complete
by providing an authentication object - 1 and 3 can be app independent (and can be
implemented directly), while 2 and 4 are app
dependent (and will be implemented as abstract
methods). - A picking algorithm that includes a sorting step
- Choosing a concrete sorting algorithm is deferred
by templating the picking algorithm (making
sorting an abstract method).
18Template Method Pattern - Forces
- You have to design a class that will be used in
multiple programs. The overall structure of the
class is always the same, but portions of it will
vary. You want to ensure that logic is supplied
for the missing (varying) parts. Making the class
abstract will accomplish this. - You have a number of classes with similar
behavior and want to minimize code duplication.
You factor out commonalities in a superclass and
abstract the differences. Subclasses implement
the abstract methods.
19Template Method Pattern - Solution
- AbstractTemplate The abstract class providing
the template method - ConcreteTemplate The concrete class that
implements the missing parts of the template
method.
20Template Method Pattern - Consequences
- A programmer writing a subclass of an
AbstractTemplate class is forced to override
those methods that must be overridden to complete
the logic of the superclass.
21Template Method Pattern - Implementation
-
- AbstractTemplate can provide empty or default
implementations for some supplemental or optional
logic. These methods are called hook methods, and
can be overwritten by subclasses to customize
behavior.
22Template Method Pattern Related patterns
- Strategy A solution to similar problems using
delegation.
23Template Method Pattern Code Example
- TemplateMethodPatternExample (Eclipse Project)
24Strategy Pattern
25Strategy Pattern - Synopsis
- Encapsulate related algorithms in classes that
are subclasses of a common superclass. This
allows the selection of algorithms to vary by
object.
26Strategy Pattern - Context
- Suppose that you have to write a program that
displays calendars. The calendars should be able
to display holidays celebrated by different
nations and religious groups (chosen by the
user). You can do this by putting the logic for
each set of holidays in separate classes.
27Strategy Pattern - Forces
- A program has to provide multiple variations of
an algorithm or behavior. - You can encapsulate variations in separate
classes that provide a consistent access. - Clients dont have to know of the
implementations, only the common interface.
28Strategy Pattern - Solution
- Client The client that delegates an operation
to an abstract class or inerface. - AbstractStrategy Provides a common way to
access the operation encapsulated by its sub-/
implementing classes. - ConcreteStrategyX Implements the operation that
the Client delegates.
29Strategy Pattern - Consequences
- The Strategy pattern allows the behavior of
Client objects to be dynamically determined on a
per-object basis.
30Strategy Pattern - Implementation
31Strategy Pattern Code Example
- StrategyPatternExample (Eclipse Project)
32References
- Robert Martin Agile Software Development
- Mark Grand Patterns in Java, Volume 1
33Next
- Tomorrow Presentations exercise 1
- Next week More patterns Factory, Composite,
Observer, MVC, Proxy