快上网专注成都网站设计 成都网站制作 成都网站建设
成都网站建设公司服务热线:028-86922220

网站建设知识

十年网站开发经验 + 多家企业客户 + 靠谱的建站团队

量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决

设计模式-模板方法模式-创新互联

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

        }

创新互联www.cdcxhl.cn,专业提供香港、美国云服务器,动态BGP最优骨干路由自动选择,持续稳定高效的网络助力业务部署。公司持有工信部办法的idc、isp许可证, 机房独有T级流量清洗系统配攻击溯源,准确进行流量调度,确保服务器高可用性。佳节活动现已开启,新人活动云服务器买多久送多久。


本文题目:设计模式-模板方法模式-创新互联
网址分享:http://6mz.cn/article/jidid.html

其他资讯