Design Patterns in .Net

1,652 views

Published on

  • Be the first to comment

Design Patterns in .Net

  1. 1. System.Activator
  2. 2. public class C { public C() { … } } public class C { public static C NewC() { return new C(); } }
  3. 3. private base() this()
  4. 4. class PersonFactory { public Person MakePerson( int age) { if (age < 18) return new Minor(); else return new Adult(); } }
  5. 5. string.Format( “<person name=”{0}”, name) XElement(“person”, XAttribute(“name”, name)) .ToString();
  6. 6. StringBuilder AppendFormat() AppendLine() Util.AppendFormatLine(stringBuilder, format, params) stringBuilder.AppendFormatLine(format, params) stringBuilder.AppendFormat(“… {0}”, params, Environment.NewLine)
  7. 7. MemberwizeClone ICloneable Clone() ToString() GetHashCode()
  8. 8. [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; } }
  9. 9. DeepCopy IPrototype<T>   WeakReference
  10. 10. new
  11. 11. 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; } } }
  12. 12. x:Static
  13. 13. Random class RandomGenerator { public Random GetRandom() { return new Random(); // not what I want } } interface IRandom { int RandomNumber(); }
  14. 14. RandomGenerator IRandom class RandomAdapter: RandomGenerator, IRandom { int RandomNumber() { return GetRandom().Next(); } }
  15. 15. – RandomGenerator rg = new RandomGenerator(); Random r = rg.GetRandom(); – IRandom rand = new Adapter(); int n = rand.RandomNumber();
  16. 16. – class C { … } – class CCollection : Collection<C> { … } class CContainer { private Collection<C> items; }
  17. 17. class Neuron { … } class Layer : Collection<Neuron> { … } neuron.ConnectTo(otherNeuron); neuron.ConnectTo(layer); layer.ConnectTo(neuron); layer.ConnectTo(otherLayer);
  18. 18. – IEnumerable<T> yield return this; ICollection<T>
  19. 19. Card CardInPlay ♣ ♠ ♥ ♦ Rank Suit Rank Suit Orientation
  20. 20. IA IB A B
  21. 21. → →
  22. 22. new Bitmap byte[]
  23. 23. Reflection.Emit
  24. 24. XElement.Parse →
  25. 25. IEnumerable<T> yield return GetEnumerator() AsyncEnumerator
  26. 26. → →
  27. 27. interface ICarState { void Go(Context ctx); // Drive? }
  28. 28. class Context { ICarState state; public void GoGoGo() { state.Go(this); state.Go(this); // yeah, right :) state.Go(this); } }
  29. 29. 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! } }
  30. 30. interface IStrategy { void Evaluate(Context ctx); } SingleEvaluator, ParralelEvaluator, GpuEvaluator
  31. 31. 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); } }
  32. 32. → →
  33. 33. StringBuilder
  34. 34.

×