使用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模式更新库存、确认订单、采取打折、确认支付、完成支付、物流配送的更多相关文章
- 转:Ogre源码分析之Root类、Facade模式
Ogre源码分析(一)Root类,Facade模式 Ogre中的Root对象是一个Ogre应用程序的主入口点.因为它是整个Ogre引擎的外观(Façade)类.通过Root对象来开启和停止Ogre是最 ...
- C++设计模式-Facade模式
Facade模式 作用:为子系统中的一组接口提供一个一致的界面,Facade模式定义了一个高层接口,这个接口使得这一子系统更加容易使用. 动机 将一个系统划分成为若干个子系统有利于降低系统的复杂性.一 ...
- 外观模式/facade模式/结构型模式
外观模式 为子系统中的一组接口提供一个一致的界面, Facade模式定义了一个高层接口,这个接口使得这一子系统更加容易使用. 外观模式三要素(client-facade-subSystem) 外观角色 ...
- 设计模式--外观(Facade)模式
Insus.NET在去年有写过一篇<软件研发公司,外观设计模式(Facade)>http://www.cnblogs.com/insus/archive/2013/02/27/293606 ...
- Facade模式
Facade模式要求一个子系统的外部与其内部的通信必须通过一个统一的Facade对象进行.Facade模式提供一个高层次的接口,使得子系统更易于使用. 就如同医院的接待员一样,Facade模式的Fa ...
- Facade模式和Mediator模式
相同的目的:把某种策略施加到另一组对象上. Facade从上面施加策略. 其使用是明显且受限的.当策略涉及范围广泛并且可见时. 约定的关注点.都同意使用Facade而不是隐藏于其下的对象. Media ...
- 设计模式之Facade模式
Facade(外观)模式为子系统中的各类(或结构与方法)提供一个简明一致的界面,隐藏子系统的复杂性,使子系统更加容易使用.他是为子系统中的一组接口所提供的一个一致的界面. 在遇到以下情况使用Facad ...
- Facade 模式
在软件系统开发中经常回会遇到这样的情况,你实现了一些接口(模块),而这些接口(模块)都分布在几个类中(比如 A和 B.C.D) :A中实现了一些接口,B 中实现一些接口(或者 A代表一个独立模块,B. ...
- 【结构型】Facade模式
外观模式主要意图是为子系统提供一个统一的接口,从而使用用户对子系统的直接依赖中,解耦合出来.Facade主要是通过为子系统统一封装个入口一样,原先用户对子系统的接口.类等都是直接访问,现在要通过Fac ...
随机推荐
- HDU 2819 Swap(行列式性质+最大匹配)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2819 题目大意:给你一个n*n的01矩阵,问是否可以通过任意交换整行或者整列使得正对角线上都是1. ...
- kickstart配置LINUX无人值守选项--rootpw
linux kickstart rootpw密码可以使用明文,也可以使用加密过的值(密码为:IPPBXADMINROOT) 注意:在这里要使用加密过的值,否则安全性就太低了 rootpw --iscr ...
- sql server 2005/2008R2 报“红叉”错,即“不允许所请求的注册表访问权”的错误
一.使用报错展示: 1.红叉错: 2.报错文字信息: 解决办法:可以鼠标右键,以管理员的身份运行即可,但这治标不治本,按如下方法可以彻底解决:把“以管理员身份运行此程序”勾上,即可
- 10月15日 | 云栖大会“淘宝移动技术实践&开放论坛”来了!
参会报名链接:http://baichuan.taobao.com/marketing/yunqi#?baichuan_channel=cnblogs 顺应移动互联网消费升级趋势, 淘宝作为移动领域的 ...
- vi/vim基本使用方法(转)
转自:http://www.cnblogs.com/itech/archive/2009/04/17/1438439.html vi/vim 基本使用方法 本文介绍了vi (vim)的基本使用方法,但 ...
- MST最小生成树
首先,贴上一个很好的讲解贴: http://www.wutianqi.com/?p=3012 HDOJ 1233 还是畅通工程 http://acm.hdu.edu.cn/showproblem.ph ...
- abtest分流随机链接方法(javascript)
¶¹¸¯¸ÉËêµÄ·¨¹úµçÊÓ¸²¸Ç --> 代码如下 <!DOCTYPE HTML> <html> <head> <script type=& ...
- ASP.NET:MVC模板化机制
模版化的核心是定制ViewEngine.完整的模板化必须实现3个功能:1.网站的多套模版(razor)支持 2.模版的多样式(style)支持. 3.按需实现模版:没有实现的加载默认模版. 我们除了要 ...
- 【LOJ】#2275. 「JXOI2017」颜色
题解 我们枚举右端点判断合法的左端点有哪些 首先,记录一下右端点右边的点的pre,也就是这个数字前一个出现的位置,取所有小于枚举右端点r的值中最大的一个做为l,用优先队列维护即可,[l + 1,r]就 ...
- [python selenium] 操作方法整理
个人笔记,摘抄自虫师python selenum,仅供个人参考 1.安装: pip install selenium 下载webdriver # webdriver 下载并放置在python主目录 · ...