您好,登錄后才能下訂單哦!
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));
}
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。