Design Pattern : FlyWeight
換句話說,用一個FlyWeight
Class存放Factory產生的物件。
例如一個DOC檔中有很多重複的Word, 只是它們的字型設定不一樣。 我們可以把這些Word儲放在一個字典,使用的時候再從字典拿出來,另行設定字型即可,避免浪費時間和空間去儲存同樣的Word。
底下的例子,為某家車廠提供了兩台試駕車,各經銷商可自行加上其他配備讓客戶試駕。
1.
試駕車的Interface
public interface ITestCar
{
Color Color { get; set; }
String Name { get; set; }
String Equipment { get; set; }
void Drive();
}
實作兩台試駕車 (另一台FocusTsB省略)
public class FocusTsA : ITestCar
{
public Color Color
{
get{ return Color.Red; }
set { }
}
public string Name
{
get{ return "Focus試駕車A"; }
set { }
}
public string Equipment{ get; set;}
public void Drive()
{
Console.WriteLine("Driving {0} {1} (備註:{2})",
this.Color.ToString(), this.Name, (this.Equipment ?? "無"));
}
}
public interface ITestCar
{
Color Color { get; set; }
String Name { get; set; }
String Equipment { get; set; }
void Drive();
}
實作兩台試駕車 (另一台FocusTsB省略)
public class FocusTsA : ITestCar
{
public Color Color
{
get{ return Color.Red; }
set { }
}
public string Name
{
get{ return "Focus試駕車A"; }
set { }
}
public string Equipment{ get; set;}
public void Drive()
{
Console.WriteLine("Driving {0} {1} (備註:{2})",
this.Color.ToString(), this.Name, (this.Equipment ?? "無"));
}
}
2.
FlyWeight Class
public class TestCarsFlyWeight
{
private List<ITestCar> testCarList;
public TestCarsFlyWeight()
{
testCarList = new List<ITestCar>();
//Create some Object
this.Create();
}
private void Create()
{
ITestCar carA = new FocusTsA();
ITestCar carB = new FocusTsB();
this.testCarList.Add(carA);
this.testCarList.Add(carB);
}
public void Add(ITestCar _tsCar)
{
this.testCarList.Add(_tsCar);
}
public void Remove(ITestCar _tsCar)
{
this.testCarList.Remove(_tsCar);
}
public ITestCar Get(String _name)
{
if(this.testCarList!=null && this.testCarList.Count >0)
{
var car = testCarList.Find(x => x.Name == _name);
if (car != null)
{
return car as ITestCar;
}
else
{
throw new Exception("查無該試駕車輛");
}
}
else
{
throw new Exception("尚未加入任何試駕車輛");
}
}
}
public class TestCarsFlyWeight
{
private List<ITestCar> testCarList;
public TestCarsFlyWeight()
{
testCarList = new List<ITestCar>();
//Create some Object
this.Create();
}
private void Create()
{
ITestCar carA = new FocusTsA();
ITestCar carB = new FocusTsB();
this.testCarList.Add(carA);
this.testCarList.Add(carB);
}
public void Add(ITestCar _tsCar)
{
this.testCarList.Add(_tsCar);
}
public void Remove(ITestCar _tsCar)
{
this.testCarList.Remove(_tsCar);
}
public ITestCar Get(String _name)
{
if(this.testCarList!=null && this.testCarList.Count >0)
{
var car = testCarList.Find(x => x.Name == _name);
if (car != null)
{
return car as ITestCar;
}
else
{
throw new Exception("查無該試駕車輛");
}
}
else
{
throw new Exception("尚未加入任何試駕車輛");
}
}
}
u 我這邊是用一個List<T>存放物件,實際上可用任何容器。
u FlyWeight可由內部建立重用物件,或由外部加入。
3.
主程式
TestCarsFlyWeight testCarFactory = new TestCarsFlyWeight();
///客戶要試駕測試車A
testCarFactory.Get("Focus試駕車A").Drive();
///客戶要試駕測試車B
testCarFactory.Get("Focus試駕車B").Drive();
///客戶要試駕測試車B,且要求的業務要是美女 XD
ITestCar testCar = testCarFactory.Get("Focus試駕車B");
testCar.Equipment = "美女業務陪同";
testCar.Drive();
結果:
Driving Red Focus試駕車A (備註:無)
Driving Blue Focus試駕車B (備註:無)
Driving Blue Focus試駕車B (備註:美女業務陪同)
TestCarsFlyWeight testCarFactory = new TestCarsFlyWeight();
///客戶要試駕測試車A
testCarFactory.Get("Focus試駕車A").Drive();
///客戶要試駕測試車B
testCarFactory.Get("Focus試駕車B").Drive();
///客戶要試駕測試車B,且要求的業務要是美女 XD
ITestCar testCar = testCarFactory.Get("Focus試駕車B");
testCar.Equipment = "美女業務陪同";
testCar.Drive();
結果:
Driving Red Focus試駕車A (備註:無)
Driving Blue Focus試駕車B (備註:無)
Driving Blue Focus試駕車B (備註:美女業務陪同)
4.
結束。
沒有留言:
張貼留言