Published on

Object-oriented analysis and design(OOAD) and Design Pattern Slides UML Slides. for more slides refer

Published in: Technology, Business
  • Be the first to comment

  • Be the first to like this

No Downloads
Total Views
On Slideshare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide


  1. 1. STATE Object Diagram for State using Vending Machine Example The State pattern allows an object to change its behavior when its internal state changes. This pattern can be observed in a vending machine. Vending machines have states based on the inventory, amount of currency deposited, the ability to make change, the item selected, etc. When currency is deposited and a selection is made, a vending machine will either deliver a product and no change, deliver a product and change, deliver no product due to insufficient currency on deposit, or deliver no product due to inventory depletion
  2. 2. <ul><li>Intent </li></ul><ul><li>Allow an object to alter its behavior when its internal state changes. The object will appear to change its class </li></ul><ul><li>Also Known As </li></ul><ul><li>Objects for States </li></ul>
  3. 3. <ul><li>Problem: </li></ul><ul><li>A object can be in one of several different states, i.e. its behavior is a function of its state </li></ul><ul><li>When a object receives request form other objects, it responds differently ( i.e.. changes its behavior ) depending on its current state </li></ul><ul><li>Such an application is likely to be characterized by large and numerous case statements </li></ul><ul><li>But we know that conditional logic is undesirable because of complexity, scalability or duplication. </li></ul>
  4. 4. <ul><li>Illustration : Example </li></ul><ul><li>Consider a class TCPConnection that represents a network connection. </li></ul><ul><li>A TCPConnection object can be in one of several different states: Established, Listening, closed. </li></ul>
  5. 5. <ul><li>Illustration : Problem </li></ul><ul><li> </li></ul><ul><li>When TCPconnection object receives requests from other objects, it responds differently depending on its current state. </li></ul><ul><li>The effect of an open request depends on the state of the connection (i.e. closed state or its established state) </li></ul>
  6. 6. <ul><li>Illustration : Solution </li></ul><ul><li>Introduce an abstract class called TCPState to represent the states of the network connection. </li></ul><ul><li>The TCPState class declares an interface common to all classes that represent different operational states. </li></ul><ul><li>Subclasses of TCPState implement State-specific behavior like TCPClosed implements behavior particular to Closed state </li></ul><ul><li>& TCPEstablished behavior particular to Established. </li></ul><ul><li>Maintain in TCPConnection a state object ( ie. An instance of a subclass of TCPState) that represent the current state of the TCP connection. </li></ul>
  7. 7. <ul><li>Applicability </li></ul><ul><li>Use the State pattern in either of the following cases </li></ul><ul><li>An object’s behavior depends on its state, and it must change its behavior at runtime depending on that state. </li></ul><ul><li>Operations have large, multipart conditional statements that depends on the object’s state. </li></ul><ul><li>Often, several operations will contain this same conditional structure. </li></ul><ul><li>The state pattern puts each branch of the conditional in a separate class.This lets you treat the object’s state as an object in its own right that can vary independently from other objects. </li></ul>
  8. 8. <ul><li>Structure </li></ul>
  9. 9. <ul><li>Collaborations </li></ul><ul><li>Context delegates state-specific request to the current concreteState object. </li></ul><ul><li>A context may pass itself as an argument to the state object handling the request. This lets the state object access the context if necessary </li></ul><ul><li>Context is the primary interface for clients. Clients can configure a context with state objects. Once a context is configured, its clients don’t have to deal with the state object directly </li></ul><ul><li>Either context or the ConcreteState subclasses can decide which state succeeds another and under what circumstances. </li></ul>
  10. 10. <ul><li>Consequences </li></ul><ul><li>It localizes state-specific behavior and partitions behavior for different states. </li></ul><ul><li>It makes state transitions explicit. </li></ul><ul><li>State objects can be shared. </li></ul>
  11. 11. <ul><li>Model </li></ul>Code next slide>>> Singleton Non virtual Note Ref of TCPConnection is taken
  12. 12. Code: <ul><li>//Default behavior: </li></ul><ul><li>/* virtual */ </li></ul><ul><li>void TCPState::Transmit (TCPConnection*, TCPOctetStream*) { } </li></ul><ul><li>void TCPState::ActiveOpen (TCPConnection*) { } </li></ul><ul><li>void TCPState::PassiveOpen (TCPConnection*) { } </li></ul><ul><li>void TCPState::Close (TCPConnection*) { } </li></ul><ul><li>void TCPState::Synchronize (TCPConnection*) { } </li></ul><ul><li>/* </li></ul><ul><li>non - virtual */ </li></ul><ul><li>void TCPState::ChangeState (TCPConnection* t, TCPState* s) { </li></ul><ul><li>t->ChangeState(s); </li></ul><ul><li>} </li></ul>
  13. 13. Code: <ul><li>/* Individual state classes will have there own speacific methods */ </li></ul><ul><li>void TCPEstablished::Close (TCPConnection* t) { </li></ul><ul><li>// send FIN, receive ACK of FIN </li></ul><ul><li>ChangeState(t, TCPListen::Instance()); </li></ul><ul><li>} </li></ul><ul><li>void TCPEstablished::Transmit ( </li></ul><ul><li>TCPConnection* t, TCPOctetStream* o </li></ul><ul><li>) { </li></ul><ul><li>t->ProcessOctet(o); </li></ul>
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.