Design Patterns

  • Uploaded by: Dmitri Nesteruk
  • 0
  • 0
  • May 2020
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Design Patterns as PDF for free.

More details

  • Words: 425
  • Pages: 62
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( “
XElement(“person”, XAttribute(“name”, name)) .ToString();

StringBuilder AppendLine()

AppendFormat()

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 { 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

  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 { … } class CContainer { private Collection items; }

class Neuron { … } class Layer : Collection { … } neuron.ConnectTo(otherNeuron); neuron.ConnectTo(layer); layer.ConnectTo(neuron); layer.ConnectTo(otherLayer);

– IEnumerable yield return this; ICollection

Card

CardInPlay

♣ ♠ ♥ ♦

Rank Suit

Rank Suit Orientation

IA A

IB B

→ →

new

Bitmap byte[]

Reflection.Emit

XElement.Parse →

IEnumerable 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 : 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


Related Documents

Design Patterns
June 2020 16
Design Patterns
May 2020 17
Design Patterns
May 2020 17
Design Patterns
November 2019 29
Design Patterns
April 2020 21
Design Patterns
June 2020 5

More Documents from ""