Drop Your Business Card



Insurance Industry & Design Patterns

Technical

Insurance Industry & Design Patterns

State design pattern provides a mechanism to change the behavior of an object based on the object’s state. We can see a lot of real-world examples for the need of state design pattern. As a very simple example, think of a fan, which has a capacitor and motor inside and a speed controlling interface. Using that knob we can increase, decrease or even stop the speed of the fan. Based on speed ‘state’ the behavior changes.

 

Class Diagram for State Design Pattern

Since we are in Insurance Domain, let us take an example scenario having an insurance policy. With respect to policy transaction processed over a policy, a policy can be in different states. For example, active, cancelled, expired or re-instated. Based on current state, behaviour of the policy changes. Which is a suitable scenario for state design pattern. Following class diagram is for this example scenario,

 

State Design Pattern Java Example

PolicyChangeState.java

This interface represents the different states involved. All the states should implement this interface.

package com.hawk.designpatterns.state;public interface PolicyChangeState {      public void change(ChangeStateContext ctx);}

ChangeStateContext.java

This class maintains the current state and is the core of the state design pattern. A client should access / run the whole setup through this class.

package com.hawk.designpatterns.state;public class ChangeStateContext {               private PolicyChangeState currentState;               public ChangeStateContext() {                               currentState = new PolicyActive();               }               public void setState(PolicyChangeState state) {                               currentState = state;               }               public void change() {                               currentState.change(this);               }}

PolicyActive.java

Representation of a state implementing the abstract state object.

package com.hawk.designpatterns.state;public class PolicyActive implements PolicyChangeState {               @Override               public void change(ChangeStateContext ctx) {                               System.out.println(“Active…”);               }}

PolicyCanceled.java

Representation of a state implementing the abstract state object.

package com.hawk.designpatterns.state;public class PolicyCanceled implements PolicyChangeState {               @Override               public void change(ChangeStateContext ctx) {                               System.out.println(“Cancelled…”);               }}

PolicyExpired.java

Representation of a state implementing the abstract state object.

package com.hawk.designpatterns.state;public class PolicyExpired implements PolicyChangeState {               @Override               public void change(ChangeStateContext ctx) {                               System.out.println(“Expired…”);               }}

StatePattern.java

package com.hawk.designpatterns.state; public class TestPolicyStatus {               public static void main(String[] args) {                               ChangeStateContext stateContext = new ChangeStateContext();                               stateContext.change();                               stateContext.setState(new PolicyCancelled());                               stateContext.change();                               stateContext.setState(new PolicyActive());                               stateContext.change();                               stateContext.setState(new PolicyExpired());                               stateContext.change();               }}

Output of the Example State Pattern Code –

Active… Cancelled… Active… Expired…

 

State Design Pattern Benefits

The benefits of using State pattern to implement polymorphic behaviour is clearly visible. The chances of error are less and it’s very easy to add more states for additional behaviour. Thus making our code more robust, easily maintainable and flexible. Also State pattern helped in avoiding if-else or switch-case conditional logic in this scenario.

State Pattern is very similar to Strategy Pattern.

This blog talks about the design pattern in an abstract way. I will be happy to learn about your experiences using this pattern in your project. Feel free to leave your suggestions in the comment box below.

 

In my next blog, I will explain how this design pattern is implemented in insurance industry. Stay tuned.

Post a comment