依赖倒置原则

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Unitys
{
    //定义一个统一接口用于依赖
    public interface IDevice
    {
        void Login();
        bool Spider();
    }

    //MML类型的设备
    public class DeviceMML : IDevice
    {
        public void Login()
        {
            Console.WriteLine("MML设备登录");
        }

        public bool Spider()
        {
            Console.WriteLine("MML设备采集");
            return true;
        }
    }

    //TL2类型设备
    public class DeviceTL2 : IDevice
    {
        public void Login()
        {
            Console.WriteLine("TL2设备登录");
        }

        public bool Spider()
        {
            Console.WriteLine("TL2设备采集");
            return true;
        }
    }

    //TELNET类型设备
    public class DeviceTELNET : IDevice
    {
        public void Login()
        {
            Console.WriteLine("TELNET设备登录");
        }

        public bool Spider()
        {
            Console.WriteLine("TELNET设备采集");
            return true;
        }
    }

    //TL5类型设备
    public class DeviceTL5 : IDevice
    {
        public void Login()
        {
            Console.WriteLine("TL5设备登录");
        }

        public bool Spider()
        {
            Console.WriteLine("TL5设备采集");
            return true;
        }
    }


    //设备采集的服务
    public class DeviceService
    {
        private IDevice m_device;
        public DeviceService(IDevice oDevice)
        {
            m_device = oDevice;
        }

        public void LoginDevice()
        {
            m_device.Login();
        }

        public bool DeviceSpider()
        {
            return m_device.Spider();
        }
    }








    class Program
    {
        static void Main(string[] args)
        {
            var oSpider = new DeviceService(new DeviceTL5());

            oSpider.LoginDevice();
            var bRes = oSpider.DeviceSpider();

            Console.ReadKey();
        }
    }
}

 

posted @ 2017-12-11 21:56  fishyue  阅读(168)  评论(0编辑  收藏  举报