KEMBAR78
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();
    else
      return 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;
  }
}
DeepCopy
                 IPrototype<T>



 
 
 WeakReference
new
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
Random
class 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>
Card   CardInPlay

♣ ♠ ♥ ♦

                    Rank
                    Suit
          Rank
          Suit
                 Orientation
IA   IB
 A   B
→
→
new
               Bitmap
      byte[]
Reflection.Emit
XElement.Parse
      →
IEnumerable<T>
   yield return
GetEnumerator()

  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 :            class MovingState :
ICarState                       ICarState
{                               {
  void Go(Context ctx)            void Go(Context ctx)
  {                               {
    MessageBox.Show(                // driving
      “Are you crazy?”);            // is that a rock?
  }
}                                       // CRASH!
                    The state           ctx.state =
                       just               new CrashedState();
                     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


Design Patterns in .Net

  • 8.
  • 9.
    public class C{ public C() { … } } public class C { public static C NewC() { return new C(); } }
  • 10.
    private base() this()
  • 11.
    class PersonFactory { public Person MakePerson( int age) { if (age < 18) return new Minor(); else return new Adult(); } }
  • 12.
    string.Format( “<personname=”{0}”, name) XElement(“person”, XAttribute(“name”, name)) .ToString();
  • 14.
    StringBuilder AppendFormat() AppendLine() Util.AppendFormatLine(stringBuilder, format, params) stringBuilder.AppendFormatLine(format, params) stringBuilder.AppendFormat(“… {0}”, params, Environment.NewLine)
  • 16.
    MemberwizeClone ICloneable Clone() ToString() GetHashCode()
  • 17.
    [Serializable] public abstract classIPrototype<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; } }
  • 18.
    DeepCopy IPrototype<T>   WeakReference
  • 19.
  • 20.
    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; } } }
  • 21.
  • 24.
    Random class RandomGenerator { public Random GetRandom() { return new Random(); // not what I want } } interface IRandom { int RandomNumber(); }
  • 25.
    RandomGenerator IRandom class RandomAdapter: RandomGenerator, IRandom { int RandomNumber() { return GetRandom().Next(); } }
  • 26.
    – RandomGenerator rg= new RandomGenerator(); Random r = rg.GetRandom(); – IRandom rand = new Adapter(); int n = rand.RandomNumber();
  • 28.
    – class C{ … } – class CCollection : Collection<C> { … } class CContainer { private Collection<C> items; }
  • 29.
    class Neuron { …} class Layer : Collection<Neuron> { … } neuron.ConnectTo(otherNeuron); neuron.ConnectTo(layer); layer.ConnectTo(neuron); layer.ConnectTo(otherLayer);
  • 30.
    – IEnumerable<T> yield return this; ICollection<T>
  • 32.
    Card CardInPlay ♣ ♠ ♥ ♦ Rank Suit Rank Suit Orientation
  • 34.
    IA IB A B
  • 36.
  • 38.
    new Bitmap byte[]
  • 39.
  • 44.
  • 45.
    IEnumerable<T> yield return GetEnumerator() AsyncEnumerator
  • 49.
  • 51.
    interface ICarState { void Go(Context ctx); // Drive? }
  • 52.
    class Context { ICarState state; public void GoGoGo() { state.Go(this); state.Go(this); // yeah, right :) state.Go(this); } }
  • 53.
    class CrashedState : class MovingState : ICarState ICarState { { void Go(Context ctx) void Go(Context ctx) { { MessageBox.Show( // driving “Are you crazy?”); // is that a rock? } } // CRASH! The state ctx.state = just new CrashedState(); changed! } }
  • 56.
    interface IStrategy { void Evaluate(Context ctx); } SingleEvaluator, ParralelEvaluator, GpuEvaluator
  • 57.
    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); } }
  • 58.
  • 59.
  • 62.