A small example of state patterns in software architecture and design patterns

Source: Internet
Author: User

Job Title

1. Draw UML Diagram

2. Write code

Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
Using System.Threading.Tasks;

Namespace status
{
Class Program
{
static void Main (string[] args)
{
Club account = new club ("Zhang San", 0);
Console.WriteLine ("Successful account Opening, Name:" + accounts.) Owner + ", Initial amount:" + account. Balance);
Console.WriteLine ("------------------------");
Account. Deposit (50);
Console.WriteLine ("------------------------");
Account. Deposit (100);
Console.WriteLine ("------------------------");
Console.ReadLine ();

}
Accountstate class


Public abstract class Accountstate
{
protected Club Account;
Deposit
public abstract void Deposit (double amount);
Consumption
public abstract void withdraw (double amount);
Check account status
public abstract void StateChange ();

}
  

Vistorstate class


public class Vistorstate:accountstate
{
Public vistorstate (Club account)
{
account = account;
}
public override void Deposit (double amount)
{
Account.balance + = amount;
Console.WriteLine ("+amount+" in the account of "+account.owner+", the account balance after deposit is "+account.balance+");
StateChange ();
}
public override void Withdraw (double amount)
{
Double newbalance = Account.balance-amount;
if (newbalance > 0)
{
Account.balance = newbalance;
Console.WriteLine ("To the account named" + Account.owner + "consumption" + Amount + "Yuan, the account balance after deposit is" + account.balance + "Yuan");
StateChange ();
}
Else
{
Console.WriteLine ("Your account balance is insufficient, cannot be consumed, please deposit first");
}
StateChange ();
}
public override void StateChange ()
{
if (Account.balance > && account.balance <= 1000)
{
Account.state = new Menberstate (account);
}
else if (account.balance > 1000)
{
Account.state = new Vipstate (account);
}
}
}
Menberstate class


public class Menberstate:accountstate
{
Public menberstate (Club account)
{
account = account;
}
public override void Deposit (double amount)
{
Account.balance + = amount;
Console.WriteLine ("To the account named" + Account.owner + "deposit" + Amount + "Yuan, the account balance after deposit is" + account.balance + "Yuan");
StateChange ();
}
public override void Withdraw (double amount)
{
Double newbalance = Account.balance-amount;
if (newbalance > 0)
{
Account.balance = newbalance;
Console.WriteLine ("To the account named" + Account.owner + "consumption" + Amount + "Yuan, the account balance after deposit is" + account.balance + "Yuan");
StateChange ();
}
Else
{
Console.WriteLine ("Your account balance is insufficient, cannot be consumed, please deposit first");
}
StateChange ();
}
public override void StateChange ()
{
if (Account.balance > 0&& account.balance <= 100)
{
Account.state = new Vistorstate (account);
}
else if (account.balance > 1000)
{
Account.state = new Vipstate (account);
}
}
}
Vipstate class


public class Vipstate:accountstate
{
Public vipstate (Club account)
{
account = account;
}
public override void Deposit (double amount)
{
Account.balance + = amount;
Console.WriteLine ("To the account named" + Account.owner + "deposit" + Amount + "Yuan, the account balance after deposit is" + account.balance + "Yuan");
StateChange ();
}
public override void Withdraw (double amount)
{
Double newbalance = Account.balance-amount;
if (newbalance > 0)
{
Account.balance = newbalance;
Console.WriteLine ("To the account named" + Account.owner + "consumption" + Amount + "Yuan, the account balance after deposit is" + account.balance + "Yuan");
StateChange ();
}
Else
{
Console.WriteLine ("Your account balance is insufficient, cannot be consumed, please deposit first");
}
StateChange ();
}
public override void StateChange ()
{
if (account.balance > 0 && account.balance <= 100)
{
Account.state = new Vistorstate (account);
}
else if (account.balance >100 && account.balance<=1000)
{
Account.state = new Menberstate (account);
}
}
}
 

Club Class


public class Club
{
Public Accountstate State {get; set;}
Public double Balance {get; set;}
public string Owner {get; set;}
Public Club (string owner, double initialamount)
{
Owner = owner;
Balance = Initialamount;
state = new Vistorstate (this);
}

public void Setbalance (double amount)
{
Balance = amount;
}
public void Deposit (double amount)
{
Console.WriteLine ("Now to deposit" + Amount + "Yuan");
State.deposit (amount);
Console.WriteLine ("Account status changed to" + state);
}
public void withdraw (double amount)
{
Console.WriteLine ("Now to consume" + Amount + "Yuan");
State.withdraw (amount);
Console.WriteLine ("Account status changed to" + state);

}

}

}
}

Effect

A small example of state patterns in software architecture and design patterns

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.