zoukankan      html  css  js  c++  java
  • C#写的工厂模式

    program.cs file

    using System;
    using System.Collections.Generic;
    using System.Text;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                #region 实现上逆
                DVD dvd = new DVD();
                Console.WriteLine(dvd.PlayVideo());
    
                VCD vcd = new VCD();
                Console.WriteLine(vcd.PlayVideo());
                #endregion
                #region 实现多态
                Test();
                #endregion
    
            }
            static void Test()
            {
                VideoShow vs;
                vs = new DVD();
                Play(vs);
    
                vs = new VCD();
                Play(vs);
            }
            static void Play(VideoShow vs)
            {
                string str = vs.PlayVideo();
                Console.WriteLine(str);
            }
        }
        public abstract class VideoShow
        {
            public abstract string PlayVideo();
        }
        public class VCD:VideoShow
        {
            public override string PlayVideo()
            {
                return "我放的是VCD";
            }
        }
        public class DVD:VideoShow
        {
            public override string PlayVideo()
            {
                return "我放的是DVD";
            }
        }
    }

    create.cs file

    using System;
    using System.Collections.Generic;
    using System.Text;
    
    namespace ConsoleApplication1
    {
        class Create
        {
            public static VideoShow factory(string VideoName)
            {
                switch (VideoName.ToUpper())
                {
                    case "DVD":
                        return new DVD();
                    case "VCD":
                        return new VCD();
                }
                return null;
            }
        }
        public class Test
        {
            public static void Main()
            {
                VideoShow vs = Create.factory("DVD");
                vs.PlayVideo();
    
                vs = Create.factory("VCD");
                vs.PlayVideo();
            }
        }
    }

    codefile

    using System;
    using System.Text;
    class Program
    {
        static void Main(string[] args)
        {
            DVD dvd = new DVD();
            Console.WriteLine(dvd.PlayVideo());
            VCD vcd = new VCD();
            Console.WriteLine(vcd.PlayVideo());
    
            TEST();
        }
    
        static void TEST()
        {
            VideoShow vs;
            vs = new DVD();
            Play(vs);
    
            vs = new VCD();
            Play(vs);
        }
        static void Play(VideoShow vs)
        {
            string str = vs.PlayVideo();
            Console.WriteLine(str);
        }
    }
    public abstract class VideoShow
    {
        public abstract string PlayVideo();
    }
    public class VCD : VideoShow
    {
        public override string PlayVideo()
        {
            return "我放的是VCD";
        }
    }
    public class DVD:VideoShow
    {
        public override string PlayVideo()
        {
            return "我放的是DVD";
        }
    }

    接口不可以实例化。但是接口对象可以指向它的实现类对象。

    C#里面比较有特点的东西
    class Example
    {
    private static Example instance;
    private Example() {}
    public static Example Instance//跟java的set和get方法差不多,但是形式那么怪,不是函数,没有参数,但是有函数体
    {
     get
     {
     if(null==instance)
     {
     instance=new Example();

     }
     return instance;
     }
    }


    }

  • 相关阅读:
    排序算法(六)快速排序
    排序算法(五)归并排序
    排序算法(四)堆排序
    排序算法(三)插入排序
    排序算法(二)选择排序
    sizeof strlen 求char*字符串的长度
    剑指offer 按之字型顺序打印二叉树
    剑指offer 对称的二叉树
    剑指offer 二叉树的下一个结点
    剑指offer 删除链表中重复的结点
  • 原文地址:https://www.cnblogs.com/fickleness/p/3157104.html
Copyright © 2011-2022 走看看