zoukankan      html  css  js  c++  java
  • 状态模式

      1 using System;
      2 using System.Collections.Generic;
      3 using System.Linq;
      4 using System.Text;
      5 using System.Threading.Tasks;
      6 
      7 namespace Club
      8 {
      9     class Program
     10     {
     11         static void Main(string[] args)
     12         {
     13             Account account = new Account("Tom", 100);
     14             Console.WriteLine("Already!");
     15             Console.WriteLine("Name:{0}", account.Owner);
     16             Console.WriteLine("Initial:{0}", account.Balance);
     17             Console.WriteLine("-----------------------------------------");
     18 
     19             account.Deposit(100);
     20             Console.WriteLine("-----------------------------------------");
     21 
     22             account.Cost(300);
     23             Console.WriteLine("-----------------------------------------");
     24 
     25             account.Deposit(1000);
     26             Console.WriteLine("-----------------------------------------");
     27 
     28             account.Cost(700);
     29             Console.WriteLine("-----------------------------------------");
     30 
     31             Console.ReadLine();
     32         }
     33     }
     34 }
     35 class Account
     36 {
     37     public ClientState State { get; set; }
     38     public string Owner { get; set; }
     39     public double Balance { get; set; }
     40 
     41     public Account(string owner, double initialAmount)
     42     {
     43         Owner = owner;
     44         Balance = initialAmount;
     45         State = new VistorState(this);
     46     }
     47 
     48     public void SetBalance(double amount)
     49     {
     50         Balance = amount;
     51     }
     52 
     53     public void Deposit(double amount)
     54     {
     55         Console.WriteLine("Now,deposit {0}.", amount);
     56         State.Deposit(amount);
     57         Console.WriteLine("ClientState turn to {0}", State);
     58     }
     59 
     60     public void Cost(double amount)
     61     {
     62         Console.WriteLine("Now,Cost {0}.", amount);
     63         State.Cost(amount);
     64         Console.WriteLine("ClientState turn to {0}", State);
     65     }
     66 }
     67 abstract class ClientState
     68 {
     69     protected Account Account;
     70     public abstract void Deposit(double amount);
     71     public abstract void Cost(double amount);
     72     public abstract void Check();
     73 }
     74 class VistorState : ClientState
     75 {
     76     public VistorState(Account account)
     77     {
     78         Account = account;
     79     }
     80 
     81     public override void Deposit(double amount)
     82     {
     83         Account.Balance += amount;
     84         Console.WriteLine("Deposit {0} to {1},surplus of account have {2} affter deposit", amount, Account.Owner, Account.Balance);
     85         Check();
     86     }
     87 
     88     public override void Cost(double amount)
     89     {
     90         double newBalance = Account.Balance - amount;
     91         Account.Balance -= amount;
     92         Console.WriteLine("Cost {0} from {1},surplus of account have {2} affter cost", amount, Account.Owner, Account.Balance);
     93         Check();
     94     }
     95     public override void Check()
     96     {
     97         if (Account.Balance > 100 && Account.Balance < 1000)
     98         {
     99             Account.State = new MemberState(Account);
    100         }
    101         else if (Account.Balance >= 1000)
    102         {
    103             Account.State = new VIPState(Account);
    104         }
    105     }
    106 }
    107 class MemberState : ClientState
    108 {
    109     public MemberState(Account account)
    110     {
    111         Account = account;
    112     }
    113 
    114     public override void Deposit(double amount)
    115     {
    116         Account.Balance += amount;
    117         Console.WriteLine("Deposit {0} to {1},surplus of account have {2} affter deposit", amount, Account.Owner, Account.Balance);
    118         Check();
    119     }
    120 
    121     public override void Cost(double amount)
    122     {
    123         double newBalance = Account.Balance - amount;
    124         Account.Balance -= amount;
    125         Console.WriteLine("Cost {0} from {1},surplus of account have {2} affter cost", amount, Account.Owner, Account.Balance);
    126         Check();
    127     }
    128     public override void Check()
    129     {
    130         if (Account.Balance > 0 && Account.Balance <= 100)
    131         {
    132             Account.State = new VistorState(Account);
    133         }
    134         else if (Account.Balance >= 1000)
    135         {
    136             Account.State = new VIPState(Account);
    137         }
    138     }
    139 }
    140 class VIPState : ClientState
    141 {
    142     public VIPState(Account account)
    143     {
    144         Account = account;
    145     }
    146 
    147     public override void Deposit(double amount)
    148     {
    149         Account.Balance += amount;
    150         Console.WriteLine("Deposit {0} to {1},surplus of account have {2} affter deposit", amount, Account.Owner, Account.Balance);
    151         Check();
    152     }
    153 
    154     public override void Cost(double amount)
    155     {
    156         double newBalance = Account.Balance - amount;
    157         Account.Balance -= amount;
    158         Console.WriteLine("Cost {0} from {1},surplus of account have {2} affter cost", amount, Account.Owner, Account.Balance);
    159         Check();
    160     }
    161     public override void Check()
    162     {
    163         if (Account.Balance > 100 && Account.Balance < 1000)
    164         {
    165             Account.State = new MemberState(Account);
    166         }
    167         else if (Account.Balance > 0 && Account.Balance <= 100)
    168         {
    169             Account.State = new VistorState(Account);
    170         }
    171     }
    172 }

  • 相关阅读:
    2020-08-25日报博客
    2020-08-24日报博客
    2020-08-23日报博客
    Pytorch-区分nn.BCELoss()、nn.BCEWithLogitsLoss()和nn.CrossEntropyLoss() 的用法
    Pytorch-基于BiLSTM+CRF实现中文分词
    Pytorch-手动实现Bert的训练过程(简写版)
    Pytorch-图像分类和CNN模型的迁移学习
    Pytorch trick集锦
    Pytorch-seq2seq机器翻译模型(不含attention和含attention两个版本)
    Pytorch-GAN
  • 原文地址:https://www.cnblogs.com/CHUR/p/5092650.html
Copyright © 2011-2022 走看看