简单工厂模式是由一个工厂对象决定创建出哪一种产品类的实例。简单工厂模式是工厂模式家族中最简单实用的模式,可以理解为是不同工厂模式的一个特殊实现。
场景:有一个图书管理系统,需要进行书本管理,所有对书本的操作都进行归类,大致可以分成入账、预定、借用、续借、归还、领料、清退、转移、核销等操作,每一次的操作认为是一个订单,订单有创建,审批、取消、拒绝等状态、每一个订单包含一个或者以上的书本信息,简单工厂,先根据订单类型来创建完订单,需要先构建好入参,如下所示 strUserDoc 主要是当前操作人信息(用户上下文),strBillDoc为订单信息,订单号、类型、订单状态等,strBooks包含订单的书本信息
//创建订单 var oBill = GetOperationType(strUserDoc, strBillDoc, strBooks); //获取订单类型 var strOperaType =oBillDoc.GetStrValue(TBLBill.PROP_OPERATE_TYPE); //初始化订单 switch (strOperaType) { case TBLBill.TYPE_BOOK: oBill = new Book(strUserDoc, strBillDoc, strBooks); break; case TBLBill.TYPE_LEND: oBill = new Lend(strUserDoc, strBillDoc, strBooks); break; case TBLBill.TYPE_RENEW: oBill = new Renew(strUserDoc, strBillDoc, strBooks); break; case TBLBill.TYPE_RETURN: oBill = new Return(strUserDoc, strBillDoc, strBooks); break; case TBLBill.TYPE_CLEAR: oBill = new Clear(strUserDoc, strBillDoc, strBooks); break; case TBLBill.TYPE_DELETE: oBill = new Delete(strUserDoc, strBillDoc, strBooks); break; case TBLBill.TYPE_PICK: oBill = new Pick(strUserDoc, strBillDoc, strBooks); break; case TBLBill.TYPE_MOVE: oBill = new Move(strUserDoc, strBillDoc, strBooks); break; case TBLBill.TYPE_TRANSFER: oBill = new Transfer(strUserDoc, strBillDoc, strBooks); break; case TBLBill.TYPE_RECORD: oBill = new Record(strUserDoc, strBillDoc, strBooks); break; default: oBill.res.ErrCode = "这是什么操作~"; break; }
创建订单时,先对当前操作人权限进行验证,操作对象是否能操作对应的数据,文书信息等是否能进行对应的操作等进行一系列验证,验证完毕后,创建好订单,然后在订单中增加书本信息,根据订单的状态,修改对应文书最新状态
/// <summary> /// 基础订单(图书馆操作系统) /// </summary> public abstract class BaseBill { #region /// <summary> /// 原始订单信息 /// </summary> public BsonDocument oOriginalBill { get; set; } /// <summary> /// 处理的订单信息 /// </summary> public BsonDocument oHandleBillDoc { get; set; } /// <summary> /// 书本信息 /// </summary> public List<BsonDocument> lstBooks { get; set; } /// <summary> /// 用户上下文 /// </summary> public UserContext userContext { get; set; } #endregion #region 构造函数 protected BaseBill() { } protected BaseBill(string strUserDoc, string strBillDoc, string strBooks) { this.userContext = new UserContext(strUserDoc); this.oOriginalBill = JsonToBson(strBillDoc); this.oHandleBillDoc = this.oOriginalBill.CopyTo(); this.lstBooks = getBooks(strBooks); } #endregion #region 订单基础操作 /// <summary> /// 验证用户是否有权限/合法等 /// </summary> public virtual void ValidateUser() { } /// <summary> /// 验证物料是否存在,是否能被借出/归还等 /// </summary> public virtual void ValidateBooks() { } /// <summary> /// 创建订单 /// </summary> public virtual void AddBill() { } /// <summary> /// 增加订单书本 /// </summary> public virtual void AddDevicesBill() { } /// <summary> /// 根据订单状态修改竖纹状态 /// </summary> public virtual void ModefyStatus() { } /// <summary> /// 结果信息发送 /// </summary> public virtual void SendMessage() { } #endregion } /// <summary> /// 预定 /// </summary> public class Book : BaseBill { } /// <summary> /// 借出操作 /// </summary> public class Lend : BaseBill { } /// <summary> /// 续借操作 /// </summary> public class Renew : BaseBill { } /// <summary> /// 归还操作 /// </summary> public class Return : BaseBill { } /// <summary> /// 核销操作 /// </summary> public class Delete : BaseBill { } /// <summary> /// 清退操作 /// </summary> public class Clear : BaseBill { } /// <summary> /// 领料操作 /// </summary> public class Pick : BaseBill { } /// <summary> /// 入账操作 /// </summary> public class Record : BaseBill { }
原文地址:https://www.cnblogs.com/singlelcx/p/11240681.html
时间: 2024-10-10 04:36:15