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

使用Facade模式更新库存、确认订单、采取打折、确认支付、完成支付、物流配送的更多相关文章

  1. 转:Ogre源码分析之Root类、Facade模式

    Ogre源码分析(一)Root类,Facade模式 Ogre中的Root对象是一个Ogre应用程序的主入口点.因为它是整个Ogre引擎的外观(Façade)类.通过Root对象来开启和停止Ogre是最 ...

  2. C++设计模式-Facade模式

    Facade模式 作用:为子系统中的一组接口提供一个一致的界面,Facade模式定义了一个高层接口,这个接口使得这一子系统更加容易使用. 动机 将一个系统划分成为若干个子系统有利于降低系统的复杂性.一 ...

  3. 外观模式/facade模式/结构型模式

    外观模式 为子系统中的一组接口提供一个一致的界面, Facade模式定义了一个高层接口,这个接口使得这一子系统更加容易使用. 外观模式三要素(client-facade-subSystem) 外观角色 ...

  4. 设计模式--外观(Facade)模式

    Insus.NET在去年有写过一篇<软件研发公司,外观设计模式(Facade)>http://www.cnblogs.com/insus/archive/2013/02/27/293606 ...

  5. Facade模式

    Facade模式要求一个子系统的外部与其内部的通信必须通过一个统一的Facade对象进行.Facade模式提供一个高层次的接口,使得子系统更易于使用.  就如同医院的接待员一样,Facade模式的Fa ...

  6. Facade模式和Mediator模式

    相同的目的:把某种策略施加到另一组对象上. Facade从上面施加策略. 其使用是明显且受限的.当策略涉及范围广泛并且可见时. 约定的关注点.都同意使用Facade而不是隐藏于其下的对象. Media ...

  7. 设计模式之Facade模式

    Facade(外观)模式为子系统中的各类(或结构与方法)提供一个简明一致的界面,隐藏子系统的复杂性,使子系统更加容易使用.他是为子系统中的一组接口所提供的一个一致的界面. 在遇到以下情况使用Facad ...

  8. Facade 模式

    在软件系统开发中经常回会遇到这样的情况,你实现了一些接口(模块),而这些接口(模块)都分布在几个类中(比如 A和 B.C.D) :A中实现了一些接口,B 中实现一些接口(或者 A代表一个独立模块,B. ...

  9. 【结构型】Facade模式

    外观模式主要意图是为子系统提供一个统一的接口,从而使用用户对子系统的直接依赖中,解耦合出来.Facade主要是通过为子系统统一封装个入口一样,原先用户对子系统的接口.类等都是直接访问,现在要通过Fac ...

随机推荐

  1. Java @SuppressWarnings

    @SuppressWarnings() 注解以@开头可以接受参数 @SuppressWarnings("unchecked") 不受检查的警告信息应该被抑制 //: holding ...

  2. (四)HttpClient 使用代理 IP

    第一节: HttpClient 使用代理 IP 在爬取网页的时候,有的目标站点有反爬虫机制,对于频繁访问站点以及规则性访问站点的行为,会采集屏蔽IP措施. 这时候,代理IP就派上用场了. 关于代理IP ...

  3. MySQL学习笔记:调用存储过程或函数报1418错误

    问题 MySQL开启bin-log后,调用存储过程或者函数以及触发器时,会出现错误号为1418的错误: ERROR 1418 (HY000): This function has none of DE ...

  4. 【直播预告】云栖直播:阿里热修复产品HotFix2.0升级详解

    全面——你知道吗?1891年,卡尔森纳做出的第一把瑞士军刀,只有螺丝刀和开罐器.经过一代又一代能工巧匠的打磨,这把刀陆续增加了锯子.剪刀.镊子.放大镜.改锥,甚至内藏激光.LED手电筒.USB记忆碟等 ...

  5. 2015309南皓芯实验二 Java面向对象程序设计

    一.实验内容 初步掌握单元测试和TDD 理解并掌握面向对象三要素:封装.继承.多态 初步掌握UML建模 熟悉S.O.L.I.D原则 了解设计模式 实验要求 1.没有Linux基础的同学建议先学习< ...

  6. Linux下fastbin利用小结——fd覆盖与任意地址free(House of Spirit)

    linux下的fastbin是ctf中pwn题的重点出题点.去年(2015)中,XCTF就有两站是使用fastbin的利用作为pwn400的压轴题来出现,这也是我刚开始接触fastbin的利用,参考了 ...

  7. SQL 2008 还原SQL 2005备份文件不成功的解决方

    在SQL Server 2008 r2上还原一个SQL Server 2005时,还原不成功,提示如下信息: 按照如下情况则还原成功: -- 查看备份文件的类型 RESTORE FILELISTONL ...

  8. Opencv算法运行时间

    使用getTickCount() 需要导入命名空间cv,using namespace cv; double t = getTickCount(); funciont(); double tm = ( ...

  9. TCP、UDP、HTTP、SOCKET之间的区别与联系

    IP:网络层协议: TCP和UDP:传输层协议: HTTP:应用层协议: SOCKET:TCP/IP网络的API. TCP/IP代表传输控制协议/网际协议,指的是一系列协议. TCP和UDP使用IP协 ...

  10. java中的dao模式

    java中Dao模式   什么是DAO   1.Data Access Object(数据存取对象) 2.位于业务逻辑和持久化数据之间 3.实现对持久化数据的访问 DAO模式的作用 1隔离业务逻辑代码 ...