Design Pattern problem involving N states and transitions between them

后端 未结 6 1409
春和景丽
春和景丽 2020-12-12 16:44

I have a problem at hand and I am not getting which design pattern to use. The problem goes as such:

I have to build a system which has \'N\' states and my system ha

6条回答
  •  孤街浪徒
    2020-12-12 17:20

    State design pattern works on the concept of state change. Entire process life-cycle can be divided in multiple phases.With completion of each phase process exits from one state and enters in to another state.

    For example In JSF framework entire web request response lifecycle is divided in six phases:

    After completion of every phase process exits from a state and enters into another state. For example after RestoreValuePhase we can say ViewRestored as exit state and RequestApply as entering state .

    So to implement State design pattern It is required to divide entire process in such a way that It can be processed in multiple phases with every phase exit defining a state change.

    Now let's understand this with below code.

    Any project lifecycle can be divided in multiple phases like

    requirementAssessment
    Design
    Development
    QualityAssessment
    Deploy
    Closure
    

    So these are the phases used in below example

    Rules :

    1. We need to define a class where we can store the current state of the process. NextPhase class in below code is doing that.

    2. We need to define an Interface wherein we can provide the contact method that would be implemented in each phase.In below code ProjectPhase is doing that.

    Learn more about State design pattern here -- State Design Pattern

    http://efectivejava.blogspot.in/2013/09/java-state-design-patten-oops-state.html?utm_source=BP_recent

提交回复
热议问题