Introduction to State Design Pattern

来源:互联网 发布:微信复制淘宝打不开 编辑:程序博客网 时间:2024/05/21 09:50
In software development, a pattern (or design pattern) is a written document that describes a general solution to a design problem that recurs repeatedly in many projects. Software designers adapt the pattern solution to their specific project. Patterns use a formal approach to describing a design problem, its proposed solution, and any other factors that might affect the problem or the solution.

In object-oriented programming, a pattern can contain the description of certain objects and object classes to be used, along with their attributes and dependencies, and the general approach to how to solve the problem. Similarly, state patterns started to appear. In contrast to the OO patterns, which are concerned with optimal ways of structuring classes and objects, the state design patterns focus on effective ways of structuring states, events and transitions.

Just as an OO pattern, a state pattern has five essential elements:


  1.    The pattern name: a word or two denoting the problem, solution and the consequences of a pattern. A good name is vital, because it will become part of your vocabulary.  
   
  2.    The problem: explanation of the problem the pattern addresses. A problem is often motivated by an example.  
   
  3.    The solution: description of the elements (states, transitions, events, actions, and extended state variables) comprising the solution, their relationships, responsibilities and collaborations.  
   
   
  4.    The sample code: presentation of a concrete implementation of an instance of the pattern. Usually the sample code implements the motivating example.  
   
  5.    The consequences: the results and trade-offs of applying the pattern.  
   
   
The following sections describe some state design patterns which are adapted from "Practical Statecharts in C/C++: Quantum Programming for Embedded Systems" Miro Samek, Ph.D. Quantum Leaps.

原创粉丝点击