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


}

posted @ 2013-06-26 19:00  夜雨阑珊  阅读(232)  评论(0编辑  收藏  举报