Application of Memorandum Mode of Computer Room Reconfiguration

table of Contents

What is the memo mode

Precautions

Application of computer room reconstruction

First create a BasicDataEntity class in the entity layer-initiator class

Memo class—BasicDataMemento

Manager class-BaiscDataCaretaker

UI layer, when the form is recorded, the data is passed to the entity layer (initiator class)

When you click the restore button, transfer the data in the memo


What is the memo mode

The so-called memo mode is to capture the internal state of an object without destroying the encapsulation, and save this state outside the object, so that the object can be restored to the original state in the future.

Precautions

  • A memo class is used to store the state of the object.
  • The client is not coupled with the memo class, but with the memo management class.
  • In order to comply with Dimit's principle, a category for managing memos should be added.

The memo mode uses three classes  Memento , Originator (initiator class)  and  CareTaker (manager class) .

Memento (memorandum class): Contains the state of the object to be restored.

Originator (initiator class): Create and store state in Memento object.

Caretaker (manager class): The object is responsible for restoring the state of the object from Memento.

Application of computer room reconstruction

Realization of ideas: The memo mode used in the basic form data set, when the method when the form is loaded with the originator class create a memo can be the data from the interface - → initiator - → memorandum (live storage) , When the restore button is clicked , the data in the memo is obtained by the method of restoring the memo of the initiator .

 

First create a BasicDataEntity class in the entity layer-initiator class

/// <summary>
/// 基本数据设定
/// </summary>
public class BasicDataEntity
{
     //VIP每小时收费
     //临时用户收费
     //至少上机时间
     //单位递增时间
     //最少金额
     private decimal vipRate;
     public decimal VipRate { get; set; }

     private decimal customerRate;
     public decimal CustomerRate { get; set; }

     private int leastTime;
     public int LeastTime { get; set; }

     private int unitTime;
     public int UnitTime { get; set; }

     public decimal minCash;
     public decimal MinCash { get; set; }

     //创建备忘录,
     public BasicDataMemento CreateMemento()
     {
            //将属性值传进备忘录
        return (new BasicDataMemento(vipRate, customerRate, leastTime, unitTime, minCash));
     }

     //恢复备忘录
     public void SetMemento(BasicDataMemento memento)
     {
         //恢复备忘录,将Memento导入并将相关数据恢复
          vipRate = memento.VipRate;
          customerRate = memento.CustomerRate;
          leastTime = memento.LeastTime;
          unitTime = memento.UnitTime;
          minCash = memento.MinCash;
     }

}

Memo class—BasicDataMemento

/// <summary>
/// 备忘录类
/// </summary>
public  class BasicDataMemento
{
     //只读属性
     private decimal vipRate;
     public decimal VipRate { get;  }

     private decimal customerRate;
     public decimal CustomerRate { get;  }

     private int leastTime;
     public int LeastTime { get; }

     private int unitTime;
     public int UnitTime { get;  }

     public decimal minCash;
     public decimal MinCash { get; }

     //构造方法,将相关数据导入
     public BasicDataMemento(decimal vipRate, decimal customerRate, int leastTime, int unitTime,decimal minCash)
     {
         this.vipRate = vipRate;
         this.customerRate = customerRate;
         this.leastTime = leastTime;
         this.unitTime = unitTime;
         this.minCash = minCash;
     }

}

Manager class-BaiscDataCaretaker

/// <summary>
/// 管理者类
/// </summary>
public  class BaiscDataCaretaker
{
   private BasicDataMemento memento;
   //得到备忘录
   public BasicDataMemento Memento
   {
       get { return memento; }
       set { memento = value; }
   }

}

UI layer, when the form is recorded, the data is passed to the entity layer (initiator class)

//实例化发起者、管理者
Entity.Boss.BasicDataEntity originator = new Entity.Boss.BasicDataEntity();
Entity.Boss.BaiscDataCaretaker caretaker = new Entity.Boss.BaiscDataCaretaker();
private void frmBasicData_Load(object sender, EventArgs e)
{
    //实例化外观层
     Facade.SelectDataFacade facade = new Facade.SelectDataFacade();
     DataTable dt = facade.SelectData();
           
     //数据绑定,将数据库中的数据显示到窗体
      try
      {
          //显示到窗体
          txtVipRate.Text = dt.Rows[0]["Rate"].ToString();
          txtCustomerRate.Text = dt.Rows[0]["tmpRate"].ToString();
          txtLeastTime.Text = dt.Rows[0]["leastTime"].ToString();
          txtUnitTime.Text = dt.Rows[0]["unitTime"].ToString();
          txtMinCash.Text = dt.Rows[0]["limitCash"].ToString();
      }
      catch (Exception)
      {

           MessageBox.Show("数据未能成功加载");
      }
        
      //给备忘录的发起者
      //记录窗体中的初始数据
      originator.VipRate =decimal.Parse( txtVipRate.Text);
      originator.UnitTime = int.Parse(txtUnitTime.Text);
      originator.LeastTime=int.Parse(txtLeastTime.Text);
      originator.CustomerRate = decimal.Parse(txtCustomerRate.Text);
      originator.MinCash = decimal.Parse(txtMinCash.Text);

      //存进备忘录
      caretaker.Memento = originator.CreateMemento();

}

When you click the restore button, transfer the data in the memo

//恢复按钮
private void btnstore_Click(object sender, EventArgs e)
{
      //恢复备忘录中的数据
      originator.SetMemento(caretaker.Memento);
      txtVipRate.Text = originator.VipRate.ToString() ;
      txtUnitTime.Text = originator.UnitTime.ToString();
      txtCustomerRate.Text = originator.CustomerRate.ToString();
      txtLeastTime.Text = originator.LeastTime.ToString();
      txtMinCash.Text = originator.MinCash.ToString();
}

If this blog is helpful to you, please remember to leave a message + like it.

Guess you like

Origin blog.csdn.net/promsing/article/details/108386569