+ All Categories
Home > Documents > Bonus Presentation

Bonus Presentation

Date post: 14-Apr-2017
Category:
Upload: lahari-ganesha
View: 168 times
Download: 0 times
Share this document with a friend
23
Design Patterns Lahari Ganesha Net id: lxg150730 Section:6359.002
Transcript
Page 1: Bonus Presentation

Design PatternsLahari GaneshaNet id: lxg150730Section:6359.002

Page 2: Bonus Presentation

Behavioral Patterns• Behavioral patterns are concerned with algorithms and the

assignment of responsibilities between objects. Behavioral patterns describe not just patterns of objects or classes but also the patterns of communication between them.

• These patterns characterize complex control flow that's difficult to follow at run-time. They shift your focus away from flow of control to let you concentrate just on the way objects are interconnected.

• Behavioral class patterns use inheritance to distribute behavior between classes.

Page 3: Bonus Presentation

Command Pattern• Intent

• Encapsulate a request as an object, thereby letting you parameterizeclients with different requests, queue or log requests, and supportundoable operations.

Page 4: Bonus Presentation

• Motivation• The Command pattern lets toolkit objects make requests of

unspecified application objects by turning the request itself into an object.

• The key to this pattern is an abstract Command class, which declares an interface for executing operations.

• Concrete Command subclasses specify a receiver-action pair by storing the receiver as an instance variable and by implementing Execute to invoke the request. The receiver has the knowledge required to carry out the request.

Page 5: Bonus Presentation

Menus can be easily implemented using command patterns.. How??• The application configures

each MenuItem with an instance of a concrete Command subclass.

• When the user selects a MenuItem, the MenuItem calls Execute on its command, and Execute carries out the operation.

• Command subclasses store the receiver of the request and invoke one or more operations on the receiver.

Page 6: Bonus Presentation

• For example, PasteCommand supports pasting text from the clipboard into a Document. PasteCommand's receiver is the Document object it is supplied upon instantiation. The Execute operation invokes Paste on the receiving Document.

Page 7: Bonus Presentation

Applicability• Use command prompts when you want to• support undo.• support logging changes so that they can be reapplied in case of a

system crash. By augmenting the Command interface with load and store operations, you can keep a persistent log of changes.

Page 8: Bonus Presentation

Structure

Page 9: Bonus Presentation

Participants• Command• declares an interface for executing an operation.

• Concrete Command• defines a binding between a Receiver object and an action.• implements Execute by invoking the corresponding operation(s)

on• Receiver• knows how to perform the operations associated with carrying

out a request. Any class may serve as a Receiver.• Client• creates a ConcreteCommand object and sets its receiver.

• Invoker• asks the command to carry out the request.

Page 10: Bonus Presentation

Implementation issues• Allow undo and redo.• Avoid error accumulation during undo.

Page 11: Bonus Presentation

The abstract command class

class Command { public: virtual ~Command(); virtual void Execute() = 0; protected: Command(); };

Page 12: Bonus Presentation

PasteCommand

class PasteCommand : public Command {public:PasteCommand(Document*);virtual void Execute();private:Document* _document;};

Page 13: Bonus Presentation

PasteCommand::PasteCommand (Document* doc) { _document = doc; } Void PasteCommand::Execute () { _document->Paste(); }

Page 14: Bonus Presentation

State Pattern

• Intent

• Allows an object to alter its behavior when its internal state changes. The object will appear to change its class.

Page 15: Bonus Presentation

Motivation

• The key idea in this pattern is to introduce an abstract class called TCPState to represent the states of the network connection. The TCPState class declares an interface common to all classes that represent different operational states. Subclasses of TCPState implement state-specific behavior.

• For example, the classes TCPEstablished and TCPClosed implement behavior particular to the Established and Closed states of TCPConnection.

Page 16: Bonus Presentation

Applicability• An object's behavior depends on its state, and it must change

its behavior at run-time depending on that state.• Structure

Page 17: Bonus Presentation

Participants• Context• Defines the interface of interest to clients.• Maintains an instance of a Concrete State subclass that defines

the current state.• State• defines an interface for encapsulating the behavior associated

with a particular state of the Context.• concreteState subclasses • each subclass implements a behavior associated with a state of

the Context.

Page 18: Bonus Presentation

Implementations issues

• Who defines state transitions?• Creating and destroying state objects.

Page 19: Bonus Presentation

Sample code..First, we define the class TCPConnection, which provides aninterface for transmitting data and handles requests to change state.class TCPOctetStream;class TCPState;class TCPConnection {public:TCPConnection();void ActiveOpen();void PassiveOpen();void Close();void Send();void Acknowledge();void Synchronize();void ProcessOctet(TCPOctetStream*);private:friend class TCPState;void ChangeState(TCPState*);private:TCPState* _state;};

Page 20: Bonus Presentation

TCPConnection keeps an instance of the TCPStateclass in the _state member variable. The classTCPState duplicates the state-changing interface ofTCPConnection. Each TCPState operation takes a TCPConnection instance as a parameter, lettingTCPState access data from TCPConnection andchange the connection's state.

class TCPState {public:virtual void Transmit(TCPConnection*, TCPOctetStream*);virtual void ActiveOpen(TCPConnection*);virtual void PassiveOpen(TCPConnection*);virtual void Close(TCPConnection*);virtual void Synchronize(TCPConnection*);virtual void Acknowledge(TCPConnection*);virtual void Send(TCPConnection*);protected:void ChangeState(TCPConnection*, TCPState*);};

Page 21: Bonus Presentation

Subclasses of TCPState implement state-specific behavior.class TCPEstablished : public TCPState {public:static TCPState* Instance();virtual void Transmit(TCPConnection*, TCPOctetStream*);virtual void Close(TCPConnection*);};class TCPListen : public TCPState {public:static TCPState* Instance();virtual void Send(TCPConnection*);// ... };class TCPClosed : public TCPState {public:static TCPState* Instance();virtual void ActiveOpen(TCPConnection*);virtual void PassiveOpen(TCPConnection*);// ...};

Page 22: Bonus Presentation

Each TCPState subclass implements state-specific behaviorfor valid requests in the state:

void TCPClosed::ActiveOpen (TCPConnection* t) {// send SYN, receive SYN, ACK, etc.ChangeState(t, TCPEstablished::Instance());}void TCPClosed::PassiveOpen (TCPConnection* t) {ChangeState(t, TCPListen::Instance());}void TCPEstablished::Close (TCPConnection* t) {// send FIN, receive ACK of FINChangeState(t, TCPListen::Instance());}void TCPEstablished::Transmit ( TCPConnection* t, TCPOctetStream* o ) {t->ProcessOctet(o);}void TCPListen::Send (TCPConnection* t) {// send SYN, receive SYN, ACK, etc.ChangeState(t, TCPEstablished::Instance());}

Page 23: Bonus Presentation

THANK YOU


Recommended