public interface IController
{
string GetName();
}
public interface IAggInterface
{
string GetInnerName();
}
public class Aggregate:IAggInterface
{
public string GetInnerName()
{
return "Aggregate";
}
}
public class AggregationPattern:IController,IAggInterface
{
protected IAggInterface aggObj;
public AggregationPattern()
{
aggObj = new Aggregate();
}
public string GetName()
{
return "Controller";
}
public string GetInnerName()
{
return aggObj.GetInnerName();
}
}
class Application
{
[STAThread]
static void Main(string[] args)
{
//执行一个基本操作
IController myController = new AggregationPattern();
Console.WriteLine(myController.GetName());
//执行一个基本操作
IAggInterface myAggregate = (IAggInterface)myController;
Console.WriteLine(myAggregate.GetInnerName());
//执行一个特殊操作,请注意,以下应用便是著名的Aggregation模式
myController = (IController)myAggregate;
if(myController != null)
{
Console.WriteLine(myController.GetName());
Console.WriteLine("c# support aggregation pattern");
}
else
{
Console.WriteLine("c# does not support aggregation pattern");
}
Console.Read();
}
}
//输出结果