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. Centos7.3安装vsftp服务

    我们需要向centos操作系统的服务器上上传文件或者下载文件,这时候,ftp有必要安装下, 我们选择主流的vsftp: 第一步:安装vsftp yum install -y vsftpd 第二步:设置 ...

  2. SQL CAST与CONVERT区别

    CAST 和 CONVERT 将某种数据类型的表达式显式转换为另一种数据类型.CAST 和 CONVERT 提供相似的功能. 语法 使用 CAST: CAST ( expression AS data ...

  3. kafka基本版与kafka acl版性能对比(单机版)

    一.场景 线上已经有kafka集群,服务运行稳定.但是因为产品升级,需要对kakfa做安全测试,也就是权限验证. 但是增加权限验证,会不会对性能有影响呢?影响大吗?不知道呀! 因此,本文就此来做一下对 ...

  4. How to tell your iPhone application that location services are required | The Agile Warrior

    div{padding-bottom:10px}.b_vPanel>div:last-child{padding:0}.banner a{color:#1020d0} --> Below ...

  5. Visual Studio Code 常用插件整理

    常用插件说明: 一.HTML Snippets 超级使用且初级的H5代码片段以及提示 二.HTML CSS Support  让HTML标签上写class智能提示当前项目所支持的样式 三.Debugg ...

  6. MyBatis-Plus 3.0.7.1

    1 .分页配置 <plugins> <plugin interceptor="com.baomidou.mybatisplus.plugins.PaginationInte ...

  7. 使用ASP.NET MVC+Entity Framework快速搭建系统

    详细资料: http://www.cnblogs.com/dingfangbo/p/5771741.html 学习 ASP.NET MVC 也有一段时间了,打算弄个小程序练练手,做为学习过程中的记录和 ...

  8. jdk1.8下字符串常量的判断,String.intern()分析

    字符串常量池在jdk升级过程中发生了一些变化 在JDK1.6中,它在方法区中,属于“永久代”. 在JDK1.7中,它被移除方法区,放在java堆中. 在JDK1.8中,取消了“永久代”,将常量池放在元 ...

  9. 【基础知识】C#数据库中主键类型的选择

    主键在数据库中占有很大的地位,对于表的关联性,和数据的唯一识别性有重要的作用: 1,在C#开发中,Int自增字段和Guid(数据库中是uniqueidentifier类型)可设置为主键: 1>G ...

  10. 哪种写法更好?<script></script> vs/or <script type=”text/javasript”></script>

    一直很奇怪 哪种写法更好<script type=“text/javascript”>…</script> or <script>…</script>? ...