Design Pattern : Facade
藉由Facade的,將多個物件封裝在一個介面。
1.
直接看主程式,一目了然:
///原始程式碼
(new Fit()).Drive();
(new Civic()).Drive();
(new Civic()).Drive();
///Facade
using(TestDriveA _testDrive = new TestDriveA())
{
_testDrive.Drive(true);
}
///原始程式碼
(new Fit()).Drive();
(new Civic()).Drive();
(new Civic()).Drive();
///Facade
using(TestDriveA _testDrive = new TestDriveA())
{
_testDrive.Drive(true);
}
u 將原始程式碼的三個物件,封裝在TestDriveA
2.
Facade
public class TestDriveA : IDisposable
{
ICar fit = new Fit();
ICar civic = new Civic();
ICar accord = new Accord();
public void Dispose()
{
fit=null;
civic=null;
accord=null;
}
public void Drive(bool CanTestDrive)
{
if (CanTestDrive)
{
fit.Drive();
civic.Drive();
accord.Drive();
}
else
{
Console.WriteLine("Deny!");
}
}
}
public class TestDriveA : IDisposable
{
ICar fit = new Fit();
ICar civic = new Civic();
ICar accord = new Accord();
public void Dispose()
{
fit=null;
civic=null;
accord=null;
}
public void Drive(bool CanTestDrive)
{
if (CanTestDrive)
{
fit.Drive();
civic.Drive();
accord.Drive();
}
else
{
Console.WriteLine("Deny!");
}
}
}
3.
Reference
Understanding and Implementing Facade Pattern in C#
Understanding and Implementing Facade Pattern in C#
沒有留言:
張貼留言