SlideShare a Scribd company logo
1 of 34
Software Design Patterns
lesson 11: More Behavioral patterns
Prepared
By
BAZZEKETA DATSUN
MIT(MAK)
Tel: 0705333525
Email: datsunbazzeketa@yahoo.com
By Bazzeketa Datsun 1
• An Observer Pattern says that "just define a one-to-one
dependency so that when one object changes state, all
its dependents are notified and updated automatically".
• The Memento pattern is also known as Dependents or
Publish-Subscribe.
Observer Pattern
1. It describes the coupling between the objects and the
observer.
2. It provides the support for broadcast-type
communication.
Benefits of the Observer Pattern
1. When the change of a state in one object must be
reflected in another object without keeping the
objects tight coupled.
2. When the framework we writes and needs to be
enhanced in future with new observers with minimal
changes.
Usage of the Observer Pattern
Example of the Observer Pattern
Step 1: Create a ResponseHandler1 class the will
implement the java.util.Observer interface.
//This is a class
import java.util.Observable;
import java.util.Observer;
public class ResponseHandler1 implements Observer {
private String resp;
public void update(Observable obj, Object arg) {
if (arg instanceof String) {
resp = (String) arg;
System.out.println("nReceived Response: " + resp );
}
} }
// End of the ResponseHandler1 interface
Code of the Example Observer Pattern
• Step 2: Create a ResponseHandler2 class the will
implement the java.util.Observer interface
//This is a class.
import java.util.Observable;
import java.util.Observer;
public class ResponseHandler2 implements Observer {
private String resp;
public void update(Observable obj, Object arg) {
if (arg instanceof String) {
resp = (String) arg;
System.out.println("nReceived Response: " + resp );
}
} }
// End of the ResponseHandler2 interface.
Code of the Example Observer Pattern cont’d
• Step 3: Create an EventSource class that will extend the
java.util.Observable class
Code of the Example Observer Pattern cont’d
Exercise
• An Interpreter Pattern says that "to define a
representation of grammar of a given language, along
with an interpreter that uses this representation to
interpret sentences in the language".
• Basically the Interpreter pattern has limited area where it
can be applied. We can discuss the Interpreter pattern
only in terms of formal grammars but in this area there
are better solutions that is why it is not frequently used.
• This pattern can applied for parsing the expressions
defined in simple grammars and sometimes in simple
rule engines.
Interpreter Pattern
1. It is easier to change and extend the grammar.
2. Implementing the grammar is straightforward.
Advantages of Interpreter Pattern
• It is used;
• When the grammar of the language is not
complicated.
• When the efficiency is not a priority.
Usage of Interpreter Pattern
Example of Interpreter Pattern
• Step 1: Create a Pattern interface.
public interface Pattern
{
public String conversion(String exp);
}
Code for the Example of Interpreter Pattern
• Step 2: Create a InfixToPostfixPattern class that will allow what kind of pattern you want to
convert.
Code for the Example Interpreter Pattern cont’d
• Step 2: cont’d
Code for the Example Interpreter Pattern cont’d
• Step 2: cont’d
Code for the Example Interpreter Pattern cont’d
• Step 3: Create a InterpreterPatternClient class that will use InfixToPostfix
Conversion.
public class InterpreterPatternClient {
public static void main(String[] args)
{
String infix = "a+b*c";
InfixToPostfixPattern ip=new InfixToPostfixPattern();
String postfix = ip.conversion(infix);
System.out.println("Infix: " + infix);
System.out.println("Postfix: " + postfix);
}
}
Code for the Example Interpreter Pattern cont’d
Exercise
• A State Pattern says that "the class behavior changes
based on its state". In State Pattern, we create objects
which represent various states and a context object
whose behavior varies as its state object changes.
• The State Pattern is also known as Objects for States.
State Pattern
1. It keeps the state-specific behavior.
2. It makes any state transitions explicit.
Benefits of the State Pattern
1. When the behavior of object depends on its state and
it must be able to change its behavior at runtime
according to the new state.
2. It is used when the operations have large, multipart
conditional statements that depend on the state of an
object.
Usage of the State Pattern
Example of the State Pattern
1. Step 1: Create a Connection interface that will provide the
connection to the Controller class.
//This is an interface.
public interface Connection {
public void open();
public void close();
public void log();
public void update();
}
// End of the Connection interface.
Code for the Example of the State Pattern
• Step 2: Create an Accounting class that will implement
to the Connection interface.
Code for the Example of the State Pattern cont’d
• Step 3: Create a Sales class that will implement to the Connection interface.
Code for the Example of the State Pattern cont’d
• Step 4: Create a Sales class that will implement to the Connection interface.
•
Code for the Example of the State Pattern cont’d
• Step 5: Create a Management class that will implement to the Connection interface.
•
Code for the Example of the State Pattern cont’d
• Step 6: Create a Controller class that will use the Connection interface for connecting
with different types of connection.
•
Code for the Example of the State Pattern cont’d
• Step 6: cont’d
•
Code for the Example of the State Pattern cont’d
• Step 6: cont’d
•
Code for the Example of the State Pattern cont’d
• Step 7: Create a StatePatternDemo class.
Code for the Example of the State Pattern cont’d
Exercise
THE END

More Related Content

What's hot

Design Patterns Illustrated
Design Patterns IllustratedDesign Patterns Illustrated
Design Patterns IllustratedHerman Peeren
 
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
 
Oo Design And Patterns
Oo Design And PatternsOo Design And Patterns
Oo Design And PatternsAnil Bapat
 
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
 
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
 
2CPP12 - Method Overriding
2CPP12 - Method Overriding2CPP12 - Method Overriding
2CPP12 - Method OverridingMichael Heron
 
Interfaces In Java
Interfaces In JavaInterfaces In Java
Interfaces In Javaparag
 

What's hot (20)

Design Patterns Illustrated
Design Patterns IllustratedDesign Patterns Illustrated
Design Patterns Illustrated
 
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
 
C# interview quesions
C# interview quesionsC# interview quesions
C# interview quesions
 
Intake 37 2
Intake 37 2Intake 37 2
Intake 37 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)
Std 12 computer chapter 8 classes and object in java (part 2)
 
Java interface
Java interfaceJava interface
Java interface
 
C# interview
C# interviewC# interview
C# interview
 
Chapter 9 Interface
Chapter 9 InterfaceChapter 9 Interface
Chapter 9 Interface
 
Oo Design And Patterns
Oo Design And PatternsOo Design And Patterns
Oo Design And Patterns
 
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
 
Java interfaces
Java interfacesJava interfaces
Java interfaces
 
Interface java
Interface java Interface java
Interface java
 
C#
C#C#
C#
 
Polymorphism presentation in java
Polymorphism presentation in javaPolymorphism presentation in java
Polymorphism presentation in java
 
2CPP12 - Method Overriding
2CPP12 - Method Overriding2CPP12 - Method Overriding
2CPP12 - Method Overriding
 
Interfaces In Java
Interfaces In JavaInterfaces In Java
Interfaces In Java
 
Interfaces
InterfacesInterfaces
Interfaces
 
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
 

Similar to Lesson11 more behavioural patterns

Basics of React Hooks.pptx.pdf
Basics of React Hooks.pptx.pdfBasics of React Hooks.pptx.pdf
Basics of React Hooks.pptx.pdfKnoldus Inc.
 
Password protected diary
Password protected diaryPassword protected diary
Password protected diarySHARDA SHARAN
 
Design Patterns
Design PatternsDesign Patterns
Design Patternssoms_1
 
Software design principles - jinal desai
Software design principles - jinal desaiSoftware design principles - jinal desai
Software design principles - jinal desaijinaldesailive
 
UNIT IV DESIGN PATTERNS.pptx
UNIT IV DESIGN PATTERNS.pptxUNIT IV DESIGN PATTERNS.pptx
UNIT IV DESIGN PATTERNS.pptxanguraju1
 
Understanding React hooks | Walkingtree Technologies
Understanding React hooks | Walkingtree TechnologiesUnderstanding React hooks | Walkingtree Technologies
Understanding React hooks | Walkingtree TechnologiesWalking Tree Technologies
 
What To Leave Implicit
What To Leave ImplicitWhat To Leave Implicit
What To Leave ImplicitMartin Odersky
 
ICALT 2010: Supporting Exception Handling in Scripted Collaborative Courses
ICALT 2010: Supporting Exception Handling in Scripted Collaborative CoursesICALT 2010: Supporting Exception Handling in Scripted Collaborative Courses
ICALT 2010: Supporting Exception Handling in Scripted Collaborative CoursesRoberto Perez-Rodriguez
 
Generative AI Application Development using LangChain and LangFlow
Generative AI Application Development using LangChain and LangFlowGenerative AI Application Development using LangChain and LangFlow
Generative AI Application Development using LangChain and LangFlowGene Leybzon
 
Design Patterns - Distributed Publisher-Subscriber Network
Design Patterns - Distributed Publisher-Subscriber NetworkDesign Patterns - Distributed Publisher-Subscriber Network
Design Patterns - Distributed Publisher-Subscriber NetworkRishabh Karajgi
 
GUI Programming in JAVA (Using Netbeans) - A Review
GUI Programming in JAVA (Using Netbeans) -  A ReviewGUI Programming in JAVA (Using Netbeans) -  A Review
GUI Programming in JAVA (Using Netbeans) - A ReviewFernando Torres
 
C# c# for beginners crash course master c# programming fast and easy today
C# c# for beginners crash course master c# programming fast and easy todayC# c# for beginners crash course master c# programming fast and easy today
C# c# for beginners crash course master c# programming fast and easy todayAfonso Macedo
 
an Introduction to Redux
an Introduction to Reduxan Introduction to Redux
an Introduction to ReduxAmin Ashtiani
 
Refactoring legacy code driven by tests - ENG
Refactoring legacy code driven by tests - ENGRefactoring legacy code driven by tests - ENG
Refactoring legacy code driven by tests - ENGLuca Minudel
 
Design Principles to design Patterns
Design Principles to design PatternsDesign Principles to design Patterns
Design Principles to design PatternsFaizan Haider
 

Similar to Lesson11 more behavioural patterns (20)

Basics of React Hooks.pptx.pdf
Basics of React Hooks.pptx.pdfBasics of React Hooks.pptx.pdf
Basics of React Hooks.pptx.pdf
 
Java unit 7
Java unit 7Java unit 7
Java unit 7
 
Password protected diary
Password protected diaryPassword protected diary
Password protected diary
 
Design Patterns
Design PatternsDesign Patterns
Design Patterns
 
Design patterns
Design patternsDesign patterns
Design patterns
 
Software design principles - jinal desai
Software design principles - jinal desaiSoftware design principles - jinal desai
Software design principles - jinal desai
 
Presentation1
Presentation1Presentation1
Presentation1
 
UNIT IV DESIGN PATTERNS.pptx
UNIT IV DESIGN PATTERNS.pptxUNIT IV DESIGN PATTERNS.pptx
UNIT IV DESIGN PATTERNS.pptx
 
Understanding React hooks | Walkingtree Technologies
Understanding React hooks | Walkingtree TechnologiesUnderstanding React hooks | Walkingtree Technologies
Understanding React hooks | Walkingtree Technologies
 
What To Leave Implicit
What To Leave ImplicitWhat To Leave Implicit
What To Leave Implicit
 
ICALT 2010: Supporting Exception Handling in Scripted Collaborative Courses
ICALT 2010: Supporting Exception Handling in Scripted Collaborative CoursesICALT 2010: Supporting Exception Handling in Scripted Collaborative Courses
ICALT 2010: Supporting Exception Handling in Scripted Collaborative Courses
 
Soild principles
Soild principlesSoild principles
Soild principles
 
Generative AI Application Development using LangChain and LangFlow
Generative AI Application Development using LangChain and LangFlowGenerative AI Application Development using LangChain and LangFlow
Generative AI Application Development using LangChain and LangFlow
 
Design Patterns - Distributed Publisher-Subscriber Network
Design Patterns - Distributed Publisher-Subscriber NetworkDesign Patterns - Distributed Publisher-Subscriber Network
Design Patterns - Distributed Publisher-Subscriber Network
 
GUI Programming in JAVA (Using Netbeans) - A Review
GUI Programming in JAVA (Using Netbeans) -  A ReviewGUI Programming in JAVA (Using Netbeans) -  A Review
GUI Programming in JAVA (Using Netbeans) - A Review
 
C# c# for beginners crash course master c# programming fast and easy today
C# c# for beginners crash course master c# programming fast and easy todayC# c# for beginners crash course master c# programming fast and easy today
C# c# for beginners crash course master c# programming fast and easy today
 
an Introduction to Redux
an Introduction to Reduxan Introduction to Redux
an Introduction to Redux
 
Asp.net main
Asp.net mainAsp.net main
Asp.net main
 
Refactoring legacy code driven by tests - ENG
Refactoring legacy code driven by tests - ENGRefactoring legacy code driven by tests - ENG
Refactoring legacy code driven by tests - ENG
 
Design Principles to design Patterns
Design Principles to design PatternsDesign Principles to design Patterns
Design Principles to design Patterns
 

Recently uploaded

React Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaReact Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaHanief Utama
 
Folding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesFolding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesPhilip Schwarz
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEOrtus Solutions, Corp
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Andreas Granig
 
The Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdfThe Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdfPower Karaoke
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideChristina Lin
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...OnePlan Solutions
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software DevelopersVinodh Ram
 
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
 
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
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)OPEN KNOWLEDGE GmbH
 
Unveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsUnveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsAhmed Mohamed
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...stazi3110
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityNeo4j
 
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataAdobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataBradBedford3
 
software engineering Chapter 5 System modeling.pptx
software engineering Chapter 5 System modeling.pptxsoftware engineering Chapter 5 System modeling.pptx
software engineering Chapter 5 System modeling.pptxnada99848
 
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...Christina Lin
 
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024StefanoLambiase
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...MyIntelliSource, Inc.
 

Recently uploaded (20)

React Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaReact Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief Utama
 
Folding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesFolding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a series
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024
 
The Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdfThe Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdf
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software Developers
 
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
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEE
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)
 
Unveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsUnveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML Diagrams
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered Sustainability
 
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataAdobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
 
software engineering Chapter 5 System modeling.pptx
software engineering Chapter 5 System modeling.pptxsoftware engineering Chapter 5 System modeling.pptx
software engineering Chapter 5 System modeling.pptx
 
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
 
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
 
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
 

Lesson11 more behavioural patterns

  • 1. Software Design Patterns lesson 11: More Behavioral patterns Prepared By BAZZEKETA DATSUN MIT(MAK) Tel: 0705333525 Email: datsunbazzeketa@yahoo.com By Bazzeketa Datsun 1
  • 2. • An Observer Pattern says that "just define a one-to-one dependency so that when one object changes state, all its dependents are notified and updated automatically". • The Memento pattern is also known as Dependents or Publish-Subscribe. Observer Pattern
  • 3. 1. It describes the coupling between the objects and the observer. 2. It provides the support for broadcast-type communication. Benefits of the Observer Pattern
  • 4. 1. When the change of a state in one object must be reflected in another object without keeping the objects tight coupled. 2. When the framework we writes and needs to be enhanced in future with new observers with minimal changes. Usage of the Observer Pattern
  • 5. Example of the Observer Pattern
  • 6. Step 1: Create a ResponseHandler1 class the will implement the java.util.Observer interface. //This is a class import java.util.Observable; import java.util.Observer; public class ResponseHandler1 implements Observer { private String resp; public void update(Observable obj, Object arg) { if (arg instanceof String) { resp = (String) arg; System.out.println("nReceived Response: " + resp ); } } } // End of the ResponseHandler1 interface Code of the Example Observer Pattern
  • 7. • Step 2: Create a ResponseHandler2 class the will implement the java.util.Observer interface //This is a class. import java.util.Observable; import java.util.Observer; public class ResponseHandler2 implements Observer { private String resp; public void update(Observable obj, Object arg) { if (arg instanceof String) { resp = (String) arg; System.out.println("nReceived Response: " + resp ); } } } // End of the ResponseHandler2 interface. Code of the Example Observer Pattern cont’d
  • 8. • Step 3: Create an EventSource class that will extend the java.util.Observable class Code of the Example Observer Pattern cont’d
  • 10. • An Interpreter Pattern says that "to define a representation of grammar of a given language, along with an interpreter that uses this representation to interpret sentences in the language". • Basically the Interpreter pattern has limited area where it can be applied. We can discuss the Interpreter pattern only in terms of formal grammars but in this area there are better solutions that is why it is not frequently used. • This pattern can applied for parsing the expressions defined in simple grammars and sometimes in simple rule engines. Interpreter Pattern
  • 11. 1. It is easier to change and extend the grammar. 2. Implementing the grammar is straightforward. Advantages of Interpreter Pattern
  • 12. • It is used; • When the grammar of the language is not complicated. • When the efficiency is not a priority. Usage of Interpreter Pattern
  • 14. • Step 1: Create a Pattern interface. public interface Pattern { public String conversion(String exp); } Code for the Example of Interpreter Pattern
  • 15. • Step 2: Create a InfixToPostfixPattern class that will allow what kind of pattern you want to convert. Code for the Example Interpreter Pattern cont’d
  • 16. • Step 2: cont’d Code for the Example Interpreter Pattern cont’d
  • 17. • Step 2: cont’d Code for the Example Interpreter Pattern cont’d
  • 18. • Step 3: Create a InterpreterPatternClient class that will use InfixToPostfix Conversion. public class InterpreterPatternClient { public static void main(String[] args) { String infix = "a+b*c"; InfixToPostfixPattern ip=new InfixToPostfixPattern(); String postfix = ip.conversion(infix); System.out.println("Infix: " + infix); System.out.println("Postfix: " + postfix); } } Code for the Example Interpreter Pattern cont’d
  • 20. • A State Pattern says that "the class behavior changes based on its state". In State Pattern, we create objects which represent various states and a context object whose behavior varies as its state object changes. • The State Pattern is also known as Objects for States. State Pattern
  • 21. 1. It keeps the state-specific behavior. 2. It makes any state transitions explicit. Benefits of the State Pattern
  • 22. 1. When the behavior of object depends on its state and it must be able to change its behavior at runtime according to the new state. 2. It is used when the operations have large, multipart conditional statements that depend on the state of an object. Usage of the State Pattern
  • 23. Example of the State Pattern
  • 24. 1. Step 1: Create a Connection interface that will provide the connection to the Controller class. //This is an interface. public interface Connection { public void open(); public void close(); public void log(); public void update(); } // End of the Connection interface. Code for the Example of the State Pattern
  • 25. • Step 2: Create an Accounting class that will implement to the Connection interface. Code for the Example of the State Pattern cont’d
  • 26. • Step 3: Create a Sales class that will implement to the Connection interface. Code for the Example of the State Pattern cont’d
  • 27. • Step 4: Create a Sales class that will implement to the Connection interface. • Code for the Example of the State Pattern cont’d
  • 28. • Step 5: Create a Management class that will implement to the Connection interface. • Code for the Example of the State Pattern cont’d
  • 29. • Step 6: Create a Controller class that will use the Connection interface for connecting with different types of connection. • Code for the Example of the State Pattern cont’d
  • 30. • Step 6: cont’d • Code for the Example of the State Pattern cont’d
  • 31. • Step 6: cont’d • Code for the Example of the State Pattern cont’d
  • 32. • Step 7: Create a StatePatternDemo class. Code for the Example of the State Pattern cont’d