日期:2014-05-18 浏览次数:20752 次
class Program { static void Main(string[] args) { IPager pager = PagerFactory.GetPager(); pager.Show(); } } /// <summary> /// 工厂类 /// </summary> public class PagerFactory { public static IPager GetPager() { IPager pager; pager = new MainPager(); pager = new FootPager1(pager); pager = new HeadPager2(pager); pager = new HeadPager1(pager); return pager; } } /// <summary> /// 页面模块接口 /// </summary> public interface IPager { void Show(); } /// <summary> /// 页面主要内容 /// </summary> public class MainPager : IPager { public void Show() { Console.WriteLine("主页面内容"); } } /// <summary> /// 页面模块Decorator基类 /// </summary> public class PageDecorator : IPager { public IPager Pager; public PageDecorator(IPager pager) { this.Pager = pager; } public void ShowPager() { if (this.Pager != null) { this.Pager.Show(); } } public virtual void Show(){ } } public class HeadPager1 : PageDecorator { public HeadPager1(IPager pager) : base(pager) { } public override void Show() { Console.WriteLine("头部一内容"); base.ShowPager(); } } public class HeadPager2 : PageDecorator { public HeadPager2(IPager pager) : base(pager) { } public override void Show() { Console.WriteLine("头部二内容"); base.ShowPager(); } } public class FootPager1 : PageDecorator { public FootPager1(IPager pager) : base(pager) { } public override void Show() { base.ShowPager(); Console.WriteLine("尾部一内容"); } }