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

//MyAbstractFactory
using System;

///////////////Basic Products////////////////
//AbstractProducts
abstract class FontsStyle
{
public string stylestring;
};

abstract class TablesStyle
{
public string stylestring;
};
//RealProducts
class FontsStyleA:FontsStyle
{
public FontsStyleA(){stylestring="FontsStyleA";}
};
class FontsStyleB:FontsStyle
{
public FontsStyleB(){stylestring="FontsStyleB";}
};

class TablesStyleA:TablesStyle
{
public TablesStyleA(){stylestring="TablesStyleA";}
};
class TablesStyleB:TablesStyle
{
public TablesStyleB(){stylestring="TablesStyleB";}
};

//////////////Basic Products////////////////

//////////////Style Factorys////////////////

abstract class StyleFactory
{
abstract public FontsStyle createFontsStyle();
abstract public TablesStyle createTablesStyle();
};

class StyleA:StyleFactory
{
override public FontsStyle createFontsStyle()
{
return new FontsStyleA();
}
override public TablesStyle createTablesStyle()
{
return new TablesStyleA();
}
};

class StyleB:StyleFactory
{
override public FontsStyle createFontsStyle()
{
return new FontsStyleB();
}
override public TablesStyle createTablesStyle()
{
return new TablesStyleB();
}
};
//////////////Style Factorys////////////////

//////////////////////HomePage is the product as last
class HomePage
{
private FontsStyle fontsstyle;
private TablesStyle tablesstyle;

private string htmlcode="<html><body><table style='tablesstyle'><tr><td><font style='fontsstyle'>HelloWorld!</font></td></tr></table></body></html>";

public HomePage(StyleFactory stylefactory)
{
fontsstyle=stylefactory.createFontsStyle();
tablesstyle=stylefactory.createTablesStyle();
htmlcode=htmlcode.Replace("fontsstyle",fontsstyle.stylestring);
htmlcode=htmlcode.Replace("tablesstyle",tablesstyle.stylestring);
}

public void PrintHTMLCode()
{
Console.WriteLine(htmlcode);
}
public void SetStyle(string filename)
{
}

};

//MyAbstractFactory App
class TestApp
{
public static void Main( string[] args )
{
StyleFactory stylea=new StyleA();
HomePage samplepage=new HomePage(stylea);
samplepage.PrintHTMLCode();

StyleFactory styleb=new StyleB();
samplepage=new HomePage(styleb);
samplepage.PrintHTMLCode();

while(true){}
}
};