程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> 關於C語言 >> My AbstractFactory in C#

My AbstractFactory in C#

編輯:關於C語言
//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){}
}
};

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved