state,pattern

Erhan 8/7/2016 0

State Pattern

C#
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
Gof 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.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace StatePattern
{
    public class Job
    {
        public delegate void JobStateChanged(object sender, JobState newJobState);
        public event JobStateChanged OnJobStateChanged;

        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 ;
        }

        private void DoJob()
        {
            State = JobState.Busy;
            new Thread (new ThreadStart(JobMethod)).Start();
        }

        private void JobMethod()
        {
            Application.DoEvents();
            Thread.Sleep(5000);
            State = JobState.Ready;
        }
    }

    public enum JobState
    {
        Ready,
        Busy
    }
}


******************************************
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace StatePattern
{
    public partial class MainForm : Form
    {
        private Job _job = new Job();

        public MainForm()
        {
            InitializeComponent();

            Control.CheckForIllegalCrossThreadCalls = false ;

            // listen to state change event
            _job.OnJobStateChanged  = new Job.JobStateChanged (_job_OnJobStateChanged);
        }

        private void _job_OnJobStateChanged(object sender, JobState newJobState)
        {
            lbl.Text = msg.Text = newJobState.ToString();
            if (newJobState == JobState .Busy)
            {
                pbr.Style = ProgressBarStyle.Marquee;
            }
            else
            {
                pbr.Style = ProgressBarStyle.Blocks;
            }

            Application.DoEvents();
        }

        private void btn_Click(object sender, EventArgs e)
        {
            bool result = _job.AddJob("job" );
            if (result)
                msg.Text = "Succcessfully added new job!";
            else
                msg.Text = "Unable to add job!";
        }
    }
}

  
 

Report Bug

Please Login to Report Bug

Reported Bugs

Comments

Please Login to Comment

Comments