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

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

using System;
namespace AbstractFactoryPattern {
  //  Abstract Factory        D-J Miller and Judith Bishop Sept 2007
  //  Uses generics to simplify the creation of factories
 
  interface IFactory<Brand>
    where Brand : IBrand {
    IBag CreateBag();
    IShoes CreateShoes();
  }
  // Conctete Factories (both in the same one)
  class Factory<Brand> : IFactory<Brand>
    where Brand : IBrand, new() {
    public IBag CreateBag() {
      return new Bag<Brand>();
    }
    public IShoes CreateShoes() {
      return new Shoes<Brand>();
    }
  }
  // Product 1
  interface IBag {
    string Material { get; }
  }
  // Product 2
  interface IShoes {
    int Price { get; }
  }
  // Concrete Product 1
  class Bag<Brand> : IBag
    where Brand : IBrand, new() {
    private Brand myBrand;
    public Bag() {
      myBrand = new Brand();
    }
    public string Material { get { return myBrand.Material; } }
  }
  // Concrete Product 2
  class Shoes<Brand> : IShoes
    where Brand : IBrand, new() {
      
    private Brand myBrand;
      
    public Shoes() {
      myBrand = new Brand();
    }
    public int Price { get { return myBrand.Price; } }
  }
  interface IBrand {
    int Price { get; }
    string Material { get; }
  }
  class Gucci : IBrand {
    public int Price { get { return 1000; } }
    public string Material { get { return "Crocodile skin"; } }
  }
  class Poochy : IBrand {
    public int Price { get { return new Gucci().Price / 3; } }
    public string Material { get { return "Plastic"; } }
  }
  class Groundcover : IBrand {
    public int Price { get { return 2000; } }
    public string Material { get { return "South african leather"; } }
  }
  class Client<Brand>
    where Brand : IBrand, new() {
    public void ClientMain() { //IFactory<Brand> factory)
      IFactory<Brand> factory = new Factory<Brand>();
      IBag bag = factory.CreateBag();
      IShoes shoes = factory.CreateShoes();
      Console.WriteLine("I bought a Bag which is made from " + bag.Material);
      Console.WriteLine("I bought some shoes which cost " + shoes.Price);
    }
  }
  static class Program {
    static void Main() {
      // Call Client twice
      new Client<Poochy>().ClientMain();
      new Client<Gucci>().ClientMain();
      new Client<Groundcover>().ClientMain();
    }
  }
}
/* Output
I bought a Bag which is made from Plastic
I bought some shoes which cost 333
I bought a Bag which is made from Crocodile skin
I bought some shoes which cost 1000
I bought a Bag which is made from South african leather
I bought some shoes which cost 2000
*/
 

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

你可能感兴趣的文章
C#开发微信门户及应用(5)--用户分组信息管理
查看>>
怎样实现前端裁剪上传图片功能
查看>>
Apriori 关联算法学习
查看>>
MD5加密
查看>>
ant
查看>>
微信,想要说爱你,却没有那么容易!
查看>>
有关sqlite与sql
查看>>
MapXtreme 2005 学习心得 概述(一)
查看>>
php进一法取整、四舍五入取整、忽略小数等的取整数方法大全
查看>>
WSDP
查看>>
Memory Management
查看>>
JQUERY 对 表格中的数据重排序
查看>>
程序员常用借口指南
查看>>
awk 常用方法
查看>>
Android网络框架实现之【Retrofit+RxJava】
查看>>
Android文件的加密与解密
查看>>
【原】记录一句话
查看>>
Android标题栏,状态栏
查看>>
java笔记:SpringSecurity应用(二)
查看>>
php记录代码执行时间
查看>>