设计模式-模板方法模式

发布时间:2020-07-08 10:26:43 作者:全嗲吉祥
来源:网络 阅读:297
public abstract class Moban
    {
        public string name;
        public Moban(string _name)
        {
            this.name = _name;
        }
        private void showSiteName()
        {
            Console.WriteLine("这是{0}网站",this.name);
        }
        private void createHead()
        {
            Console.WriteLine("这里是页头");
        }
        private void createFoot()
        {
            Console.WriteLine("这里是页脚");
        }
        public abstract void createContent();//内容区

        public void createHtml()
        {
            showSiteName();
            createHead();
            createContent();
            createFoot();
            Console.ReadLine();
        }
    }
    public class SiteA : Moban
    {
        public SiteA(string _name) : base(_name)
        {
        }

        public override void createContent()
        {
            Console.WriteLine("我是{0}网站的内容区:阿里巴巴和腾讯今天合并了",this.name);
        }
    }
    public class SiteB : Moban
    {
        public SiteB(string _name) : base(_name)
        {
        }

        public override void createContent()
        {
            Console.WriteLine("我是{0}网站的内容区:马云和马化腾不得不说的故事", this.name);
        }
    }
        //前端:
        static void Main(string[] args)
        {
            Moban sa = new SiteA("百家论坛");
            Moban sb = new SiteB("天涯论坛");
            sa.createHtml();
            sb.createHtml();
        }

总结:模板方法模式貌似最常用,没什么好说的,用原型模式改造下。

public class MobanPrototype:ICloneable
    {
        public string name { get; set; }
        public MobanPrototype()
        {            
        }
        private void showSiteName()
        {
            Console.WriteLine("这是{0}网站", this.name);
        }
        private void createHead()
        {
            Console.WriteLine("这里是页头");
        }
        private void createFoot()
        {
            Console.WriteLine("这里是页脚");
        }
        private void createContent(string content)//内容区
        {
            Console.WriteLine(content);
        }

        public void createHtml(string content)
        {
            showSiteName();
            createHead();
            createContent(content);
            createFoot();
            Console.ReadLine();
        }

        public object Clone()
        {
            return (MobanPrototype)this.MemberwiseClone();
        }
    }
        //前端
        static void Main(string[] args)
        {
            //Moban sa = new SiteA("百家论坛");
            //Moban sb = new SiteB("天涯论坛");
            //sa.createHtml();
            //sb.createHtml();

            MobanPrototype mobanPrototype = new MobanPrototype();
            MobanPrototype siteA = (MobanPrototype)mobanPrototype.Clone();
            MobanPrototype siteB = (MobanPrototype)mobanPrototype.Clone();

            siteA.name = "百家论坛";            
            siteB.name = "天涯论坛";
            siteA.createHtml(string.Format("我是{0}网站的内容区:阿里巴巴和腾讯今天合并了", siteA.name));
            siteB.createHtml(string.Format("我是{0}网站的内容区:马云和马化腾不得不说的故事", siteB.name));

        }
推荐阅读:
  1. 【设计模式与Android】模板方法模式——照葫芦画瓢
  2. Java描述设计模式(19):模板方法模式

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

设计模式 模板 方法模式

上一篇:云桌面的几大特点!

下一篇:opencv形态学变换的方法

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》