首页 > 开发 > 综合 > 正文

My Builder in C#

2024-07-21 02:19:39
字体:
来源:转载
供稿:网友
//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){}

}
};



上一篇:c#范型体验1

下一篇:My AbstractFactory in C#

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表