1. 程式人生 > >C#設計模式-介面卡模式

C#設計模式-介面卡模式

 物件介面卡模式:

using System;
using System.Collections.Generic;

namespace TestCS
{
    class Program
    {
        static void Main(string[] args)
        {
            Triplex2DuplexAdapter adapter = new Triplex2DuplexAdapter();
            adapter.ReceptDuplex();

            Console.ReadKey();
        }
        
        class DuplexOutlet
        {
            public virtual void ReceptDuplex()
            {
                Console.WriteLine("ReceptDuplex");
            }
        }

        class Triplex2DuplexAdaptee
        {
            public void ReceptTriplex()
            {
                Console.WriteLine("ReceptTriplex");
            }
        }

        class Triplex2DuplexAdapter : DuplexOutlet
        {
            Triplex2DuplexAdaptee m_Adaptee = new Triplex2DuplexAdaptee();

            public override void ReceptDuplex()
            {
                m_Adaptee.ReceptTriplex();
            }
        }
    }
}

 

類介面卡模式:

using System;
using System.Collections.Generic;

namespace TestCS
{
    class Program
    {
        static void Main(string[] args)
        {
            Triplex2DuplexAdapter adapter = new Triplex2DuplexAdapter();
            adapter.ReceptDuplex();

            Console.ReadKey();
        }

        interface IDuplexOutlet
        {
            void ReceptDuplex();
        }

        class DuplexOutlet : IDuplexOutlet
        {
            public void ReceptDuplex()
            {
                Console.WriteLine("ReceptDuplex");
            }
        }

        class Triplex2DuplexAdaptee
        {
            public void ReceptTriplex()
            {
                Console.WriteLine("ReceptTriplex");
            }
        }

        class Triplex2DuplexAdapter : Triplex2DuplexAdaptee, IDuplexOutlet
        {
            public void ReceptDuplex()
            {
                ReceptTriplex();
            }
        }
    }
}