SlideShare a Scribd company logo
1 of 39
Prepared
By
BAZZEKETA DATSUN
MIT(MAK)
Tel: 0705333525
Email: datsunbazzeketa@yahoo.com
By Bazzeketa Datsun 1
 Behavioral design patterns are concerned with
the interaction and assigning of responsibility
between different objects.
 In these design patterns,the interaction
between the objects should be in such a way
that they can easily talk to each other and
still should be loosely coupled.
 That means the implementation and the
client should be loosely coupled in order to
avoid hard coding and dependencies.
 A Command Pattern says that "encapsulate a
request under an object as a command and
pass it to invoker object. Invoker object looks
for the appropriate object which can handle
this command and pass the command to the
corresponding object and that object
executes the command".
 It is also known as Action or Transaction
• It separates the object that invokes the
operation from the object that actually
performs the operation.
• It makes easy to add new commands,
because existing classes remain unchanged.
• It is used:
• When you need parameterize objects
according to an action perform.
• When you need to create and execute
requests at different times.
• When you need to support rollback, logging
or transaction functionality.
• Command This is an interface for executing
an operation.
• Concrete Command This class extends the
Command interface, implements and
executes the method. This class creates a
binding between the action and the receiver.
• Client This class creates the Concrete
Command class and associates it with the
receiver.
• Invoker This class asks the command to carry
out the request.
• Receiver This class knows to perform the
operation.
 Step 1: Create a ActionListernerCommand interface
that will act as a Command.
public interface ActionListenerCommand {
public void execute();
}
 Step 2: Create a Document class that will act as a
Receiver.
public class Document {
public void open(){
System.out.println("Document Opened");
}
public void save(){
System.out.println("Document Saved");
} }
 Step 3: Create a ActionOpen class that will act
as an ConcreteCommand.
public class ActionOpen implements ActionListenerCommand {
private Document doc;
public ActionOpen(Document doc) {
this.doc = doc;
}
@Override
public void execute() {
doc.open();
} }
 Step 4: Create a ActionSave class that will act
as an ConcreteCommand.
public class ActionSave implements ActionListenerCommand {
private Document doc;
public ActionSave(Document doc) {
this.doc = doc;
}
@Override
public void execute() {
doc.save();
} }
 Step 5: Create a MenuOptions class that will
act as an Invoker.
public class ActionSave implements ActionListenerCommand {
private Document doc;
public ActionSave(Document doc) {
this.doc = doc;
}
@Override
public void execute() {
doc.save();
} }
 Step 6: Create a CommanPatternClient class
that will act as a Client.
public class CommandPatternClient
{
public static void main(String[] args)
{
Document doc = new Document();
ActionListenerCommand clickOpen = new ActionOpen(doc);
ActionListenerCommand clickSave = new ActionSave(doc);
MenuOptions menu = new MenuOptions(clickOpen, clickSave);
menu.clickOpen();
menu.clickSave();
} }
 A Mediator Pattern says that "to define an
object that encapsulates how a set of objects
interact".
 I will explain the Mediator pattern by
considering a problem. When we begin with
development, we have a few classes and these
classes interact with each other producing
results. Now, consider slowly, the logic becomes
more complex when functionality increases.
Then what happens? We add more classes and
they still interact with each other but it gets
really difficult to maintain this code now. So,
Mediator pattern takes care of this problem.
 Mediator pattern is used to reduce
communication complexity between multiple
objects or classes. This pattern provides a
mediator class which normally handles all the
communications between different classes
and supports easy maintainability of the code
by loose coupling.
1. It decouples the number of classes.
2. It simplifies object protocols.
3. It centralizes the control.
4. The individual components become
simpler and much easier to deal with
because they don't need to pass messages
to one another.
5. The components don't need to contain
logic to deal with their intercommunication
and therefore, they are more generic.
• It is commonly used in message-based
systems likewise chat applications.
• When the set of objects communicate in
complex but in well-defined ways.
• ApnaChatroom :- defines the interface for
interacting with participants.
• ApnaChatroomImpl :- implements the operations
defined by the Chatroom interface. The
operations are managing the interactions between
the objects: when one participant sends a
message, the message is sent to the other
participants.
• Participant :- defines an interface for the users
involved in chatting.
• User1, User2, ...UserN :- implements Participant
interface; the participant can be a number of
users involved in chatting. But each Participant
will keep only a reference to the ApnaChatRoom.
 Step 1: Create a ApnaChatRoom interface.
//This is an interface.
public interface ApnaChatRoom {
public void showMsg(String msg, Participant p);
}
// End of the ApnaChatRoom interface
 Step 2: Create a ApnaChatRoomIml class that will implement
ApnaChatRoom interface and will also use the number of
participants involved in chatting through Participant interface.
 Step 3:Create a Participant abstract class.
//This is an abstract class.
public abstract class Participant {
public abstract void sendMsg(String msg);
public abstract void setname(String name);
public abstract String getName();
}
// End of the Participant abstract class.
 Step 4: Create a User1 class that will extend Participant
abstract class and will use the ApnaChatRoom interface.
 Step 5: Create a User2 class that will extend Participant
abstract class and will use the ApnaChatRoom interface.
 Step 6: Create a MediatorPatternDemo class that
will use participants involved in chatting.
 A Memento Pattern defines a way "to restore
the state of an object to its previous state".
But it must do this without violating
Encapsulation. Such case is useful in case of
error or failure.
 The Memento pattern is also known as Token.
 Undo or backspace or ctrl+z is one of the
most used operation in an editor. Memento
design pattern is used to implement the undo
operation. This is done by saving the current
state of the object as it changes state.
1. It preserves encapsulation boundaries.
2. It simplifies the originator.
1. It is used in Undo and Redo operations in
most software.
2. It is also used in database transactions to
Undo or Redo transactions.
 Memento:
• Stores internal state of the originator object. The state can
include any number of state variables.
• The Memento must have two interfaces, an interface to the
caretaker. This interface must not allow any operations or any
access to internal state stored by the memento and thus
maintains the encapsulation. The other interface is Originator and
it allows the Originator to access any state variables necessary to
the originator to restore the previous state.
 Originator:
• Creates a memento object that will capture the internal state of
Originator.
• Use the memento object to restore its previous state.
 Caretaker:
• Responsible for keeping the memento.
• The memento is transparent to the caretaker, and the caretaker
must not operate on it.
 Step 1: Create an Originator class that will use Memento object to
restore its previous state.
 Step 2: Create a Memento class that will Store internal
state of the Originator object.
//This is a class.
public class Memento
{
private String state;
public Memento(String state) {
this.state=state;
}
public String getState() {
return state;
}
}// End of the Memento class.
 Step 3: Create a Caretaker class that will responsible
for keeping the Memento.
//This is a class
import java.util.ArrayList;
import java.util.List;
public class Caretaker {
private List<Memento> mementoList = new ArrayList<Memento>();
public void add(Memento state) {
mementoList.add(state);
}
public Memento get(int index){
return mementoList.get(index);
} }
// End of the Caretaker class.
 Step 4: Create a MementoPatternDemo class.
Lesson10 behavioral patterns
Lesson10 behavioral patterns

More Related Content

What's hot

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
 
PATTERNS03 - Behavioural Design Patterns
PATTERNS03 - Behavioural Design PatternsPATTERNS03 - Behavioural Design Patterns
PATTERNS03 - Behavioural Design PatternsMichael Heron
 
Design Patterns Illustrated
Design Patterns IllustratedDesign Patterns Illustrated
Design Patterns IllustratedHerman Peeren
 
Strategy Design Pattern
Strategy Design PatternStrategy Design Pattern
Strategy Design PatternGanesh Kolhe
 
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
 
Interface java
Interface java Interface java
Interface java atiafyrose
 
Chapter 9 Interface
Chapter 9 InterfaceChapter 9 Interface
Chapter 9 InterfaceOUM SAOKOSAL
 
Polymorphism presentation in java
Polymorphism presentation in javaPolymorphism presentation in java
Polymorphism presentation in javaAhsan Raja
 
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
 
Interfaces In Java
Interfaces In JavaInterfaces In Java
Interfaces In Javaparag
 
Nairobi JVM meetup : Introduction to akka
Nairobi JVM meetup : Introduction to akkaNairobi JVM meetup : Introduction to akka
Nairobi JVM meetup : Introduction to akkaAD_
 

What's hot (20)

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
 
PATTERNS03 - Behavioural Design Patterns
PATTERNS03 - Behavioural Design PatternsPATTERNS03 - Behavioural Design Patterns
PATTERNS03 - Behavioural Design Patterns
 
Design Patterns Illustrated
Design Patterns IllustratedDesign Patterns Illustrated
Design Patterns Illustrated
 
Strategy Design Pattern
Strategy Design PatternStrategy Design Pattern
Strategy Design Pattern
 
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)
 
C# interview quesions
C# interview quesionsC# interview quesions
C# interview quesions
 
Java interface
Java interfaceJava interface
Java interface
 
Java interfaces
Java interfacesJava interfaces
Java interfaces
 
Interface java
Interface java Interface java
Interface java
 
Chapter 9 Interface
Chapter 9 InterfaceChapter 9 Interface
Chapter 9 Interface
 
C# interview
C# interviewC# interview
C# interview
 
Intake 37 1
Intake 37 1Intake 37 1
Intake 37 1
 
Polymorphism presentation in java
Polymorphism presentation in javaPolymorphism presentation in java
Polymorphism presentation in java
 
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
 
C#
C#C#
C#
 
Interfaces
InterfacesInterfaces
Interfaces
 
Interfaces In Java
Interfaces In JavaInterfaces In Java
Interfaces In Java
 
Nairobi JVM meetup : Introduction to akka
Nairobi JVM meetup : Introduction to akkaNairobi JVM meetup : Introduction to akka
Nairobi JVM meetup : Introduction to akka
 

Similar to Lesson10 behavioral 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.
 
Md02 - Getting Started part-2
Md02 - Getting Started part-2Md02 - Getting Started part-2
Md02 - Getting Started part-2Rakesh Madugula
 
Multithreading
MultithreadingMultithreading
Multithreadingbackdoor
 
Repository Pattern in MVC3 Application with Entity Framework
Repository Pattern in MVC3 Application with Entity FrameworkRepository Pattern in MVC3 Application with Entity Framework
Repository Pattern in MVC3 Application with Entity FrameworkAkhil Mittal
 
Java Advance Concepts
Java Advance ConceptsJava Advance Concepts
Java Advance ConceptsEmprovise
 
common design patterns summary.pdf
common design patterns summary.pdfcommon design patterns summary.pdf
common design patterns summary.pdfNikolayRaychev2
 
Design Pattern Notes: Nagpur University
Design Pattern Notes: Nagpur UniversityDesign Pattern Notes: Nagpur University
Design Pattern Notes: Nagpur UniversityShubham Narkhede
 
C# Advanced L07-Design Patterns
C# Advanced L07-Design PatternsC# Advanced L07-Design Patterns
C# Advanced L07-Design PatternsMohammad Shaker
 
Command pattern in java
Command pattern in javaCommand pattern in java
Command pattern in javaRakibAhmed0
 
Why using finalizers is a bad idea
Why using finalizers is a bad ideaWhy using finalizers is a bad idea
Why using finalizers is a bad ideaPVS-Studio
 
Nt1310 Unit 3 Language Analysis
Nt1310 Unit 3 Language AnalysisNt1310 Unit 3 Language Analysis
Nt1310 Unit 3 Language AnalysisNicole Gomez
 

Similar to Lesson10 behavioral patterns (20)

Java unit 7
Java unit 7Java unit 7
Java unit 7
 
Design_Patterns_Dr.CM.ppt
Design_Patterns_Dr.CM.pptDesign_Patterns_Dr.CM.ppt
Design_Patterns_Dr.CM.ppt
 
Basics of React Hooks.pptx.pdf
Basics of React Hooks.pptx.pdfBasics of React Hooks.pptx.pdf
Basics of React Hooks.pptx.pdf
 
Tech talk
Tech talkTech talk
Tech talk
 
Software Design Patterns
Software Design PatternsSoftware Design Patterns
Software Design Patterns
 
Software Design Patterns
Software Design PatternsSoftware Design Patterns
Software Design Patterns
 
Md02 - Getting Started part-2
Md02 - Getting Started part-2Md02 - Getting Started part-2
Md02 - Getting Started part-2
 
Multithreading
MultithreadingMultithreading
Multithreading
 
Servlet session 9
Servlet   session 9Servlet   session 9
Servlet session 9
 
Repository Pattern in MVC3 Application with Entity Framework
Repository Pattern in MVC3 Application with Entity FrameworkRepository Pattern in MVC3 Application with Entity Framework
Repository Pattern in MVC3 Application with Entity Framework
 
Oops
OopsOops
Oops
 
Java Advance Concepts
Java Advance ConceptsJava Advance Concepts
Java Advance Concepts
 
common design patterns summary.pdf
common design patterns summary.pdfcommon design patterns summary.pdf
common design patterns summary.pdf
 
Clojure 7-Languages
Clojure 7-LanguagesClojure 7-Languages
Clojure 7-Languages
 
Design Pattern Notes: Nagpur University
Design Pattern Notes: Nagpur UniversityDesign Pattern Notes: Nagpur University
Design Pattern Notes: Nagpur University
 
C# Advanced L07-Design Patterns
C# Advanced L07-Design PatternsC# Advanced L07-Design Patterns
C# Advanced L07-Design Patterns
 
Command pattern in java
Command pattern in javaCommand pattern in java
Command pattern in java
 
Why using finalizers is a bad idea
Why using finalizers is a bad ideaWhy using finalizers is a bad idea
Why using finalizers is a bad idea
 
Dot NET Remoting
Dot NET RemotingDot NET Remoting
Dot NET Remoting
 
Nt1310 Unit 3 Language Analysis
Nt1310 Unit 3 Language AnalysisNt1310 Unit 3 Language Analysis
Nt1310 Unit 3 Language Analysis
 

Recently uploaded

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
 
What are the features of Vehicle Tracking System?
What are the features of Vehicle Tracking System?What are the features of Vehicle Tracking System?
What are the features of Vehicle Tracking System?Watsoo Telematics
 
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
 
chapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptchapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptkotipi9215
 
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
 
buds n tech IT solutions
buds n  tech IT                solutionsbuds n  tech IT                solutions
buds n tech IT solutionsmonugehlot87
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxTier1 app
 
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
 
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
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackVICTOR MAESTRE RAMIREZ
 
What is Binary Language? Computer Number Systems
What is Binary Language?  Computer Number SystemsWhat is Binary Language?  Computer Number Systems
What is Binary Language? Computer Number SystemsJheuzeDellosa
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software DevelopersVinodh Ram
 
XpertSolvers: Your Partner in Building Innovative Software Solutions
XpertSolvers: Your Partner in Building Innovative Software SolutionsXpertSolvers: Your Partner in Building Innovative Software Solutions
XpertSolvers: Your Partner in Building Innovative Software SolutionsMehedi Hasan Shohan
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...MyIntelliSource, Inc.
 
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...gurkirankumar98700
 
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样umasea
 
Intelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmIntelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmSujith Sukumaran
 
Asset Management Software - Infographic
Asset Management Software - InfographicAsset Management Software - Infographic
Asset Management Software - InfographicHr365.us smith
 
why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfjoe51371421
 
Engage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The UglyEngage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The UglyFrank van der Linden
 

Recently uploaded (20)

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
 
What are the features of Vehicle Tracking System?
What are the features of Vehicle Tracking System?What are the features of Vehicle Tracking System?
What are the features of Vehicle Tracking System?
 
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
 
chapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptchapter--4-software-project-planning.ppt
chapter--4-software-project-planning.ppt
 
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)
 
buds n tech IT solutions
buds n  tech IT                solutionsbuds n  tech IT                solutions
buds n tech IT solutions
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
 
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
 
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
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStack
 
What is Binary Language? Computer Number Systems
What is Binary Language?  Computer Number SystemsWhat is Binary Language?  Computer Number Systems
What is Binary Language? Computer Number Systems
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software Developers
 
XpertSolvers: Your Partner in Building Innovative Software Solutions
XpertSolvers: Your Partner in Building Innovative Software SolutionsXpertSolvers: Your Partner in Building Innovative Software Solutions
XpertSolvers: Your Partner in Building Innovative Software Solutions
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
 
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
 
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
 
Intelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmIntelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalm
 
Asset Management Software - Infographic
Asset Management Software - InfographicAsset Management Software - Infographic
Asset Management Software - Infographic
 
why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdf
 
Engage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The UglyEngage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The Ugly
 

Lesson10 behavioral patterns

  • 1. Prepared By BAZZEKETA DATSUN MIT(MAK) Tel: 0705333525 Email: datsunbazzeketa@yahoo.com By Bazzeketa Datsun 1
  • 2.  Behavioral design patterns are concerned with the interaction and assigning of responsibility between different objects.  In these design patterns,the interaction between the objects should be in such a way that they can easily talk to each other and still should be loosely coupled.  That means the implementation and the client should be loosely coupled in order to avoid hard coding and dependencies.
  • 3.
  • 4.
  • 5.  A Command Pattern says that "encapsulate a request under an object as a command and pass it to invoker object. Invoker object looks for the appropriate object which can handle this command and pass the command to the corresponding object and that object executes the command".  It is also known as Action or Transaction
  • 6. • It separates the object that invokes the operation from the object that actually performs the operation. • It makes easy to add new commands, because existing classes remain unchanged.
  • 7. • It is used: • When you need parameterize objects according to an action perform. • When you need to create and execute requests at different times. • When you need to support rollback, logging or transaction functionality.
  • 8. • Command This is an interface for executing an operation. • Concrete Command This class extends the Command interface, implements and executes the method. This class creates a binding between the action and the receiver. • Client This class creates the Concrete Command class and associates it with the receiver. • Invoker This class asks the command to carry out the request. • Receiver This class knows to perform the operation.
  • 9.
  • 10.  Step 1: Create a ActionListernerCommand interface that will act as a Command. public interface ActionListenerCommand { public void execute(); }  Step 2: Create a Document class that will act as a Receiver. public class Document { public void open(){ System.out.println("Document Opened"); } public void save(){ System.out.println("Document Saved"); } }
  • 11.  Step 3: Create a ActionOpen class that will act as an ConcreteCommand. public class ActionOpen implements ActionListenerCommand { private Document doc; public ActionOpen(Document doc) { this.doc = doc; } @Override public void execute() { doc.open(); } }
  • 12.  Step 4: Create a ActionSave class that will act as an ConcreteCommand. public class ActionSave implements ActionListenerCommand { private Document doc; public ActionSave(Document doc) { this.doc = doc; } @Override public void execute() { doc.save(); } }
  • 13.  Step 5: Create a MenuOptions class that will act as an Invoker. public class ActionSave implements ActionListenerCommand { private Document doc; public ActionSave(Document doc) { this.doc = doc; } @Override public void execute() { doc.save(); } }
  • 14.  Step 6: Create a CommanPatternClient class that will act as a Client. public class CommandPatternClient { public static void main(String[] args) { Document doc = new Document(); ActionListenerCommand clickOpen = new ActionOpen(doc); ActionListenerCommand clickSave = new ActionSave(doc); MenuOptions menu = new MenuOptions(clickOpen, clickSave); menu.clickOpen(); menu.clickSave(); } }
  • 15.
  • 16.  A Mediator Pattern says that "to define an object that encapsulates how a set of objects interact".  I will explain the Mediator pattern by considering a problem. When we begin with development, we have a few classes and these classes interact with each other producing results. Now, consider slowly, the logic becomes more complex when functionality increases. Then what happens? We add more classes and they still interact with each other but it gets really difficult to maintain this code now. So, Mediator pattern takes care of this problem.
  • 17.  Mediator pattern is used to reduce communication complexity between multiple objects or classes. This pattern provides a mediator class which normally handles all the communications between different classes and supports easy maintainability of the code by loose coupling.
  • 18. 1. It decouples the number of classes. 2. It simplifies object protocols. 3. It centralizes the control. 4. The individual components become simpler and much easier to deal with because they don't need to pass messages to one another. 5. The components don't need to contain logic to deal with their intercommunication and therefore, they are more generic.
  • 19. • It is commonly used in message-based systems likewise chat applications. • When the set of objects communicate in complex but in well-defined ways.
  • 20.
  • 21. • ApnaChatroom :- defines the interface for interacting with participants. • ApnaChatroomImpl :- implements the operations defined by the Chatroom interface. The operations are managing the interactions between the objects: when one participant sends a message, the message is sent to the other participants. • Participant :- defines an interface for the users involved in chatting. • User1, User2, ...UserN :- implements Participant interface; the participant can be a number of users involved in chatting. But each Participant will keep only a reference to the ApnaChatRoom.
  • 22.  Step 1: Create a ApnaChatRoom interface. //This is an interface. public interface ApnaChatRoom { public void showMsg(String msg, Participant p); } // End of the ApnaChatRoom interface
  • 23.  Step 2: Create a ApnaChatRoomIml class that will implement ApnaChatRoom interface and will also use the number of participants involved in chatting through Participant interface.
  • 24.  Step 3:Create a Participant abstract class. //This is an abstract class. public abstract class Participant { public abstract void sendMsg(String msg); public abstract void setname(String name); public abstract String getName(); } // End of the Participant abstract class.
  • 25.  Step 4: Create a User1 class that will extend Participant abstract class and will use the ApnaChatRoom interface.
  • 26.  Step 5: Create a User2 class that will extend Participant abstract class and will use the ApnaChatRoom interface.
  • 27.  Step 6: Create a MediatorPatternDemo class that will use participants involved in chatting.
  • 28.
  • 29.  A Memento Pattern defines a way "to restore the state of an object to its previous state". But it must do this without violating Encapsulation. Such case is useful in case of error or failure.  The Memento pattern is also known as Token.  Undo or backspace or ctrl+z is one of the most used operation in an editor. Memento design pattern is used to implement the undo operation. This is done by saving the current state of the object as it changes state.
  • 30. 1. It preserves encapsulation boundaries. 2. It simplifies the originator.
  • 31. 1. It is used in Undo and Redo operations in most software. 2. It is also used in database transactions to Undo or Redo transactions.
  • 32.
  • 33.  Memento: • Stores internal state of the originator object. The state can include any number of state variables. • The Memento must have two interfaces, an interface to the caretaker. This interface must not allow any operations or any access to internal state stored by the memento and thus maintains the encapsulation. The other interface is Originator and it allows the Originator to access any state variables necessary to the originator to restore the previous state.  Originator: • Creates a memento object that will capture the internal state of Originator. • Use the memento object to restore its previous state.  Caretaker: • Responsible for keeping the memento. • The memento is transparent to the caretaker, and the caretaker must not operate on it.
  • 34.  Step 1: Create an Originator class that will use Memento object to restore its previous state.
  • 35.  Step 2: Create a Memento class that will Store internal state of the Originator object. //This is a class. public class Memento { private String state; public Memento(String state) { this.state=state; } public String getState() { return state; } }// End of the Memento class.
  • 36.  Step 3: Create a Caretaker class that will responsible for keeping the Memento. //This is a class import java.util.ArrayList; import java.util.List; public class Caretaker { private List<Memento> mementoList = new ArrayList<Memento>(); public void add(Memento state) { mementoList.add(state); } public Memento get(int index){ return mementoList.get(index); } } // End of the Caretaker class.
  • 37.  Step 4: Create a MementoPatternDemo class.