SlideShare a Scribd company logo
Software Design Patterns
lesson 12: Other Behavioral patterns
Prepared
By
BAZZEKETA DATSUN
MIT(MAK)
Tel: 0705333525
Email: datsunbazzeketa@yahoo.com
By Bazzeketa Datsun 1
• 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
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
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
Example of the Strategy Pattern in Java
Example Implementation of the Strategy Pattern
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
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
• 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
• 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
• 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
• Step 6: Create a StartegyPatternDemo class.
Code for Example Implementation of the Strategy Pattern cont’d
Exercise
• A Template Pattern says that "just define the skeleton
of a function in an operation, deferring some steps to
its subclasses".
Template Pattern
• 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
UML Example of the Template Pattern
• 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
• 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
• 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
• 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
Exercise
• 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
1. It supports variations in the traversal of a collection.
2. It simplifies the interface to the collection.
Advantages of the Iterator Pattern
• 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
UML Example of the Iterator Pattern
• Step 1: Create a Iterator interface.
public interface Iterator
{
public boolean hasNext();
public Object next();
}
Code for the UML Example of the Iterator Pattern
• 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
• Step 3: Create a CollectionofNames class that will implement Container interface.
Code for the UML Example of the Iterator Pattern cont’d
• 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
• 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
• 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
THE END

More Related Content

PPT
Lesson10 behavioral patterns
PPT
Lesson11 more behavioural patterns
PPT
Strategy and Template Pattern
PDF
Behavioral Design Patterns
PPT
Singleton design pattern
PPTX
Java interface
PPT
Command Design Pattern
PDF
Design Patterns Illustrated
Lesson10 behavioral patterns
Lesson11 more behavioural patterns
Strategy and Template Pattern
Behavioral Design Patterns
Singleton design pattern
Java interface
Command Design Pattern
Design Patterns Illustrated

What's hot (20)

PPTX
PATTERNS03 - Behavioural Design Patterns
PPT
Strategy Design Pattern
PPTX
Top 20 c# interview Question and answers
PPTX
Std 12 computer chapter 8 classes and object in java (part 2)
PDF
Intake 37 2
PPT
Java interface
DOCX
C# interview quesions
PPT
Java interfaces
PPT
Chapter 9 Interface
PPTX
Interface java
PPTX
Polymorphism presentation in java
PPTX
C# interview
PPT
Interfaces
PPT
Interfaces In Java
PPS
Jump start to OOP, OOAD, and Design Pattern
PPT
Oo Design And Patterns
DOC
PPTX
Polymorphism in java
PPTX
Method overloading and constructor overloading in java
PPTX
java interface and packages
PATTERNS03 - Behavioural Design Patterns
Strategy Design Pattern
Top 20 c# interview Question and answers
Std 12 computer chapter 8 classes and object in java (part 2)
Intake 37 2
Java interface
C# interview quesions
Java interfaces
Chapter 9 Interface
Interface java
Polymorphism presentation in java
C# interview
Interfaces
Interfaces In Java
Jump start to OOP, OOAD, and Design Pattern
Oo Design And Patterns
Polymorphism in java
Method overloading and constructor overloading in java
java interface and packages
Ad

Similar to Lesson12 other behavioural patterns (20)

PPTX
Design pattern
PDF
Design Patterns
PDF
design_pattern_tutorial.pdf
PDF
Design pattern tutorial
PDF
Applying Design Patterns in Practice
PPTX
Design pattern - part 3
PPTX
Presentation on Template Method Design Pattern
PPTX
Gof design patterns
PDF
Design patterns through refactoring
PPT
Iterator pattern
PPTX
Design Patterns
PPTX
Design pattern
PDF
Design Patterns
PPTX
design patter related ppt and presentation
PDF
GOF Design pattern with java
PPSX
Prophecy Of Design Patterns
PDF
Unified Modeling Language (UML), Object-Oriented Programming Concepts & Desig...
PPTX
Let us understand design pattern
PDF
dotnet stuff.com tutorials-design-patterns_exploring-net-design-patterns-in-s...
PPTX
Software Architecture and Design Patterns Notes.pptx
Design pattern
Design Patterns
design_pattern_tutorial.pdf
Design pattern tutorial
Applying Design Patterns in Practice
Design pattern - part 3
Presentation on Template Method Design Pattern
Gof design patterns
Design patterns through refactoring
Iterator pattern
Design Patterns
Design pattern
Design Patterns
design patter related ppt and presentation
GOF Design pattern with java
Prophecy Of Design Patterns
Unified Modeling Language (UML), Object-Oriented Programming Concepts & Desig...
Let us understand design pattern
dotnet stuff.com tutorials-design-patterns_exploring-net-design-patterns-in-s...
Software Architecture and Design Patterns Notes.pptx
Ad

Recently uploaded (20)

PDF
Product Update: Alluxio AI 3.7 Now with Sub-Millisecond Latency
PPTX
Monitoring Stack: Grafana, Loki & Promtail
PDF
Autodesk AutoCAD Crack Free Download 2025
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
PDF
Odoo Companies in India – Driving Business Transformation.pdf
PDF
iTop VPN 6.5.0 Crack + License Key 2025 (Premium Version)
PDF
AI-Powered Threat Modeling: The Future of Cybersecurity by Arun Kumar Elengov...
PDF
Design an Analysis of Algorithms II-SECS-1021-03
PDF
iTop VPN Crack Latest Version Full Key 2025
PDF
Wondershare Filmora 15 Crack With Activation Key [2025
PDF
How AI/LLM recommend to you ? GDG meetup 16 Aug by Fariman Guliev
PDF
wealthsignaloriginal-com-DS-text-... (1).pdf
PPTX
Weekly report ppt - harsh dattuprasad patel.pptx
PDF
Adobe Illustrator 28.6 Crack My Vision of Vector Design
PDF
Salesforce Agentforce AI Implementation.pdf
PDF
17 Powerful Integrations Your Next-Gen MLM Software Needs
PDF
Designing Intelligence for the Shop Floor.pdf
PDF
iTop VPN Free 5.6.0.5262 Crack latest version 2025
PDF
CCleaner Pro 6.38.11537 Crack Final Latest Version 2025
PDF
CapCut Video Editor 6.8.1 Crack for PC Latest Download (Fully Activated) 2025
Product Update: Alluxio AI 3.7 Now with Sub-Millisecond Latency
Monitoring Stack: Grafana, Loki & Promtail
Autodesk AutoCAD Crack Free Download 2025
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
Odoo Companies in India – Driving Business Transformation.pdf
iTop VPN 6.5.0 Crack + License Key 2025 (Premium Version)
AI-Powered Threat Modeling: The Future of Cybersecurity by Arun Kumar Elengov...
Design an Analysis of Algorithms II-SECS-1021-03
iTop VPN Crack Latest Version Full Key 2025
Wondershare Filmora 15 Crack With Activation Key [2025
How AI/LLM recommend to you ? GDG meetup 16 Aug by Fariman Guliev
wealthsignaloriginal-com-DS-text-... (1).pdf
Weekly report ppt - harsh dattuprasad patel.pptx
Adobe Illustrator 28.6 Crack My Vision of Vector Design
Salesforce Agentforce AI Implementation.pdf
17 Powerful Integrations Your Next-Gen MLM Software Needs
Designing Intelligence for the Shop Floor.pdf
iTop VPN Free 5.6.0.5262 Crack latest version 2025
CCleaner Pro 6.38.11537 Crack Final Latest Version 2025
CapCut Video Editor 6.8.1 Crack for PC Latest Download (Fully Activated) 2025

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
  • 5. Example of the Strategy Pattern in Java
  • 6. Example Implementation 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
  • 16. UML Example 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
  • 25. UML Example 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