设计模式-模板方法模式-成都创新互联网站建设

关于创新互联

多方位宣传企业产品与服务 突出企业形象

公司简介 公司的服务 荣誉资质 新闻动态 联系我们

设计模式-模板方法模式

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));

        }

分享题目:设计模式-模板方法模式
分享路径:http://kswsj.cn/article/pgpghs.html

其他资讯