日期:2008-09-23  浏览次数:20419 次

//MyBuilder
using System;
using System.Collections;

///////////////////////product///////////////////////
class HomePage
{
//Fields
private string pagetype;
private string samplecode;
private Hashtable parts=new Hashtable();
//Construcrors
public HomePage(string pagetype)
{
this.pagetype=pagetype;
samplecode="<html><body>parttable,partspan</body></html>";
}
//Indexers
public object this [string key]
{
get{return parts[key];}
set{parts[key]=value;}
}

//Methods
public void PrintHTMLCode()
{
samplecode=samplecode.Replace("parttable",parts["table"].ToString());
samplecode=samplecode.Replace("partspan",parts["span"].ToString());
Console.WriteLine(samplecode);
}

};

///////////////////////product///////////////////////
//////////////////////director////////////////////////
class PageDirector
{
public void Construct(PageBuilder pagebuilder)
{
pagebuilder.BuildFrame();
pagebuilder.BuildTable();
pagebuilder.BuildSpan();
}

};
//////////////////////director////////////////////////
///////////////////////builder///////////////////////
abstract class PageBuilder
{
//Fields
protected HomePage homepage;
//Properties
public HomePage HomePage
{
get{return homepage;}
}
//Methods
abstract public void BuildFrame();
abstract public void BuildTable();
abstract public void BuildSpan();
}

class CoolBuilder : PageBuilder
{
override public void BuildFrame()
{
homepage=new HomePage("coolpage");
}
override public void BuildTable()
{
homepage["table"]="<table color=cool><tr><td><td></tr></table>";
}
override public void BuildSpan()
{
homepage["span"]="<span color=cool></span>";
}
};

class WormBuilder : PageBuilder
{
override public void BuildFrame()
{
homepage=new HomePage("wormpage");
}
override public void BuildTable()
{
homepage["table"]="<table color=worm><tr><td><td></tr></table>";
}
override public void BuildSpan()
{
homepage["span"]="<span color=worm></span>";
}
};

class ClearBuilder : PageBuilder
{
override public void BuildFrame()
{
homepage=new HomePage("clearpage");
}
override public void BuildTable()
{
homepage["table"]="<table color=clear><tr><td><td></tr></table>";
}
override public void BuildSpan()
{
homepage["span"]="<span color=clear></span>";
}
};


///////////////////////builder///////////////////////

//MyBuilder test
class TestApp
{
public static void Main(string[] args)
{
PageBuilder b1=new CoolBuilder();
PageBuilder b2=new WormBuilder();
PageBuilder b3=new ClearBuilder();

PageDirector pagedirector=new PageDirector();

pagedirector.Construct(b1);
pagedirector.Construct(b2);
pagedirector.Construct(b3);

b1.HomePage.PrintHTMLCode();
b2.HomePage.PrintHTMLCode();
b3.HomePage.PrintHTMLCode();

while(true){}

}
};