zoukankan      html  css  js  c++  java
  • factorymethodavocados.cs

      using System;
      using System.Collections;

      class FactoryPattern {
     
      // Factory Method Pattern       Judith Bishop 2006
      //  Example of exporting from different suppliers
        
      interface IProduct {
        string ShipFrom();
      }

      class ProductA : IProduct {
        public String ShipFrom () {
          return " from South Africa";
        }
      }
     
      class ProductB : IProduct {
        public String ShipFrom () {
                return "from Spain";
        }
      }

      class DefaultProduct : IProduct {
        public String ShipFrom () {
                return "not available";
        }
      }

      class Creator {
        public  IProduct FactoryMethod(int month) {
          if (month >= 4 && month <=11)
            return new ProductA();
          else
          if (month == 1 || month == 2 || month == 12)
            return new ProductB();
          else
            return new DefaultProduct();
        }
      }
     
        static void Main() {
          Creator c = new Creator();
          IProduct product;
            
          for (int i=1; i<=12; i++) {
            product = c.FactoryMethod(i);
            Console.WriteLine("Avocados "+product.ShipFrom());
          }
        }
      }
     
    /* Output
    Avocados from Spain
    Avocados from Spain
    Avocados not available
    Avocados  from South Africa
    Avocados  from South Africa
    Avocados  from South Africa
    Avocados  from South Africa
    Avocados  from South Africa
    Avocados  from South Africa
    Avocados  from South Africa
    Avocados  from South Africa
    Avocados from Spain
    */
     

     
  • 相关阅读:
    【codeforces 791D】 Bear and Tree Jumps
    【codeforces 791C】Bear and Different Names
    【codeforces 791B】Bear and Friendship Condition
    【codeforces 791A】Bear and Big Brother
    【t017】YL杯超级篮球赛
    Java Web整合开发(80) -- EJB & WebService
    搜索与排序
    T2821 天使之城 codevs
    T1155 金明的预算方案 codevs
    后缀表达式
  • 原文地址:https://www.cnblogs.com/shihao/p/2500462.html
Copyright © 2011-2022 走看看