Code Monkey home page Code Monkey logo

statepattern's Introduction

We have the IState interface, which defines the contract for different states. The StateA, StateB, and StateC classes are concrete implementations of the state interface.

The Context class represents the object that has different states. It maintains a reference to the current state and delegates the handling of requests to the current state by calling its Handle method. The Context class also provides a method called Request to initiate a request.

In the Main method, we create an instance of the Context class. We then call the Request method on the context multiple times, which triggers the handling of the request by the current state. The output displays the messages indicating which state is currently handling the context.

The State Pattern allows an object to alter its behavior when its internal state changes. It encapsulates each state as a separate class, and the object's behavior varies based on its internal state. This pattern promotes loose coupling between the client and the states, as the client interacts with the context object without being aware of the specific state classes.

using System;

// Step 1: Define the state interface
public interface IState
{
    void Handle(Context context);
}

// Step 2: Implement concrete states
public class StateA : IState
{
    public void Handle(Context context)
    {
        Console.WriteLine("State A is handling the context.");
        // Perform state-specific operations
        // Transition to the next state if needed
        context.State = new StateB();
    }
}

public class StateB : IState
{
    public void Handle(Context context)
    {
        Console.WriteLine("State B is handling the context.");
        // Perform state-specific operations
        // Transition to the next state if needed
        context.State = new StateC();
    }
}

public class StateC : IState
{
    public void Handle(Context context)
    {
        Console.WriteLine("State C is handling the context.");
        // Perform state-specific operations
        // Transition to the next state if needed
        context.State = new StateA();
    }
}

// Step 3: Define the context
public class Context
{
    public IState State { get; set; }

    public Context()
    {
        // Set the initial state
        State = new StateA();
    }

    public void Request()
    {
        // Delegate the handling to the current state
        State.Handle(this);
    }
}

// Step 4: Usage example
class Program
{
    static void Main(string[] args)
    {
        // Create the context
        var context = new Context();

        // Perform requests on the context
        context.Request(); // State A handles the context
        context.Request(); // State B handles the context
        context.Request(); // State C handles the context
        context.Request(); // State A handles the context
    }
}

statepattern's People

Contributors

melihco avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.