BookRags.com Literature Guides Literature
Guides
Criticism & Essays Criticism &
Essays
Questions & Answers Questions &
Answers
Lesson Plans Lesson
Plans
My Bibliography Periodic Table U.S. Presidents Shakespeare Sonnet Shake-Up
Research Anything:        
History | Encyclopedias | Films | News | Create a Bibliography | More... Login | Register | Help
Not What You Meant?  There are 38 definitions for State.

State pattern

Print-Friendly
About 2 pages (679 words)

Bookmark and Share Know this topic well? Help others and get FREE products!

The state pattern is a behavioral software design pattern, also known as the objects for states pattern. This pattern is used in computer programming to represent the state of an object. This is a clean way for an object to partially change its type at runtime. Take for example, a drawing program, in which there could be an abstract interface representing a tool, then concrete instances of that class could each represent a kind of tool. When the user selects a different tool, the appropriate tool would be instantiated. For example, an interface to a drawing tool could be <source lang="cpp">

class AbstractTool {
public:
  virtual void MoveTo(const Point& inP) = 0;
  virtual void MouseDown(const Point& inP) = 0;
  virtual void MouseUp(const Point& inP) = 0;
}; </source>

Then a simple pen tool could be <source lang="cpp">

class PenTool : public AbstractTool {
public:
  PenTool() : mMouseIsDown(false) {}
  virtual void MoveTo(const Point& inP) {
    if(mMouseIsDown) {
      DrawLine(mLastP, inP);
    }
    mLastP = inP;
  }
  virtual void MouseDown(const Point& inP) {
    mMouseIsDown = true;
    mLastP = inP;
  }
  virtual void MouseUp(const Point& inP) { 
    mMouseIsDown = false;
  }
private:
  bool mMouseIsDown;
  Point mLastP;
};

class SelectionTool : public AbstractTool {
public:
  SelectionTool() : mMouseIsDown(false) {}
  virtual void MoveTo(const Point& inP) {
    if(mMouseIsDown) {
      mSelection.Set(mLastP, inP);
    }
  }
  virtual void MouseDown(const Point& inP) {
    mMouseIsDown = true;
    mLastP = inP;
    mSelection.Set(mLastP, inP);
  }
  virtual void MouseUp(const Point& inP) { 
    mMouseIsDown = false;
  }
private:
  bool mMouseIsDown;
  Point mLastP;
  Rectangle mSelection;
}; </source>

A client using the state pattern above could look like this: <source lang="cpp">

class DrawingController {
 public:
  DrawingController() { selectPenTool(); } // Start with some tool.
  void MoveTo(const Point& inP) {currentTool->MoveTo(inP)}
  void MouseDown(const Point& inP) {currentTool->MouseDown(inP)}
  void MouseUp(const Point& inP) {currentTool->MouseUp(inP)}

  selectPenTool() {
    currentTool.reset(new PenTool);
  }

  selectSelectionTool() {
    currentTool.reset(new SelectionTool);
  }

 private:
  std::auto_ptr<AbstractTool> currentTool;
};

</source> The state of the drawing tool is thus represented entirely by an instance of AbstractTool. This makes it easy to add more tools and to keep their behavior localized to that subclass of AbstractTool.

Contents

Diagram

+-------------------+               +---------------------+
|      Context      |              1|                     |
|-------------------|<>------------>|   ContextState      |
|-------------------|          state|---------------------|
|                   |               |---------------------|
+-------------------+        +----|>|+DoAction ()         |<|---+
                             |      +---------------------+     | 
                             |      {disjoint, incomplete}      |
                             |                                  |
                 (Implements)|                      (Implements)|
                             |                                  |
                 +---------------------+              +---------------------+
                 | Concrete1           |              | Concrete2           |
                 |---------------------|              |---------------------|
                 |---------------------|              |---------------------|
                 |+DoAction ()         |              |+DoAction ()         |
                 +---------------------+              +---------------------+

As opposed to using switch

The state pattern can be used to replace switch() statements which can be difficult to maintain and are less type-safe. For example, the following is similar to the above but adding a new tool type to this version would be much more difficult. <source lang="cpp">

class Tool {
public:
  Tool() : mMouseIsDown(false) {}
  virtual void MoveTo(const Point& inP);
  virtual void MouseDown(const Point& inP);
  virtual void MouseUp(const Point& inP);
private:
  enum Mode { Pen, Selection };
  Mode mMode;
  Point mLastP;
  bool mMouseIsDown;
  Rectangle mSelection;
};

void Tool::MoveTo(const Point& inP) {
  switch(mMode) {
  case Pen:
    if(mMouseIsDown) {
      DrawLine(mLastP, inP);
    }
    mLastP = inP;
    break;
  case Selection:
    if(mMouseIsDown) {
      mSelection.Set(mLastP, inP);
    }
    break;
  default:
    throw std::exception();
  }
}

void Tool::MouseDown(const Point& inP) {
  switch(mMode) {
  case Pen:
    mMouseIsDown = true;
    mLastP = inP;
    break;
  case Selection:
    mMouseIsDown = true;
    mLastP = inP;
    mSelection.Set(mLastP, inP);
    break;
  default:
    throw std::exception();
  }
}

void Tool::MouseUp(const Point& inP) {
  mMouseIsDown = false;
} </source>

See also

External links

View More Summaries on State pattern
 
Ask any question on State pattern and get it answered FAST!
Answer questions in BookRags Q&A and earn points toward
discounted or even FREE Study Guides and other BookRags products!
Learn more about BookRags Q&A
Copyrights
State pattern from Wíkipedia. ©2006 by Wíkipedia. Licensed under the GNU Free Documentation License. View a list of authors or edit this article.

Article Navigation
Join BookRagslearn moreJoin BookRags




About BookRags | Customer Service | Report an Error | Terms of Use | Privacy Policy