using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
public partial class Design_Pattern_Command : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Controller controller = new Controller();
Hello hello = new Hello();
HelloCommand helloCommand = new HelloCommand(hello);
controller.SetCommand(0, helloCommand);
Bye bye = new Bye();
ByeCommand byeCommand = new ByeCommand(bye);
controller.SetCommand(1, byeCommand);
Good good = new Good();
GoodCommand goodCommand = new GoodCommand(good);
controller.SetCommand(2, goodCommand);
controller.OnPush(2);
controller.OnPush(0);
controller.OnPush(1);
}
}
public class Hello
{
public void SayHello()
{
System.Web.HttpContext.Current.Response.Write("Hello!");
}
}
public class Bye
{
public void SayBye()
{
System.Web.HttpContext.Current.Response.Write("Bye!");
}
}
public class Good
{
public void SayGood()
{
System.Web.HttpContext.Current.Response.Write("Good!");
}
}
public interface ICommand
{
void Execute();
}
public class HelloCommand : ICommand
{
private Hello _hello;
public HelloCommand(Hello hello)
{
this._hello = hello;
}
public void Execute()
{
this._hello.SayHello();
}
}
public class ByeCommand : ICommand
{
private Bye _bye;
public ByeCommand(Bye bye)
{
this._bye = bye;
}
public void Execute()
{
this._bye.SayBye();
}
}
public class GoodCommand : ICommand
{
private Good _good;
public GoodCommand(Good good)
{
this._good = good;
}
public void Execute()
{
this._good.SayGood();
}
}
public class Controller
{
ICommand[] commands = new ICommand[3];
public void SetCommand(int slot, ICommand command)
{
commands[slot] = command;
}
public void OnPush(int slot)
{
commands[slot].Execute();
}
}
根据《Head Of Design Pattern》里面的Java代码改写的Decorator模式
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
public partial class Design_Pattern_Decorator : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Beverage beverage1 = new Espresso();
beverage1 = new Mocha(beverage1);
beverage1 = new Sugar(beverage1);
Response.Write(beverage1.cost());
Response.Write(beverage1.GetDescription());
}
}
public abstract class Beverage
{
protected string description = "Unknown Beverage";
public virtual string GetDescription()
{
return description;
}
public abstract double cost();
}
public class Espresso : Beverage
{
public Espresso()
{
this.description = "Espresso";
}
public override double cost()
{
return 1.99;
}
}
public class HouseBlend : Beverage
{
public HouseBlend()
{
this.description = "House Blend";
}
public override double cost()
{
return 0.89;
}
}
public abstract class CondimentDecorator : Beverage
{
public override abstract string GetDescription();
}
public class Mocha : CondimentDecorator
{
Beverage beverage;
public Mocha(Beverage beverage)
{
this.beverage = beverage;
}
public override string GetDescription()
{
this.description = beverage.GetDescription() + ", Mocha";
return this.description;
}
public override double cost()
{
return beverage.cost() + 0.2;
}
}
public class Sugar : CondimentDecorator
{
Beverage beverage;
public Sugar(Beverage beverage)
{
this.beverage = beverage;
}
public override string GetDescription()
{
return beverage.GetDescription() + ", Sugar";
}
public override double cost()
{
return beverage.cost() + 0.12;
}
}
今天继续百无聊赖。
许多公司在招人的时候都会要求写一个singleton,我觉得这个本身就蛮可笑的。这个有啥写的,不会的人看5分钟都背出来了。

一般这么写会让你通过。
public sealed class Singleton
{
static Singleton instance=null;
Singleton()
{
}
public static Singleton Instance
{
get
{
if (instance==null)
{
instance = new Singleton();
}
return instance;
}
}
}
但是多线程的时候这个会有多个实例出来的。
所以比较好的还是用下面这个,我把test page和class包括了,还有点注释。我喜欢用page而不是console做test,呵呵。
参考了:
http://www.aspcool.com/lanmu/browse1.asp?ID=1138&bbsuser=csharp
这个作者还有另外一种实现,但是我觉得lazy instantiation还要客户端去单独写代码保证线程同步是有问题的。最好还是在singlton类内部做。
using System;
using System.Threading;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
public partial class Design_Pattern_Singleton : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Thread thread0 = Thread.CurrentThread;
thread0.Name = "Thread 0";
Thread thread1 = new Thread(new ThreadStart(this.DoSomeWork));
thread1.Name = "Thread 1";
Thread thread2 = new Thread(new ThreadStart(this.DoSomeWork));
thread2.Name = "Thread 2";
Thread thread3 = new Thread(new ThreadStart(this.DoSomeWork));
thread3.Name = "Thread 3";
thread1.Start();
thread2.Start();
thread3.Start();
DoSomeWork(); // thread0 do the same work
}
public void DoSomeWork()
{
MySingleton mySingleton = MySingleton.GetInstance();
for (int i = 1; i < 5; i++)
{
mySingleton.Add();
Response.Write("Thread" + Thread.CurrentThread.Name.ToString() + "report: the current counter number is " + mySingleton.GetNumber().ToString() + "<br>");
}
}
}
public class MySingleton
{
private MySingleton() { }//it is must for a singleton
private int totalNum = 0;//for test in test page.
private static MySingleton mySingleton = new MySingleton();//that means early instantiation, it's safe for multi-thread
//client get the instance by this method
public static MySingleton GetInstance()
{
return mySingleton;
}
// add the value by 1
public void Add()
{
totalNum++;
}
//get the count number, for test in test page.
public int GetNumber()
{
return totalNum;
}
}

照着UML图,随便写了个Abstract Factory模式

Load assembly的时候有点不一样,因为那个目录很怪很长,是个临时目录,所以我用location得到了它。很奇怪阿。怎么放那里,看来vs2008是不一样阿。
string location = Assembly.GetAssembly(Type.GetType(factoryName)).Location;
factory = (IFactory)Assembly.LoadFile(location).CreateInstance(factoryName);
代码没啥说的,就是连连手而已。小时候看图说话,现在看图写代码,哈哈。
using System;
using System.Collections;
using System.Configuration;
using System.Reflection;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
public partial class FactoryMethod : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string factoryName = ConfigurationManager.AppSettings.Get("factoryName");
string location = Assembly.GetAssembly(Type.GetType(factoryName)).Location;
IFactory factory;
factory = (IFactory)Assembly.LoadFile(location).CreateInstance(factoryName);
Response.Write(factory.CreateProduct1());
Response.Write(factory.CreateProduct2());
}
}
//interface for productA
public interface IProduct1
{
string GetProductName();
}
//interface for productB
public interface IProduct2
{
string GetProductCode();
}
public class ProductA1 : IProduct1
{
public string GetProductName()
{
return "ProductA1";
}
}
public class ProductA2 : IProduct2
{
public string GetProductCode()
{
return "ProductA2";
}
}
public class ProductB1 : IProduct1
{
public string GetProductName()
{
return "ProductB1";
}
}
public class ProductB2 : IProduct2
{
public string GetProductCode()
{
return "ProductB2";
}
}
interface IFactory
{
IProduct1 CreateProduct1();
IProduct2 CreateProduct2();
}
public class FactoryA : IFactory
{
public IProduct1 CreateProduct1()
{
return new ProductA1();
}
public IProduct2 CreateProduct2()
{
return new ProductA2();
}
}
public class FactoryB : IFactory
{
public IProduct1 CreateProduct1()
{
return new ProductB1();
}
public IProduct2 CreateProduct2()
{
return new ProductB2();
}
}