Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
Lesson12 other behavioural patterns
1. Software Design Patterns
lesson 12: Other Behavioral patterns
Prepared
By
BAZZEKETA DATSUN
MIT(MAK)
Tel: 0705333525
Email: datsunbazzeketa@yahoo.com
By Bazzeketa Datsun 1
2. • A Strategy Pattern says that "define a family of
functionality, encapsulate each one, and make them
interchangeable".
• The Strategy Pattern is also known as Policy.
Strategy Pattern
3. 1. It provides a substitute to subclassing.
2. It defines each behavior within its own class,
eliminating the need for conditional statements.
3. It makes it easier to extend and incorporate new
behavior without changing the application.
Benefits of the Strategy Pattern
4. 1. When the multiple classes differ only in their
behaviors.e.g. Servlet API.
2. It is used when you need different variations of an
algorithm.
Usage of the Strategy Pattern
7. Step 1: Create a Strategy interface
//This is an interface.
public interface Strategy
{
public float calculation(float a, float b);
}
// End of the Strategy interface.
Code for Example Implementation of the Strategy Pattern
8. Step 2: Create a Addition class that will implement Strategy interface.
//This is a class.
public class Addition implements Strategy{
@Override
public float calculation(float a, float b) {
return a+b;
}
}
// End of the Addition class.
Code for Example Implementation of the Strategy Pattern
cont’d
9. • Step 3: Create a Subtraction class that will implement
Strategy interface.
//This is a class.
public class Subtraction implements Strategy {
@Override
public float calculation(float a, float b) {
return a-b;
} }// End of the Subtraction class.
Code for Example Implementation of the Strategy Pattern
cont’d
10. • Step 4: Create a Multiplication class that will implement
Strategy interface.
//This is a class.
public class Multiplication implements Strategy{
@Override
public float calculation(float a, float b){
return a*b;
}
}
// End of the Multiplication class.
Code for Example Implementation of the Strategy Pattern
cont’d
11. • Step 5: Create a Context class that will ask from Strategy
interface to execute the type of strategy.
//This is a class.
public class Context {
private Strategy strategy;
public Context(Strategy strategy)
{
this.strategy = strategy;
}
public float executeStrategy(float num1, float num2) {
return strategy.calculation(num1, num2);
} }
Code for Example Implementation of the Strategy Pattern
cont’d
12. • Step 6: Create a StartegyPatternDemo class.
Code for Example Implementation of the Strategy Pattern cont’d
14. • A Template Pattern says that "just define the skeleton
of a function in an operation, deferring some steps to
its subclasses".
Template Pattern
15. • Benefits of the Template Pattern
• It is very common technique for reusing the code.This
is only the main benefit of it.
• Usage of the Template Pattern
• It is used when the common behavior among sub-
classes should be moved to a single common class by
avoiding the duplication
Benefits and Usage of the Template Pattern
17. • Step 1: Create a Game abstract class.
//This is an abstract class.
public abstract class Game {
abstract void initialize();
abstract void start();
abstract void end();
public final void play(){
//initialize the game
initialize();
//start game
start();
//end game
end();
} }
// End of the Game abstract class.
Code for the UML Example of the Template Pattern
18. • Step 2: Create a Chess class that will extend Game abstract class for giving
the definition to its method.
//This is a class.
public class Chess extends Game {
@Override
void initialize() {
System.out.println("Chess Game Initialized! Start playing.");
}
@Override
void start() {
System.out.println("Game Started. Welcome to in the chess game!");
}
@Override
void end() {
System.out.println("Game Finished!");
Code for the UML Example of the Template Pattern cont’d
19. • Step 3: Create a Soccer class that will extend Game abstract class
for giving the definition to its method.
//This is a class.
public class Soccer extends Game {
@Override
void initialize() {
System.out.println("Soccer Game Initialized! Start playing.");
}
@Override
void start() {
System.out.println("Game Started. Welcome to in the Soccer game!"); }
@Override
void end() {
System.out.println("Game Finished!");
} } // End of the Soccer class.
Code for the UML Example of the Template Pattern cont’d
20. • Step 4: Create a TemplatePatternDemo class.
//This is a class.
public class TemplatePatternDemo {
public static void main(String[] args) throws InstantiationEx
ception, IllegalAccessException, ClassNotFoundException {
Class c=Class.forName(args[0]);
Game game=(Game) c.newInstance();
game.play();
} }
// End of the Soccer class.
Code for the UML Example of the Template Pattern cont’d
22. • According to GoF, Iterator Pattern is used "to access the
elements of an aggregate object sequentially without
exposing its underlying implementation".
• The Iterator pattern is also known as Cursor.
• In collection framework, we are now using Iterator that is
preferred over Enumeration
Iterator Pattern
23. 1. It supports variations in the traversal of a collection.
2. It simplifies the interface to the collection.
Advantages of the Iterator Pattern
24. • It is used:
1. When you want to access a collection of objects
without exposing its internal representation.
2. When there are multiple traversals of objects need to
be supported in the collection.
Example of Usage
• java.util.Iterator interface uses Iterator Design Pattern.
Usage of the Iterator Pattern
26. • Step 1: Create a Iterator interface.
public interface Iterator
{
public boolean hasNext();
public Object next();
}
Code for the UML Example of the Iterator Pattern
27. • Step 2: Create a Container interface
• //Iterator interface.
public interface Container {
public Iterator getIterator();
}
// End of the Iterator interface.
Code for the UML Example of the Iterator Pattern cont’d
28. • Step 3: Create a CollectionofNames class that will implement Container interface.
Code for the UML Example of the Iterator Pattern cont’d
29. • Step 4: Create a IteratorPatternDemo class.
public class IteratorPatternDemo {
public static void main(String[] args) {
CollectionofNames cmpnyRepository = new CollectionofNames();
for(Iterator iter = cmpnyRepository.getIterator(); iter.hasNext();) {
String name = (String)iter.next();
System.out.println("Name : " + name);
}
} }
Code for the UML Example of the Iterator Pattern cont’d
30. • Research about the Behavioral Patterns listed below;
1. Null Object pattern
2. OBJECT AUTHENTICATOR pattern (Protection Proxy
Pattern)
3. COMMON ATTRIBUTE REGISTRY pattern
Coursework 2
31. • Research about the Concurrency Patterns listed below;
1. Critical Section Pattern
2. Consistent Lock Order Pattern
3. Guarded Suspension Pattern
4. Read-Write Lock Pattern
Coursework 3-Concurrency patterns