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. numpy数学计算

    1.求范数 np.linalg.norm norm(x, ord=None, axis=None, keepdims=False)  范数理论的一个小推论告诉我们:ℓ1≥ℓ2≥ℓ∞

  2. css 让背景图片不停旋转

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  3. gitlab-针对API,获取私有令牌

    Gitlab有一个强大的API系统,几乎所有的功能都可以在web中执行,当然也可以通过API来执行,为了使用API,需要从Gitlab中获取私有token. 执行步骤: 1. 登陆Gitlab服务器 ...

  4. JSP的学习二(请求转发与 重定向)

    一: 1.介绍知识点 1). 本质区别: 请求的转发只发出了一次请求, 而重定向则发出了两次请求. 具体: ①. 请求的转发: 地址栏是初次发出请求的地址.  请求的重定向: 地址栏不再是初次发出的请 ...

  5. 安卓手机获取IP地址

    public class IpGetUtil { public static String getIPAddress(Context context) { NetworkInfo info = ((C ...

  6. HDU - 2199 Can you solve this equation? 二分 简单题

    Can you solve this equation? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ( ...

  7. 安恒月赛WP

    一月 一叶飘零大佬的WP:安恒月赛一月 二进制部分:zjgcjy大佬的WP reverse1更容易理解的一种解法 pwn1详解 二月 一叶飘零WP 二进制部分: reverse Pwn 三月 ...

  8. JDK源码分析(三)——HashMap 下(基于JDK8)

    目录 概述 内部字段及构造方法 哈希值与索引计算 存储元素 扩容 删除元素 查找元素 总结 概述   在上文我们基于JDK7分析了HashMap的实现源码,介绍了HashMap的加载因子loadFac ...

  9. sort sign numeric

    //JS001   EXEC PGM=SORT                                 //*                                          ...

  10. android view的 绘制流程

    韩梦飞沙  韩亚飞  313134555@qq.com  yue31313  han_meng_fei_sha 首先是 从  视图根 这个类的  进行遍历 performTraversals 方法 开 ...