zoukankan      html  css  js  c++  java
  • 设计模式-工厂模式(一)

    实例:生产罐头工厂

    1.罐头实例类

    public class ThinCan
        {
            public string name { get; set; }
    
            public DateTime factorydate { get; set; }
    
    
            public virtual void Show()
            {
                Console.WriteLine("我是一个" + name + "罐头,我得出厂日期为" + factorydate + "");
            }
        }

    2.苹果罐头

      public  class AppleThinCan:ThinCan
        {
            public override void Show()
            {
                ShowApple();
                base.Show();
            }
    
            public void ShowApple()
            {
                Console.WriteLine("我是AppleThinCan类");
            }
        }

    3.香蕉罐头

    public class BananaThinCan:ThinCan
        {
            public override void Show()
            {
                ShowBanana();
                base.Show();
            }
            public void ShowBanana()
            {
                Console.WriteLine("我是BananaThinCan类");
            }
        }

    4.橘子罐头

     public class OrangeThinCan:ThinCan
        {
            public override void Show()
            {
                ShowOrange();
                base.Show();
            }
            public void ShowOrange()
            {
                Console.WriteLine("我是ShowOrange类");
            }
        }

    5.罐头工厂

      public class ThinCanFactory
        {
            public static ThinCan CreateFactory(string name)
            {
                ThinCan tc = null;
                switch (name)
                {
                    case "Apple": tc=new AppleThinCan(); break;
                    case "Banana":tc=new BananaThinCan();break;
                    case "Orange":tc=new OrangeThinCan();break;
                    default:break;
                }
                return tc;
            }
        }

    6.调用

                ThinCan tc = null;
                tc = ThinCanFactory.CreateFactory("Apple");
                tc.name = "苹果";
                tc.factorydate = DateTime.Now;
                tc.Show();
    萌橙 你瞅啥?
  • 相关阅读:
    杭电1827
    hdu 3118
    poj 2060
    hdu 2236
    poj 2226
    poj 1719
    poj 1466
    poj 3160
    骑士飞行棋笔记
    基础测试学习笔记
  • 原文地址:https://www.cnblogs.com/daimaxuejia/p/12082356.html
Copyright © 2011-2022 走看看