使用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 ...
随机推荐
- SQL CAST与CONVERT区别
CAST 和 CONVERT 将某种数据类型的表达式显式转换为另一种数据类型.CAST 和 CONVERT 提供相似的功能. 语法 使用 CAST: CAST ( expression AS data ...
- 基于docker 搭建Elasticsearch6.2.4(centos)
一.介绍 ElasticSearch是一个基于Lucene的搜索服务器.它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口.Elasticsearch是用Java开发的,并作为 ...
- Linux下的IPC机制
Linux下的IPC机制 IPC(Inter-Process Communication)是多个进程之间相互沟通的一种方法.在linux下有多种进程间通信的方法. 共享内存 Linux内存共享有多种, ...
- PHP array_key_exists() 函数(判断某个数组中是否存在指定的 key)
定义和用法 array_key_exists() 函数判断某个数组中是否存在指定的 key,如果该 key 存在,则返回 true,否则返回 false. 语法 array_key_exists(ke ...
- 《精通Python设计模式》学习结构型之装饰器模式
这只是实现了Python的装饰器模式. 其实,python的原生的装饰器的应用比这个要强,要广的. ''' known = {0:0, 1:1} def fibonacci(n): assert(n ...
- linux下解除端口占用
1.找出占用端口进程的pid sudo lsof -i:port 2.终止进程 pid
- ASP.NET MVC中在Action获取提交的表单数据方法
有Index视图如下: 视图代码如下: <%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Mas ...
- HTTP 的请求过程?
当点击一个链接时,浏览器首先找到站点的IP地址,这是通过DNS来实现的,在找到IP地址后就可以建立TCP连接了,连接建立后我们就可以发送请求了.但这个请求是什么样子的呢 ? 我们现在假设点击了一个从 ...
- OpenVAS漏洞扫描基础教程之连接OpenVAS服务
OpenVAS漏洞扫描基础教程之连接OpenVAS服务 连接OpenVAS服务 当用户将OpenVAS工具安装并配置完后,用户即可使用不同的客户端连接该服务器.然后,对目标主机实施漏洞扫描.在本教程中 ...
- 配置k8s dns
DNS (domain name system),提供域名解析服务,解决了难于记忆的IP地址问题,以更人性可读可记忆可标识的方式映射对应IP地址. Cluster DNS扩展插件用于支持k8s集群系统 ...