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

Lesson10 behavioral patterns

  • 1.
    Prepared By BAZZEKETA DATSUN MIT(MAK) Tel: 0705333525 Email:datsunbazzeketa@yahoo.com By Bazzeketa Datsun 1
  • 2.
     Behavioral designpatterns 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.
  • 5.
     A CommandPattern 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 separatesthe 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 isused: • 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 Thisis 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.
  • 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(); } }
  • 16.
     A MediatorPattern 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 patternis 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 decouplesthe 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 iscommonly used in message-based systems likewise chat applications. • When the set of objects communicate in complex but in well-defined ways.
  • 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:Createa 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.
  • 29.
     A MementoPattern 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 preservesencapsulation boundaries. 2. It simplifies the originator.
  • 31.
    1. It isused in Undo and Redo operations in most software. 2. It is also used in database transactions to Undo or Redo transactions.
  • 33.
     Memento: • Storesinternal 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.