[設計模式練習]外觀模式

[設計模式練習]外觀模式

行為:食衣住行

用外觀模式提供統一的介面存取行為

類別圖

Facade 

物件檔定義


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

namespace DesignModelTest.外觀
{

    /// <summary>
    /// 演算法一:食
    /// </summary>
    class Food
    {
        public void RequirementFood()
        {
            Console.WriteLine("食");
        }
    }
    /// <summary>
    /// 演算法二:衣
    /// </summary>
    class Cloth
    {
        public void RequirementCloth()
        {
            Console.WriteLine("衣");
        }
    }
    /// <summary>
    /// 演算法三:住
    /// </summary>
    class House
    {
        public void RequirementHouse()
        {
            Console.WriteLine("住");
        }
    }
    /// <summary>
    /// 演算法三:行
    /// </summary>
    class Walk
    {
        public void RequirementWalk()
        {
            Console.WriteLine("行");
        }
    }
    /// <summary>
    /// 外觀模式:管理演算法介面
    /// </summary>
    class FacadeRequirement
    {
        Food food;
        Cloth cloth;
        House house;
        Walk walk;
        public FacadeRequirement()
        {
            food = new Food();
            cloth = new Cloth();
            house = new House();
            walk = new Walk();
        }
        public void GetFood()
        {
            food.RequirementFood();
        }
        public void GetCloth()
        {
            cloth.RequirementCloth();
        }
        public void GetHouse()
        {
            house.RequirementHouse();
        }
        public void GetWalk()
        {
            walk.RequirementWalk();
        }
    }
}

用戶端程式碼


            DesignModelTest.外觀.FacadeRequirement facade = new DesignModelTest.外觀.FacadeRequirement();
            facade.GetFood();
            facade.GetCloth();
            facade.GetHouse();
            facade.GetWalk();
            Console.Read();
            #endregion

輸出結果

pic1