• 【设计模式】工厂方法(FactoryMethod)模式


    看不见PPT的请自行解决DNS污染问题。

     相关类的代码:

    1 namespace FactoryPatternConsole.Model
    2 {
    3     public class Address
    4     {
    5         public string CountryCode { get; set; }
    6     }
    7 }
    Address
     1 namespace FactoryPatternConsole.Model
     2 {
     3     public class Order
     4     {
     5         public decimal TotalCost { get; set; }
     6         public decimal WeightInKG { get; set; }
     7         public string CourierTrackingId { get; set; }
     8         public Address DispatchAddress { get; set; }
     9     }
    10 }
    Order
    1 namespace FactoryPatternConsole.Model
    2 {
    3     public interface IShippingCourier
    4     {
    5         string GenerateConsignmentLabelFor(Address address);
    6     }
    7 }
    IShippingCourier
     1 namespace FactoryPatternConsole.Model
     2 {
     3     public class DHL : IShippingCourier
     4     {
     5         public string GenerateConsignmentLabelFor(Address address)
     6         {
     7             return "DHL-XXXX-XXXX-XXXX";
     8         }
     9     }
    10 }
    DHL
     1 namespace FactoryPatternConsole.Model
     2 {
     3     public class RoyalMail:IShippingCourier
     4     {
     5         public string GenerateConsignmentLabelFor(Address address)
     6         {
     7             return "RM-XXXX-XXXX-XXXX";
     8         }
     9     }
    10 }
    RoyalMail
     1 using FactoryPatternConsole.Model;
     2 
     3 namespace FactoryPatternConsole.Factory
     4 {
     5     public static class UKShippingCourierFactory
     6     {
     7         public static IShippingCourier CreateShippingCourier(Order order)
     8         {
     9             if (order.TotalCost > 100 || order.WeightInKG > 5)
    10             {
    11                 return new DHL();
    12             }
    13             else
    14             {
    15                 return new RoyalMail();
    16             }
    17         }
    18     }
    19 }
    UKShippingCourierFactory
     1 using FactoryPatternConsole.Factory;
     2 using FactoryPatternConsole.Model;
     3 
     4 namespace FactoryPatternConsole.Service
     5 {
     6     public class OrderService
     7     {
     8         public void Dispatch(Order order)
     9         {
    10             IShippingCourier shippingCourier = UKShippingCourierFactory.CreateShippingCourier(order);
    11 
    12             order.CourierTrackingId = shippingCourier.GenerateConsignmentLabelFor(order.DispatchAddress);
    13         }
    14     }
    15 }
    OrderService

     测试用代码:

     1 using FactoryPatternConsole.Model;
     2 using FactoryPatternConsole.Service;
     3 using System;
     4 
     5 namespace FactoryPatternConsole
     6 {
     7     class Program
     8     {
     9         static void Main(string[] args)
    10         {
    11             OrderService service = new OrderService();
    12             foreach (Order order in GetOrders())
    13             {
    14                 service.Dispatch(order);
    15                 Console.WriteLine(string.Format("TotalCost:{0}, WeightInKG:{1}, CourierTrackingId:{2}",
    16                     order.TotalCost, order.WeightInKG, order.CourierTrackingId));
    17             }
    18             Console.ReadLine();
    19         }
    20 
    21         public static Order[] GetOrders()
    22         {
    23             return new Order[] {
    24                 new Order() 
    25                 { 
    26                     TotalCost = 100,
    27                     WeightInKG = 5,
    28                     DispatchAddress = new Address() { CountryCode = "CN" },
    29                 },
    30                 new Order() 
    31                 { 
    32                     TotalCost = 100.1m,
    33                     WeightInKG = 4,
    34                     DispatchAddress = new Address() { CountryCode = "CN" },
    35                 },
    36                 new Order() 
    37                 { 
    38                     TotalCost = 100,
    39                     WeightInKG = 5.1m,
    40                     DispatchAddress = new Address() { CountryCode = "CN" },
    41                 },
    42             };
    43         }
    44     }
    45 }
    Program

    执行结果:

  • 相关阅读:
    C#线程类Thread初步
    无限级分类存储过程版
    C#多线程编程实例实战
    数据库里阻塞和死锁情况 看那里死锁的存储过程
    预防按钮的多次点击 恶意刷新
    .net2.0文件压缩/解压缩
    HttpHandler和HttpModule入门
    反射,枚举,绑定下拉框
    在C#里关于定时器类
    判断上传的图片文件格式是否合法不是用后缀做的判断
  • 原文地址:https://www.cnblogs.com/Ryukaka/p/4661859.html
Copyright © 2020-2023  润新知