码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2014-05-31 20:03:12      阅读:294      评论:0      收藏:0      [点我收藏+]

标签:des   c   style   class   blog   code   

Facade模式对外提供了统一的接口,而隐藏了内部细节。在网上购物的场景中,当点击提交订单按钮,与此订单相关的库存、订单确认、折扣、确认支付、完成支付、物流配送等都要做相应的动作。本篇尝试使用Facade模式,把这些类似工作者单元的动作隐藏到一类中,只要点击提交订单,余下的事情一步到位:

bubuko.com,布布扣


□ 关于库存

bubuko.com,布布扣
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));
        }
    }
}
bubuko.com,布布扣

 

□ 关于确认订单

bubuko.com,布布扣
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;
        }
    }
}
bubuko.com,布布扣

 

□ 关于打折

bubuko.com,布布扣
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);
        }
    }
}
bubuko.com,布布扣

 

□ 关于确认支付和支付

bubuko.com,布布扣
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;
        }
    }
}
bubuko.com,布布扣


□ 关于物流

bubuko.com,布布扣
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));
        }
    }
}
bubuko.com,布布扣

 

□ 关于OrderDetails

bubuko.com,布布扣
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;
        }
    }
}
bubuko.com,布布扣

 

□ 体现Facade模式的类

bubuko.com,布布扣
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));
        }
    }
}
bubuko.com,布布扣

 

□ 客户端调用

bubuko.com,布布扣
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();
        }
    }
}
bubuko.com,布布扣


参考资料:
Facade Design Pattern

使用Facade模式更新库存、确认订单、采取打折、确认支付、完成支付、物流配送,布布扣,bubuko.com

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

标签:des   c   style   class   blog   code   

原文地址:http://www.cnblogs.com/darrenji/p/3762306.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!