zoukankan      html  css  js  c++  java
  • 工厂方法

     1 abstract class LeiFeng
     2     {
     3         public abstract void Sweep();
     4         public abstract void Wash();
     5         public abstract void BuyRice();
     6     }
     7 
     8     class Undergraduate : LeiFeng
     9     {
    10         public override void Sweep()
    11         {
    12             Console.WriteLine("在校大学生,扫地");
    13         }
    14 
    15         public override void Wash()
    16         {
    17             Console.WriteLine("在校大学生,洗衣");
    18         }
    19 
    20         public override void BuyRice()
    21         {
    22             Console.WriteLine("在校大学生,买米");
    23         }
    24     }
    25 
    26     class Volunteer : LeiFeng
    27     {
    28         public override void Sweep()
    29         {
    30             Console.WriteLine("志愿者,扫地");
    31         }
    32 
    33         public override void Wash()
    34         {
    35             Console.WriteLine("志愿者,洗衣");
    36         }
    37 
    38         public override void BuyRice()
    39         {
    40             Console.WriteLine("志愿者,买米");
    41         }
    42     }
    43 
    44     interface IFactory
    45     {
    46         LeiFeng CreateLeiFeng();
    47     }
    48 
    49     class UndergraduateFactory : IFactory
    50     {
    51         public LeiFeng CreateLeiFeng()
    52         {
    53             return new Undergraduate();
    54         }
    55     }
    56 
    57     class VolunteerFactory : IFactory
    58     {
    59         public LeiFeng CreateLeiFeng()
    60         {
    61             return new Volunteer();
    62         }
    63     }
    工厂方法

    转自《大话设计模式》

  • 相关阅读:
    git配置公钥,私钥
    vscode之vue文件代码格式化代码无效解决办法
    [python 并行3]进程
    [spring 并行6]分布式
    [spring 并行5]GPU
    [python 并行2]线程
    [spring 并行4]异步
    [python 并行1]简介
    [flask] flask api + vue 跨域问题
    [spring学习4] MVC
  • 原文地址:https://www.cnblogs.com/yixiu868/p/6557869.html
Copyright © 2011-2022 走看看