?? facaderealworld.cs
字號:
using System;
using System.Windows.Forms;
using System.Text;
//外觀模式(Facade)
//意圖
// 為子系統(tǒng)中的一組接口提供一個一致的界面,F(xiàn)acade模式定義了一個高層接口,這個接口使得這一子系統(tǒng)更加容易使用。
//適用性
//1.當(dāng)你要為一個復(fù)雜子系統(tǒng)提供一個簡單接口時。子系統(tǒng)往往因為不斷演化而變得越來越復(fù)雜。
// 大多數(shù)模式使用時都會產(chǎn)生更多更小的類。這使得子系統(tǒng)更具可重用性,也更容易對子系統(tǒng)進(jìn)行定制,
// 但這也給那些不需要定制子系統(tǒng)的用戶帶來一些使用上的困難。Facade可以提供一個簡單的缺省視圖,
// 這一視圖對大多數(shù)用戶來說已經(jīng)足夠,而那些需要更多的可定制性的用戶可以越過Facade層。
//2.客戶程序與抽象類的實現(xiàn)部分之間存在著很大的依賴性。引入Facade 將這個子系統(tǒng)與客戶以及其他的子系統(tǒng)分離,可以提高子系統(tǒng)的獨立性和可移植性。
//3.當(dāng)你需要構(gòu)建一個層次結(jié)構(gòu)的子系統(tǒng)時,使用Facade模式定義子系統(tǒng)中每層的入口點。
// 如果子系統(tǒng)之間是相互依賴的,你可以讓它們僅通過Facade 進(jìn)行通訊,從而簡化了它們之間的依賴關(guān)系。
namespace DesignPattern.FacadeRealWorld
{
class FacadeRealWorld : AbstractPattern
{
public static void Run(TextBox tbInfo)
{
s_tbInfo = tbInfo;
s_tbInfo.Text = "";
// Facade
Mortgage mortgage = new Mortgage();
// Evaluate mortgage eligibility for customer
Customer customer = new Customer("Ann McKinsey");
bool eligable = mortgage.IsEligible(customer,125000);
DesignPattern.FormMain.OutputInfo("\n" + customer.Name + " has been " + (eligable ? "Approved" : "Rejected"));
// Wait for user
//Console.Read();
}
}
// "Subsystem ClassA"
class Bank
{
public bool HasSufficientSavings(Customer c, int amount)
{
DesignPattern.FormMain.OutputInfo("Check bank for " + c.Name);
return true;
}
}
// "Subsystem ClassB"
class Credit
{
public bool HasGoodCredit(Customer c)
{
DesignPattern.FormMain.OutputInfo("Check credit for " + c.Name);
return true;
}
}
// "Subsystem ClassC"
class Loan
{
public bool HasNoBadLoans(Customer c)
{
DesignPattern.FormMain.OutputInfo("Check loans for " + c.Name);
return true;
}
}
class Customer
{
private string name;
// Constructor
public Customer(string name)
{
this.name = name;
}
// Property
public string Name
{
get
{
return name;
}
}
}
// "Facade"
class Mortgage
{
private Bank bank = new Bank();
private Loan loan = new Loan();
private Credit credit = new Credit();
public bool IsEligible(Customer cust, int amount)
{
DesignPattern.FormMain.OutputInfo("{0} applies for {1:C} loan\n", cust.Name, amount);
bool eligible = true;
// Check creditworthyness of applicant
if (!bank.HasSufficientSavings(cust, amount))
{
eligible = false;
}
else if (!loan.HasNoBadLoans(cust))
{
eligible = false;
}
else if (!credit.HasGoodCredit(cust))
{
eligible = false;
}
return eligible;
}
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -