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;
     }
    }


    }

  • 相关阅读:
    软件建模之UML图形讲解
    Android中级第八讲安卓子线程,以及定时任务使用讲解
    有你同行,我不会寂寞物联网操作系统Hello China后续开发计划及开发者征集
    物联网操作系统再思考Hello China操作系统的运营商网络协同机制
    Windows Phone 7 Storage
    Silverlight &Windows phone7 中使用Isolated Storage存储与读取图片
    Windows Phone7的Pivot控件简介
    windowsphone7的启动器和选择器
    如何将App的图标放到起始页面
    WebBrowser控件用法总结
  • 原文地址:https://www.cnblogs.com/fickleness/p/3157104.html
Copyright © 2011-2022 走看看