博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
builder-bags.cs
阅读量:6247 次
发布时间:2019-06-22

本文共 2738 字,大约阅读时间需要 9 分钟。

using System;
  using System.Diagnostics;
  using System.IO;
  using System.Threading;
  namespace BuilderPattern {
    //  Builder Pattern    D-J Miller and Judith Bishop Sept 2007
    // Abstract Factory : Builder Implementation
    interface IBuilder<Brand>
      where Brand : IBrand {
      IBag CreateBag();
    }
    // Abstract Factory now the Builder
    class Builder<Brand> : IBuilder<Brand>
      where Brand : IBrand, new() {
      Brand myBrand;
      public Builder() {
        myBrand = new Brand();
      }
      public IBag CreateBag() {
        return myBrand.CreateBag();
      }
    }
    // Product 1
    interface IBag {
      string Properties { get; set; }
    }
    // Concrete Product 1
    class Bag :IBag {
      public string Properties { get; set; }
    }
    // Directors
    interface IBrand {
      IBag CreateBag();
    }
    class Gucci : IBrand {
      public IBag CreateBag() {
        Bag b = new Bag();
        Program.DoWork("Cut Leather", 250);
        Program.DoWork("Sew leather", 1000);
        b.Properties += "Leather";
        Program.DoWork("Create Lining", 500);
        Program.DoWork("Attach Lining", 1000);
        b.Properties += " lined";
        Program.DoWork("Add Label", 250);
        b.Properties += " with label";
        return b;
      }
    }
    class Poochy : IBrand {
      public IBag CreateBag() {
        Bag b = new Bag();
        Program.DoWork("Hire cheap labour", 200);
        Program.DoWork("Cut Plastic", 125);
        Program.DoWork("Sew Plastic", 500);
        b.Properties += "Plastic";
        Program.DoWork("Add Label", 100);
        b.Properties += " with label";
        return b;
      }
    }
    class Client<Brand>
      where Brand : IBrand, new() {
        
      public void ClientMain() { //IFactory<Brand> factory)
        IBuilder<Brand> factory = new Builder<Brand>();
        DateTime date = DateTime.Now;
        Console.WriteLine("I want to buy a bag!");
        IBag bag = factory.CreateBag();
        Console.WriteLine("I got my Bag which took " + DateTime.Now.Subtract(date).TotalSeconds * 5 + " days");
        Console.WriteLine("  with the following properties " + bag.Properties+"\n");
      }
    }
    static class Program {
    
      static void Main() {
        // Call Client twice
        new Client<Poochy>().ClientMain();
        new Client<Gucci>().ClientMain();
      }
      public static void DoWork(string workitem, int time) {
        Console.Write("" + workitem + ": 0%");
        Thread.Sleep(time);
        Console.Write("....25%");
        Thread.Sleep(time);
        Console.Write("....50%");
        Thread.Sleep(time);
        Console.WriteLine("....100%");
      }
    }
  }
/*Output
I want to buy a bag!
Hire cheap labour: 0%....25%....50%....100%
Cut Plastic: 0%....25%....50%....100%
Sew Plastic: 0%....25%....50%....100%
Add Label: 0%....25%....50%....100%
I got my Bag which took 14.02016 days
  with the following properties Plastic with label
I want to buy a bag!
Cut Leather: 0%....25%....50%....100%
Sew leather: 0%....25%....50%....100%
Create Lining: 0%....25%....50%....100%
Attach Lining: 0%....25%....50%....100%
Add Label: 0%....25%....50%....100%
I got my Bag which took 45.0648 days
  with the following properties Leather lined with label
*/

转载地址:http://lmria.baihongyu.com/

你可能感兴趣的文章
SQL 中 关于Left Join 转为 Inner Join 的问题
查看>>
java9 响应式编程支持
查看>>
常用的高防有哪几类?主要的区别是什么?
查看>>
数据中心的六大节能方法
查看>>
k8s重要概念
查看>>
AT80C51串口通信编程 | 按键控制LED灯列
查看>>
关于网站建设的主要流程和步骤【新手指南】
查看>>
【译】如何更好的使用javascript数组
查看>>
一日之计在于晨
查看>>
Drupal 曝出代码执行高危漏洞,数百万网站受影响
查看>>
Spring Boot中初始化资源的几种方式
查看>>
Spring Boot几种启动问题的解决方案
查看>>
SpringBlade 2.1 发布,升级为SaaS多租户系统
查看>>
SQL性能优化
查看>>
Go语言基础语法(package)-5
查看>>
Java 程序员必须掌握的 5 个注解!
查看>>
sklearn调包侠之支持向量机
查看>>
源码专题之spring概述
查看>>
CSS box-shadow 详解
查看>>
Openwrt单独编译某一个模块而不是整个固件
查看>>