SlideShare a Scribd company logo
1 of 32
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

What's hot

PATTERNS03 - Behavioural Design Patterns
PATTERNS03 - Behavioural Design PatternsPATTERNS03 - Behavioural Design Patterns
PATTERNS03 - Behavioural Design PatternsMichael Heron
 
Strategy Design Pattern
Strategy Design PatternStrategy Design Pattern
Strategy Design PatternGanesh Kolhe
 
Top 20 c# interview Question and answers
Top 20 c# interview Question and answersTop 20 c# interview Question and answers
Top 20 c# interview Question and answersw3asp dotnet
 
Std 12 computer chapter 8 classes and object in java (part 2)
Std 12 computer chapter 8 classes and object in java (part 2)Std 12 computer chapter 8 classes and object in java (part 2)
Std 12 computer chapter 8 classes and object in java (part 2)Nuzhat Memon
 
Chapter 9 Interface
Chapter 9 InterfaceChapter 9 Interface
Chapter 9 InterfaceOUM SAOKOSAL
 
Interface java
Interface java Interface java
Interface java atiafyrose
 
Polymorphism presentation in java
Polymorphism presentation in javaPolymorphism presentation in java
Polymorphism presentation in javaAhsan Raja
 
Interfaces In Java
Interfaces In JavaInterfaces In Java
Interfaces In Javaparag
 
Jump start to OOP, OOAD, and Design Pattern
Jump start to OOP, OOAD, and Design PatternJump start to OOP, OOAD, and Design Pattern
Jump start to OOP, OOAD, and Design PatternNishith Shukla
 
Oo Design And Patterns
Oo Design And PatternsOo Design And Patterns
Oo Design And PatternsAnil Bapat
 
java interface and packages
java interface and packagesjava interface and packages
java interface and packagesVINOTH R
 

What's hot (20)

PATTERNS03 - Behavioural Design Patterns
PATTERNS03 - Behavioural Design PatternsPATTERNS03 - Behavioural Design Patterns
PATTERNS03 - Behavioural Design Patterns
 
Strategy Design Pattern
Strategy Design PatternStrategy Design Pattern
Strategy Design Pattern
 
Top 20 c# interview Question and answers
Top 20 c# interview Question and answersTop 20 c# interview Question and answers
Top 20 c# interview Question and answers
 
Std 12 computer chapter 8 classes and object in java (part 2)
Std 12 computer chapter 8 classes and object in java (part 2)Std 12 computer chapter 8 classes and object in java (part 2)
Std 12 computer chapter 8 classes and object in java (part 2)
 
Intake 37 2
Intake 37 2Intake 37 2
Intake 37 2
 
Java interface
Java interfaceJava interface
Java interface
 
C# interview quesions
C# interview quesionsC# interview quesions
C# interview quesions
 
Java interfaces
Java interfacesJava interfaces
Java interfaces
 
Chapter 9 Interface
Chapter 9 InterfaceChapter 9 Interface
Chapter 9 Interface
 
Interface java
Interface java Interface java
Interface java
 
Polymorphism presentation in java
Polymorphism presentation in javaPolymorphism presentation in java
Polymorphism presentation in java
 
C# interview
C# interviewC# interview
C# interview
 
Interfaces
InterfacesInterfaces
Interfaces
 
Interfaces In Java
Interfaces In JavaInterfaces In Java
Interfaces In Java
 
Jump start to OOP, OOAD, and Design Pattern
Jump start to OOP, OOAD, and Design PatternJump start to OOP, OOAD, and Design Pattern
Jump start to OOP, OOAD, and Design Pattern
 
Oo Design And Patterns
Oo Design And PatternsOo Design And Patterns
Oo Design And Patterns
 
C#
C#C#
C#
 
Polymorphism in java
Polymorphism in javaPolymorphism in java
Polymorphism in java
 
Method overloading and constructor overloading in java
Method overloading and constructor overloading in javaMethod overloading and constructor overloading in java
Method overloading and constructor overloading in java
 
java interface and packages
java interface and packagesjava interface and packages
java interface and packages
 

Similar to Lesson12 other behavioural patterns

P Training Presentation
P Training PresentationP Training Presentation
P Training PresentationGaurav Tyagi
 
Logic and Coding of Java Interfaces & Swing Applications
Logic and Coding of Java Interfaces & Swing ApplicationsLogic and Coding of Java Interfaces & Swing Applications
Logic and Coding of Java Interfaces & Swing Applicationskjkleindorfer
 
Concurrency Programming in Java - 02 - Essentials of Java Part 1
Concurrency Programming in Java - 02 - Essentials of Java Part 1Concurrency Programming in Java - 02 - Essentials of Java Part 1
Concurrency Programming in Java - 02 - Essentials of Java Part 1Sachintha Gunasena
 
Interface
InterfaceInterface
Interfacevvpadhu
 
Understanding And Using Reflection
Understanding And Using ReflectionUnderstanding And Using Reflection
Understanding And Using ReflectionGanesh Samarthyam
 
Introduction to design_patterns
Introduction to design_patternsIntroduction to design_patterns
Introduction to design_patternsamitarcade
 
Design Patterns - Part 1 of 2
Design Patterns - Part 1 of 2Design Patterns - Part 1 of 2
Design Patterns - Part 1 of 2Savio Sebastian
 
Design Pattern Mastery - Momentum Dev Con 19 Apr 2018
Design Pattern Mastery - Momentum Dev Con 19 Apr 2018Design Pattern Mastery - Momentum Dev Con 19 Apr 2018
Design Pattern Mastery - Momentum Dev Con 19 Apr 2018Steven Smith
 
Introduction to c first week slides
Introduction to c first week slidesIntroduction to c first week slides
Introduction to c first week slidesluqman bawany
 
Java For beginners and CSIT and IT students
Java  For beginners and CSIT and IT studentsJava  For beginners and CSIT and IT students
Java For beginners and CSIT and IT studentsPartnered Health
 
Pi j3.2 polymorphism
Pi j3.2 polymorphismPi j3.2 polymorphism
Pi j3.2 polymorphismmcollison
 
Design patterns in Java - Monitis 2017
Design patterns in Java - Monitis 2017Design patterns in Java - Monitis 2017
Design patterns in Java - Monitis 2017Arsen Gasparyan
 
INTRODUCTION TO OBJECT ORIENTED PROGRAMMING.pptx
INTRODUCTION TO OBJECT ORIENTED PROGRAMMING.pptxINTRODUCTION TO OBJECT ORIENTED PROGRAMMING.pptx
INTRODUCTION TO OBJECT ORIENTED PROGRAMMING.pptxDeepasCSE
 
TDD And Refactoring
TDD And RefactoringTDD And Refactoring
TDD And RefactoringNaresh Jain
 

Similar to Lesson12 other behavioural patterns (20)

L09 Frameworks
L09 FrameworksL09 Frameworks
L09 Frameworks
 
P Training Presentation
P Training PresentationP Training Presentation
P Training Presentation
 
Logic and Coding of Java Interfaces & Swing Applications
Logic and Coding of Java Interfaces & Swing ApplicationsLogic and Coding of Java Interfaces & Swing Applications
Logic and Coding of Java Interfaces & Swing Applications
 
Concurrency Programming in Java - 02 - Essentials of Java Part 1
Concurrency Programming in Java - 02 - Essentials of Java Part 1Concurrency Programming in Java - 02 - Essentials of Java Part 1
Concurrency Programming in Java - 02 - Essentials of Java Part 1
 
Design Patterns
Design PatternsDesign Patterns
Design Patterns
 
Design Patterns
Design PatternsDesign Patterns
Design Patterns
 
Template Method Design Pattern
Template Method Design PatternTemplate Method Design Pattern
Template Method Design Pattern
 
Interface
InterfaceInterface
Interface
 
Design patterns
Design patternsDesign patterns
Design patterns
 
Understanding And Using Reflection
Understanding And Using ReflectionUnderstanding And Using Reflection
Understanding And Using Reflection
 
Introduction to design_patterns
Introduction to design_patternsIntroduction to design_patterns
Introduction to design_patterns
 
Design Patterns - Part 1 of 2
Design Patterns - Part 1 of 2Design Patterns - Part 1 of 2
Design Patterns - Part 1 of 2
 
Design Pattern Mastery - Momentum Dev Con 19 Apr 2018
Design Pattern Mastery - Momentum Dev Con 19 Apr 2018Design Pattern Mastery - Momentum Dev Con 19 Apr 2018
Design Pattern Mastery - Momentum Dev Con 19 Apr 2018
 
Introduction to c first week slides
Introduction to c first week slidesIntroduction to c first week slides
Introduction to c first week slides
 
Java For beginners and CSIT and IT students
Java  For beginners and CSIT and IT studentsJava  For beginners and CSIT and IT students
Java For beginners and CSIT and IT students
 
Pi j3.2 polymorphism
Pi j3.2 polymorphismPi j3.2 polymorphism
Pi j3.2 polymorphism
 
Design patterns in Java - Monitis 2017
Design patterns in Java - Monitis 2017Design patterns in Java - Monitis 2017
Design patterns in Java - Monitis 2017
 
INTRODUCTION TO OBJECT ORIENTED PROGRAMMING.pptx
INTRODUCTION TO OBJECT ORIENTED PROGRAMMING.pptxINTRODUCTION TO OBJECT ORIENTED PROGRAMMING.pptx
INTRODUCTION TO OBJECT ORIENTED PROGRAMMING.pptx
 
L05 Frameworks
L05 FrameworksL05 Frameworks
L05 Frameworks
 
TDD And Refactoring
TDD And RefactoringTDD And Refactoring
TDD And Refactoring
 

Recently uploaded

What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...Technogeeks
 
Intelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmIntelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmSujith Sukumaran
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEEVICTOR MAESTRE RAMIREZ
 
What is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWhat is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWave PLM
 
Ahmed Motair CV April 2024 (Senior SW Developer)
Ahmed Motair CV April 2024 (Senior SW Developer)Ahmed Motair CV April 2024 (Senior SW Developer)
Ahmed Motair CV April 2024 (Senior SW Developer)Ahmed Mater
 
Odoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 EnterpriseOdoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 Enterprisepreethippts
 
How to submit a standout Adobe Champion Application
How to submit a standout Adobe Champion ApplicationHow to submit a standout Adobe Champion Application
How to submit a standout Adobe Champion ApplicationBradBedford3
 
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...Matt Ray
 
Best Web Development Agency- Idiosys USA.pdf
Best Web Development Agency- Idiosys USA.pdfBest Web Development Agency- Idiosys USA.pdf
Best Web Development Agency- Idiosys USA.pdfIdiosysTechnologies1
 
SpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at RuntimeSpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at Runtimeandrehoraa
 
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte GermanySuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte GermanyChristoph Pohl
 
英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作qr0udbr0
 
Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Velvetech LLC
 
Buds n Tech IT Solutions: Top-Notch Web Services in Noida
Buds n Tech IT Solutions: Top-Notch Web Services in NoidaBuds n Tech IT Solutions: Top-Notch Web Services in Noida
Buds n Tech IT Solutions: Top-Notch Web Services in Noidabntitsolutionsrishis
 
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)jennyeacort
 
Implementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureImplementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureDinusha Kumarasiri
 
PREDICTING RIVER WATER QUALITY ppt presentation
PREDICTING  RIVER  WATER QUALITY  ppt presentationPREDICTING  RIVER  WATER QUALITY  ppt presentation
PREDICTING RIVER WATER QUALITY ppt presentationvaddepallysandeep122
 
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...OnePlan Solutions
 

Recently uploaded (20)

Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort ServiceHot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
 
What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...
 
Intelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmIntelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalm
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEE
 
What is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWhat is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need It
 
Ahmed Motair CV April 2024 (Senior SW Developer)
Ahmed Motair CV April 2024 (Senior SW Developer)Ahmed Motair CV April 2024 (Senior SW Developer)
Ahmed Motair CV April 2024 (Senior SW Developer)
 
Odoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 EnterpriseOdoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 Enterprise
 
How to submit a standout Adobe Champion Application
How to submit a standout Adobe Champion ApplicationHow to submit a standout Adobe Champion Application
How to submit a standout Adobe Champion Application
 
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
 
Best Web Development Agency- Idiosys USA.pdf
Best Web Development Agency- Idiosys USA.pdfBest Web Development Agency- Idiosys USA.pdf
Best Web Development Agency- Idiosys USA.pdf
 
SpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at RuntimeSpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at Runtime
 
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte GermanySuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
 
英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作
 
Advantages of Odoo ERP 17 for Your Business
Advantages of Odoo ERP 17 for Your BusinessAdvantages of Odoo ERP 17 for Your Business
Advantages of Odoo ERP 17 for Your Business
 
Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...
 
Buds n Tech IT Solutions: Top-Notch Web Services in Noida
Buds n Tech IT Solutions: Top-Notch Web Services in NoidaBuds n Tech IT Solutions: Top-Notch Web Services in Noida
Buds n Tech IT Solutions: Top-Notch Web Services in Noida
 
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
 
Implementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureImplementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with Azure
 
PREDICTING RIVER WATER QUALITY ppt presentation
PREDICTING  RIVER  WATER QUALITY  ppt presentationPREDICTING  RIVER  WATER QUALITY  ppt presentation
PREDICTING RIVER WATER QUALITY ppt presentation
 
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
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
  • 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