Giter Site home page Giter Site logo

designpatternsincsharp's Introduction

Design patterns in C#

State pattern is behavioral software design pattern that helps You to avoid a lot of if else and/or swich statements by changing state of object (and thus his behaviour) by changing his property (into different state objects) and delegating actions(method) that change state to that objects. state pattern image Class Account has flags and has to check their values in methods to fulfil business logic (user requirements)

public class Account
   {
       public decimal Balance { get; private set; }
       public bool IsVerified { get; private set; }
       public bool IsClosed { get; private set; }
       public bool IsFrozen { get; private set; }
       private Action OnUnfreeze { get; }
       public Account(Action onUnfreeze=null)
       {
           this.OnUnfreeze = onUnfreeze;
       }

       public void Deposit(decimal amount)
       {
           if (IsClosed)
               return;
           if (this.IsFrozen)
           {
               this.IsFrozen = false;
               this.OnUnfreeze();
           }
           Balance += amount;
       }
       public void Withdrow(decimal amount)
       {
           if (!IsVerified)
               return;
           if (IsClosed)
               return;
           if (amount > Balance)
               return;
           if (this.IsFrozen)
           {
               this.IsFrozen = false;
               this.OnUnfreeze();
           }
           Balance -= amount;
       }
       public void Open()
       {
           this.IsClosed = false;
       }
       public void Close()
       {
           this.IsClosed = true;
       }

       public void HolderVerified()
       {
           IsVerified = true;
       }
       public void Freeze()
       {
           if (this.IsClosed)
               return;
           if (!this.IsVerified)
               return;
           this.IsFrozen = true;
       }
   }
  • Class Account after state pattern is applied is readable and maintainable. As You can see methods are delegated to current state of Account and in those method change of state might happen. After state pattern tag v2.0
public class Account
    {
        public decimal Balance { get;  set; }
        public AccountState State  { get; set; }
        public Action OnUfreezeAction  { get; set; }


        public Account(Action onUnfreezeAction)
        {
               OnUfreezeAction = onUnfreezeAction;
               State = new AccountStateOpened(this);
        }
     
	 public void Deposit(decimal amount)
        {
            State.Deposit(amount);
        }

        public void Withdrow(decimal amount)
        {
            State.Withdrow(amount);
        }
        public void Open()
        {
            State.Open();
        }
        public void Close()
        {
            State.Close();
        }

        public void HolderVerified()
        {
            State.HolderVerified();
        }
        public void Freeze()
        {
            State.Freeze();
        }
    }

Mediator pattern

https://github.com/MirzaAbazovic/mediator-pattern

designpatternsincsharp's People

Contributors

mirzaabazovic avatar

Stargazers

 avatar

Watchers

 avatar

Forkers

plumpmath

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.