首页 > 代码库 > 使用Facade模式更新库存、确认订单、采取打折、确认支付、完成支付、物流配送
使用Facade模式更新库存、确认订单、采取打折、确认支付、完成支付、物流配送
Facade模式对外提供了统一的接口,而隐藏了内部细节。在网上购物的场景中,当点击提交订单按钮,与此订单相关的库存、订单确认、折扣、确认支付、完成支付、物流配送等都要做相应的动作。本篇尝试使用Facade模式,把这些类似工作者单元的动作隐藏到一类中,只要点击提交订单,余下的事情一步到位:
□ 关于库存
namespace ConsoleApplication1.Interfaces { public interface IInventory { void Update(int productId); } } using System; using ConsoleApplication1.Interfaces; namespace ConsoleApplication1.Implements { public class InventoryManager : IInventory { public void Update(int productId) { Console.WriteLine(string.Format("产品编号为{0}的库存已更新", productId)); } } }
□ 关于确认订单
namespace ConsoleApplication1.Interfaces { public interface IOrderVerify { bool VerifyShippingAddress(int pinCode); } } using System; using ConsoleApplication1.Interfaces; namespace ConsoleApplication1.Implements { public class OrderVerifyManager : IOrderVerify { public bool VerifyShippingAddress(int pinCode) { Console.WriteLine(string.Format("产品可被运输至{0}", pinCode)); return true; } } }
□ 关于打折
namespace ConsoleApplication1.Interfaces { public interface ICosting { float ApplyDiscounts(float originalPrice, float discountPercent); } } using System; using ConsoleApplication1.Interfaces; namespace ConsoleApplication1.Implements { public class CostManager : ICosting { public float ApplyDiscounts(float originalPrice, float discountPercent) { Console.WriteLine(string.Format("产品的原价为:{0},采取的折扣为{1}%", originalPrice, discountPercent)); return originalPrice - ((discountPercent/100)*originalPrice); } } }
□ 关于确认支付和支付
namespace ConsoleApplication1.Interfaces { public interface IPaymentGateway { bool VerifyCardDetails(string cardNo); bool ProcessPayment(string cardNo, float cost); } } using System; using ConsoleApplication1.Interfaces; namespace ConsoleApplication1.Implements { public class PaymentGatewayManager : IPaymentGateway { public bool VerifyCardDetails(string cardNo) { Console.WriteLine(string.Format("卡号为{0}的卡可以被使用",cardNo)); return true; } public bool ProcessPayment(string cardNo, float cost) { Console.WriteLine(string.Format("卡号为{0}的卡支付{0}元",cardNo, cost)); return true; } } }
□ 关于物流
namespace ConsoleApplication1.Interfaces { public interface ILogistics { void ShipProduct(string productName, string shippingAddress); } } using System; using ConsoleApplication1.Interfaces; namespace ConsoleApplication1.Implements { public class LogisticsManager : ILogistics { public void ShipProduct(string productName, string shippingAddress) { Console.WriteLine(string.Format("产品{0}准备发送至{1}", productName, shippingAddress)); } } }
□ 关于OrderDetails
using System; namespace ConsoleApplication1.Model { public class OrderDetails { public int ProductNo { get; set; } public string ProductName { get; set; } public string ProductDescription { get; set; } public float Price { get; set; } public float DiscountPercent { get; set; } public string Address1 { get; set; } public string Addres2 { get; set; } public int PinCode { get; set; } public string CardNo { get; set; } public OrderDetails(string productName, string prodDescription, float price, float discount, string address1, string address2, int pinCode, string cardNo) { this.ProductNo = new Random(1).Next(1, 100); this.ProductName = productName; this.ProductDescription = prodDescription; this.Price = price; this.DiscountPercent = discount; this.Address1 = address1; this.Addres2 = address2; this.PinCode = pinCode; this.CardNo = cardNo; } } }
□ 体现Facade模式的类
using ConsoleApplication1.Implements; using ConsoleApplication1.Interfaces; using ConsoleApplication1.Model; namespace ConsoleApplication1.Services { public class OnlineShoppingFacade { IInventory inventory = new InventoryManager(); IOrderVerify orderVerify = new OrderVerifyManager(); ICosting costManager = new CostManager(); IPaymentGateway paymentGateway = new PaymentGatewayManager(); ILogistics logistics = new LogisticsManager(); public void SubmitOrder(OrderDetails ordeerDetails) { inventory.Update(ordeerDetails.ProductNo); orderVerify.VerifyShippingAddress(ordeerDetails.PinCode); ordeerDetails.Price = costManager.ApplyDiscounts(ordeerDetails.Price, ordeerDetails.DiscountPercent); paymentGateway.VerifyCardDetails(ordeerDetails.CardNo); paymentGateway.ProcessPayment(ordeerDetails.CardNo, ordeerDetails.Price); logistics.ShipProduct(ordeerDetails.ProductName, string.Format("{0},{1} - {2}",ordeerDetails.Address1, ordeerDetails.Addres2,ordeerDetails.PinCode)); } } }
□ 客户端调用
using System; using ConsoleApplication1.Model; using ConsoleApplication1.Services; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { OrderDetails orderDetails = new OrderDetails("产品A", "清凉一夏", 800, 20, "山东省", "青岛市", 1122, "888666999"); OnlineShoppingFacade onlineShopping = new OnlineShoppingFacade(); onlineShopping.SubmitOrder(orderDetails); Console.ReadKey(); } } }
参考资料:
Facade Design Pattern
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。