SIGN UP MEMBER LOGIN:    
ARTICLE

State Pattern

Posted by Jean Paul Articles | Design & Architecture December 22, 2011
In this article I am going to explain the State Pattern. It is one among the 23 design patters and provides a good solution to a common problem.
Reader Level:
Download Files:
 

In this article I am going to explain the State Pattern. It is one among the 23 design patters and provides a good solution to a common problem.
 
As usual the pattern starts with a challenge and then implementing the solution using the pattern.

Challenge

You are working on a job processing application. The application can handle only 1 job at a time. So when a new job arrives the system response will be weird. How to provide a better approach to the above problem?

Definition

Allow an object to alter its behavior when its internal state changes. The object will appear to change its class."

Implementation

An enumeration named JobState is used to define the states used in our application.

    public enum JobState
    {
        Ready,
        Busy
    }

This enumeration is used inside our Job class.

    public class Job
    {
        private JobState _State;

        public JobState State
        {
            get { return _State; }
            set
            {
                _State = value;

                if (OnJobStateChanged != null)
                    OnJobStateChanged(this, _State);
            }
        }

        public bool AddJob(object jobParameter)
        {
            switch (State)
            {
                case JobState.Ready:
                    {
                        DoJob();
                        return true;
                    }
                case JobState.Busy:
                    {
                        return false;
                    }
            }

            return false;
        }
    }


In the AddJob() method we can see the new job is taken only if the JobState is in Ready state. Here true will be returned by the method. If there is another job running, then the new job will not be queued and false will be returned.

Thus the object Job changes the behavior based on the internal state.

Application

The associated application contains the classes we discussed.

On executing the windows application and clicking the Assign Job button, you can see the following screen:

StatePattern.jpg

While a job is in process, we cannot add another job. If a try is made the response is:

Summary

In this article we have seen about State Pattern. The attachment file contains the source code of the application we have discuseed.
 

Login to add your contents and source code to this article
share this article :
post comment
 

Thank You Thomas for the good words.. Although I thought of adding more description it ended up as a small article :)

Posted by Jean Paul Dec 26, 2011

I went through your source code , It was nice working on it. Thanks

Posted by Thomas Mapother Dec 26, 2011
Nevron Gauge for SharePoint
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
    Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor