外观模式的实用性?
1.为一个复杂子系统提供一个简单接口。
2.提高子系统的独立性。
3.在层次化结构中,可以使用Facade模式定义系统中每一层的入口。代码实现
using System;
using UnityEngine;
public class AdpaterTestDemo{
public void testBirdgeMain()
{
Mortgage mo = new Mortgage ();
Customers cus = new Customers ();
if(mo.IsEligible(cus))
{
Debug.Log ("贷款没问题");
}
}
}
public class Mortgage
{
Bank mBank = new Bank();
Credit mCredit = new Credit();
Loan mLoan = new Loan ();
public bool IsEligible(Customers customer)
{
bool isEligible = true;
if(!mBank.HasSufficientSavings(customer))
{
isEligible = false;
}
if(!mCredit.HasSufficientSavings(customer))
{
isEligible = false;
}
if(!mLoan.HasSufficientSavings(customer))
{
isEligible = false;
}
return isEligible;
}
}
/// <summary>
/// 外观模式
/// </summary>
public class Customers
{
private string name;
public string Name
{
set{ name = value; }
get{ return name;}
}
}
public class Bank
{
public bool HasSufficientSavings(Customers c)
{
Debug.Log ("Bank : HasSufficientSavings");
return true;
}
}
public class Credit
{
public bool HasSufficientSavings(Customers c)
{
Debug.Log ("Credit : HasSufficientSavings");
return true;
}
}
public class Loan
{
public bool HasSufficientSavings(Customers c)
{
Debug.Log ("Credit : HasSufficientSavings");
return true;
}
}
facade几个要点
从客户程序的角度来看,Facade模式不仅简化了整个组件系统的接口,同时对于组件内部与外部客户程序来说,从某种程度上也达到了一种“解耦”的效果----内部子系统的任何变化不会影响到Facade接口的变化。
Facade设计模式更注重从架构的层次去看整个系统,而不是单个类的层次。Facdae很多时候更是一种架构
设计模式。
注意区分Facade模式、Adapter模式、Bridge模式与Decorator模式。Facade模式注重简化接口,Adapter模式注重转换接口,Bridge模式注重分离接口(抽象)与其实现,Decorator模式注重稳定接口的前提下为对象扩展功能。
Facade设计模式更注重从架构的层次去看整个系统,而不是单个类的层次。Facdae很多时候更是一种架构
设计模式。
注意区分Facade模式、Adapter模式、Bridge模式与Decorator模式。Facade模式注重简化接口,Adapter模式注重转换接口,Bridge模式注重分离接口(抽象)与其实现,Decorator模式注重稳定接口的前提下为对象扩展功能。