Design Patterns in .Net

Post on 30-Nov-2014

1.429 views 3 download

description

 

Transcript of Design Patterns in .Net

System.Activator

public class C {public C() { … }

}

public class C {public static C NewC() {return new C();

}}

private

base() this()

class PersonFactory {public Person MakePerson(int age)

{if (age < 18)return new Minor();

elsereturn new Adult();

}}

string.Format(“<person name=\”{0}\”, name)

XElement(“person”,XAttribute(“name”, name)).ToString();

StringBuilder AppendFormat()AppendLine()

Util.AppendFormatLine(stringBuilder, format, params)

stringBuilder.AppendFormatLine(format, params)

stringBuilder.AppendFormat(“… {0}”, params, Environment.NewLine)

MemberwizeClone

ICloneable

Clone()

ToString() GetHashCode()

[Serializable]

public abstract class IPrototype<T>

{public T DeepCopy()

{

MemoryStream stream = new MemoryStream();

BinaryFormatter formatter = new BinaryFormatter();

formatter.Serialize(stream, this);

stream.Seek(0, SeekOrigin.Begin);

T copy = (T)formatter.Deserialize(stream);

stream.Close();

return copy;

}

}

DeepCopyIPrototype<T>

WeakReference

public class C{ private C() { /* nothing here */ }

class CMaker {static CMaker () { }internal static readonly C instance = new C();

}

public static C Instance {get { return CMaker.instance; }

}}

x:Static

Randomclass RandomGenerator {

public Random GetRandom() {return new Random(); // not what I want

}}

interface IRandom {int RandomNumber();

}

RandomGenerator

IRandom

class RandomAdapter:RandomGenerator, IRandom

{int RandomNumber() {

return GetRandom().Next();}

}

– RandomGenerator rg = new RandomGenerator();Random r = rg.GetRandom();

– IRandom rand = new Adapter();int n = rand.RandomNumber();

– class C { … }

– class CCollection : Collection<C> { … }

class CContainer {private Collection<C> items;

}

class Neuron { … }

class Layer : Collection<Neuron>{ … }

neuron.ConnectTo(otherNeuron);neuron.ConnectTo(layer);layer.ConnectTo(neuron);layer.ConnectTo(otherLayer);

– IEnumerable<T>yield return this;

ICollection<T>

♣ ♠ ♥ ♦

CardInPlayCard

RankSuit

RankSuit

Orientation

IA IB

A B

new

Bitmapbyte[]

XElement.Parse

IEnumerable<T>

yield returnGetEnumerator()

AsyncEnumerator

interface ICarState{void Go(Context ctx); // Drive?

}

class Context {ICarState state;public void GoGoGo() {state.Go(this);state.Go(this); // yeah, right :)state.Go(this);

}}

class CrashedState : ICarState{void Go(Context ctx){ MessageBox.Show(“Are you crazy?”);

}}

class MovingState : ICarState{void Go(Context ctx){// driving// is that a rock?

// CRASH!ctx.state = new CrashedState();

}}

The state just

changed!

interface IStrategy{void Evaluate(Context ctx);

}

SingleEvaluator, ParralelEvaluator, GpuEvaluator

class Context {IStrategy strategy;Matrix m1, m2;static void Multiply(Matrix m1, Matrix m2){ this.m1 = m1; this.m2 = m2;if (strategy == null) { // on-demand

if (gpu.pixelShader >= 2.0)strategy = new GpuStrategy();

else if (Environment.ProcessorCount > 1)strategy = new ParallelStrategy();

else strategy = new SingleStrategy();}strategy.Evaluate(this);

}}

StringBuilder