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

    1.工厂方法模式(Factory Method)

      定义一个用于创建对象的接口,让子类决定实例化哪一个类。工厂方法使一个类的实例化延迟到子类

     

    例子

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace 工厂方法模式
    {
        class Program
        {
            static void Main(string[] args)
            {
                IFartory factory = new UndergraduateFartory();
                LeiFeng student = factory.CreatLeiFeng();
                student.Sweep();
                student.Wash();
                student.BuyRice();
                Console.Read();
            }
        }
        #region- 雷锋类 -
        class LeiFeng
        {
            public void Sweep()
            {
                Console.Write("扫地");
            }
            public void Wash()
            {
                Console.Write("洗衣");
            }
            public void BuyRice()
            {
                Console.Write("买米");
            }
        }
       #endregion
    
        #region- 学雷锋的大学生类 -
        class Undergraduate:LeiFeng
        {
        }
       #endregion
    
        #region- 社区志愿者类 -
        class Volunteer:LeiFeng
        {
        }
       #endregion
    
        #region- 雷锋工厂 -
        interface IFartory
        {
            LeiFeng CreatLeiFeng();
        }
        #endregion
    
        #region- 学雷锋的大学生工厂 -
        class UndergraduateFartory:IFartory
        {
            public LeiFeng CreatLeiFeng()
            {
                return new Undergraduate();
            }
        }
        #endregion
    
        #region- 社区志愿者工厂 -
        class VolunteerFartory:IFartory
        {
            public LeiFeng CreatLeiFeng()
            {
                return new Volunteer();
            }
        }
       #endregion
    }

    运行结果:

  • 相关阅读:
    bzoj1059: [ZJOI2007]矩阵游戏
    NEW
    bzoj2438: [中山市选2011]杀人游戏
    bzoj4554: [Tjoi2016&Heoi2016]游戏 二分图匹配
    【高精度】模板 (C++)
    【BZOJ4025】二分图 LCT
    读入/输出模板
    一些 Markdown 语法
    题解 P3732 [HAOI2017]供给侧改革
    题解 CF1598A Computer Game
  • 原文地址:https://www.cnblogs.com/dong897812629/p/3164163.html
Copyright © 2011-2022 走看看