外觀模式
外觀模式(Facade Pattern)隱藏系統的複雜性,並向客戶端提供了一個客戶端可以訪問系統的接口。這種類型的設計模式屬於結構型模式,它向現有的系統添加一個接口,來隱藏系統的複雜性。
這種模式涉及到一個單一的類,該類提供了客戶端請求的簡化方法和對現有系統類方法的委託調用。
介紹
意圖:爲子系統中的一組接口提供一個一致的界面,外觀模式定義了一個高層接口,這個接口使得這一子系統更加容易使用。
主要解決:降低訪問複雜系統的內部子系統時的複雜度,簡化客戶端與之的接口。
何時使用: 1、客戶端不需要知道系統內部的複雜聯繫,整個系統只需提供一個"接待員"即可。 2、定義系統的入口。
如何解決:客戶端不與系統耦合,外觀類與系統耦合。
關鍵代碼:在客戶端和複雜系統之間再加一層,這一層將調用順序、依賴關係等處理好。
應用實例: 1、去醫院看病,可能要去掛號、門診、劃價、取藥,讓患者或患者家屬覺得很複雜,如果有提供接待人員,只讓接待人員來處理,就很方便。 2、JAVA 的三層開發模式。
優點: 1、減少系統相互依賴。 2、提高靈活性。 3、提高了安全性。
缺點:不符合開閉原則,如果要改東西很麻煩,繼承重寫都不合適。
使用場景: 1、爲複雜的模塊或子系統提供外界訪問的模塊。 2、子系統相對獨立。 3、預防低水平人員帶來的風險。
注意事項:在層次化結構中,可以使用外觀模式定義系統中每一層的入口。
實現
我們將創建一個 Shape 接口和實現了 Shape 接口的實體類。下一步是定義一個外觀類 ShapeMaker。
ShapeMaker 類使用實體類來代表用戶對這些類的調用。FacadePatternDemo,我們的演示類使用 ShapeMaker 類來顯示結果。
步驟一:創建接口
#region 模塊信息
// **********************************************************************
// Copyright (C) 2018 Blazors
// Please contact me if you have any questions
// File Name: Square
// Author: romantic123fly
// WeChat||QQ:
// **********************************************************************
#endregion
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace AppearanceMode
{
public class Square : IShape
{
public void Draw()
{
Debug.Log("Square");
}
}
}
步驟二:創建子類
#region 模塊信息
// **********************************************************************
// Copyright (C) 2018 Blazors
// Please contact me if you have any questions
// File Name: Rectangle
// Author: romantic123fly
// WeChat||QQ:
// **********************************************************************
#endregion
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace AppearanceMode
{
public class Rectangle : IShape
{
public void Draw()
{
Debug.Log("Rectangle");
}
}
}
#region 模塊信息
// **********************************************************************
// Copyright (C) 2018 Blazors
// Please contact me if you have any questions
// File Name: Circle
// Author: romantic123fly
// WeChat||QQ:
// **********************************************************************
#endregion
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace AppearanceMode
{
public class Circle : IShape
{
public void Draw()
{
Debug.Log("Circle");
}
}
}
#region 模塊信息
// **********************************************************************
// Copyright (C) 2018 Blazors
// Please contact me if you have any questions
// File Name: Square
// Author: romantic123fly
// WeChat||QQ:
// **********************************************************************
#endregion
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace AppearanceMode
{
public class Square : IShape
{
public void Draw()
{
Debug.Log("Square");
}
}
}
步驟三:創建外觀類
#region 模塊信息
// **********************************************************************
// Copyright (C) 2018 Blazors
// Please contact me if you have any questions
// File Name: ShapeMaker
// Author: romantic123fly
// WeChat||QQ:
// **********************************************************************
#endregion
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace AppearanceMode
{
public class ShapeMaker
{
private IShape circle;
private IShape rectangle;
private IShape square;
public ShapeMaker()
{
circle = new Circle();
rectangle = new Rectangle();
square = new Square();
}
public void drawCircle()
{
circle.Draw();
}
public void drawRectangle()
{
rectangle.Draw();
}
public void drawSquare()
{
square.Draw();
}
}
}
步驟四:使用外觀類調用各實例
#region 模塊信息
// **********************************************************************
// Copyright (C) 2018 Blazors
// Please contact me if you have any questions
// File Name: Main
// Author: romantic123fly
// WeChat||QQ:
// **********************************************************************
#endregion
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/*介紹
意圖:爲子系統中的一組接口提供一個一致的界面,外觀模式定義了一個高層接口,這個接口使得這一子系統更加容易使用。
主要解決:降低訪問複雜系統的內部子系統時的複雜度,簡化客戶端與之的接口。
何時使用: 1、客戶端不需要知道系統內部的複雜聯繫,整個系統只需提供一個"接待員"即可。 2、定義系統的入口。
如何解決:客戶端不與系統耦合,外觀類與系統耦合。
關鍵代碼:在客戶端和複雜系統之間再加一層,這一層將調用順序、依賴關係等處理好。
優點: 1、減少系統相互依賴。 2、提高靈活性。 3、提高了安全性。
缺點:不符合開閉原則,如果要改東西很麻煩,繼承重寫都不合適。
使用場景: 1、爲複雜的模塊或子系統提供外界訪問的模塊。 2、子系統相對獨立。 3、預防低水平人員帶來的風險。
注意事項:在層次化結構中,可以使用外觀模式定義系統中每一層的入口。
*/
namespace AppearanceMode
{
public class Main : MonoBehaviour
{
// Use this for initialization
void Start()
{
ShapeMaker shapeMaker = new ShapeMaker();
shapeMaker.drawCircle();
shapeMaker.drawRectangle();
shapeMaker.drawSquare();
}
}
}
步驟五:運行結果
源碼鏈接地址:https://download.csdn.net/download/qq_37310110/10287569