Template Method Pattern

   
 

using System;
public abstract class Calculate {
    public abstract void PositionLabel();

    public void SetupCalculate() {
        this.PositionLabel();
    }

    public Calculate() {}
}

public class CalculateDeluxe : Calculate {

    public override void PositionLabel() {
        Console.WriteLine("Deluxe clock");
    }
    public CalculateDeluxe() { ;}
}

public class CalculateStandard : Calculate {
    public override void PositionLabel() {}

    public CalculateStandard() {}
}
class Client {
    static void Main(string[] args) {
        Calculate delCalculate = new CalculateDeluxe();
        delCalculate.SetupCalculate();
        Calculate stdCalculate = new CalculateStandard();
        stdCalculate.SetupCalculate();
    }
}

    


Strategy Pattern Demo

   
 

using System;
public abstract class Strategy {
    public abstract void CreditBusinessRule();
    public Strategy() { ;}
}

public class AStrategy : Strategy {
    public override void CreditBusinessRule() {
        Console.WriteLine("A");
    }

    public AStrategy() { ;}
}

public class BStrategy : Strategy {
    public override void CreditBusinessRule() {
        Console.WriteLine("Manager");
    }

    public BStrategy() { }
}

public class MyContext {
    Strategy aStrategy;

    public MyContext(Strategy crStrategy) {
        this.aStrategy = crStrategy;
    }

    public void BusinessRule() {
        this.aStrategy.CreditBusinessRule();
    }
}

class Client {
    static void Main(string[] args) {
        MyContext cr = new MyContext(new AStrategy());
        cr.BusinessRule();
    }
}

    


Singleton Pattern Demo

   
 


using System;

sealed class MyClass {
    private static bool instanceFlag = false;
    public static MyClass GetMyClass() {
        if (!instanceFlag) {
            instanceFlag = true;
            return new MyClass();
        } else {
            throw new Exception("An engine has already been created!");
        }
    }

    private MyClass() {
        Console.WriteLine("An MyClass");
    }
}


class Client {

    static void Main(string[] args) {
        Console.WriteLine("Attempting to get first engine");
        try {
            MyClass eng = MyClass.GetMyClass();
        } catch (Exception e) {
            Console.WriteLine(e.Message);
        }

        Console.WriteLine("Attempting to get second engine");
        try {
            MyClass eng = MyClass.GetMyClass();

        } catch (Exception e) {
            Console.WriteLine(e.Message);
        }

    }
}

    


Pattern Example: Remote-Proxy

   
 

using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;

public interface IEmployee {
    string Design();
    string StressTest();
    string Mechanical();
    string Performance();
}

public class ChiefDesignEngineer : MarshalByRefObject, IEmployee {
    public string Design() {
        return "Design ";
    }

    public string StressTest() {
        return "stress";
    }

    public string Mechanical() {
        return "mechanical";
    }

    public string Performance() {
        return "performance";
    }

    public ChiefDesignEngineer() { ;}
}

public class ProxyChiefDesignEngineer : MarshalByRefObject, IEmployee {

    private ChiefDesignEngineer chief;

    public string Design() {
        return this.chief.Design();
    }

    public string StressTest() {
        return this.chief.StressTest();
    }

    public string Mechanical() {
        return "A supplement to mechanical";
    }

    public string Performance() {
        return "Performance information is in the manual.";
    }

    public ProxyChiefDesignEngineer() {
        this.chief = new ChiefDesignEngineer();
    }

}


public class Server {
    static void Main(string[] args) {
        TcpServerChannel channel = new TcpServerChannel(1234);
        ChannelServices.RegisterChannel(channel);

        RemotingConfiguration.RegisterWellKnownServiceType(typeof(ProxyChiefDesignEngineer), "RemoteProxy", WellKnownObjectMode.SingleCall);
        Console.WriteLine("Press [Enter] to terminate server...");
        Console.ReadLine();
    }
}

public class Client {
    static void Main(string[] args) {
        TcpClientChannel channel = new TcpClientChannel();
        ChannelServices.RegisterChannel(channel);

        RemotingConfiguration.RegisterWellKnownClientType
        (typeof(ProxyChiefDesignEngineer), "tcp://localhost:1234/RemoteProxy");
        ProxyChiefDesignEngineer ukProxyChief = new ProxyChiefDesignEngineer();

        Console.WriteLine("Answer: " + ukProxyChief.Performance());
        Console.WriteLine("Answer: " + ukProxyChief.Design());
    }
}

    


Proxy Pattern Demo

   
 

using System;

public interface IEmployee {
    string Design();
    string StressTest();
    string Mechanical();
    string Performance();
}

public class ChiefDesignEngineer : IEmployee {

    public string Design() {
        return "Design ";

    }

    public string StressTest() {
        return "Stress Test";
    }

    public string Mechanical() {
        return "mechanical";
    }

    public string Performance() {
        return "performance";
    }

    public ChiefDesignEngineer() { ;}
}


public class ProxyChiefDesignEngineer : IEmployee {
    private ChiefDesignEngineer chief;

    public string Design() {
        return this.chief.Design();
    }

    public string StressTest() {
        return this.chief.StressTest();
    }

    public string Mechanical() {
        return "supplement to mechanical.";
    }
    public string Performance() {
        return "Performance for Chief Design Engineer";
    }
    public ProxyChiefDesignEngineer() {
        this.chief = new ChiefDesignEngineer();
    }

}

public class Client {

    static void Main(string[] args) {
        ProxyChiefDesignEngineer caProxyChief = new ProxyChiefDesignEngineer();

        Console.WriteLine(caProxyChief.StressTest());

        Console.WriteLine(caProxyChief.Performance());

        ProxyChiefDesignEngineer nyProxyChief = new ProxyChiefDesignEngineer();

        Console.WriteLine(nyProxyChief.Mechanical());

    }
}

    


Observer Pattern Demo

   
 

using System;
public delegate void ChangedEventHandler(object sender, EventArgs e);

public class Subject {

    private string data;
    public event ChangedEventHandler Changed;

    public string Data {
        get { return data; }
        set {
            data = value;
            this.OnChanged(EventArgs.Empty);
        }
    }

    protected virtual void OnChanged(EventArgs e) {
        if (Changed != null)
            this.Changed(this, e);
    }

    public Subject() { }
}


public class Observer {
    private Subject data;
    private string observerName;

    public Subject ObservedObject {
        get { return data; }
        set { data = value; }
    }

    private void DataChanged(object sender, EventArgs e) {
        Console.WriteLine("Notification to {0}, {1}", observerName, data.Data);
    }

    public void Attach() {
        data.Changed += new ChangedEventHandler(DataChanged);
    }

    public void Detach() {
        data.Changed -= new ChangedEventHandler(DataChanged);
    }

    public Observer(string name) {
        observerName = name;
    }
}

class Client {
    static void Main(string[] args) {
        Subject sub = new Subject();
        Observer observerA = new Observer("A Observer");
        Observer observerB = new Observer("B Observer");
        observerA.ObservedObject = sub;
        observerB.ObservedObject = sub;
        observerA.Attach();
        observerB.Attach();
        sub.Data = "1";
        observerA.Detach();
        sub.Data = "2";
    }
}

    


Factory Method Pattern

   
 

using System;
using System.Collections;

public abstract class Module {
    public abstract void SomeModule();
}

public class FeaturesModule : Module {

    public override void SomeModule() {
        Console.WriteLine("Technical content.");
    }

    public FeaturesModule() {}
}

public class InstructionModule : Module {

    public override void SomeModule() {
        Console.WriteLine("Instruction content.");
    }

    public InstructionModule() {}

}

public class PictureModule : Module {

    public override void SomeModule() {
        Console.WriteLine("Picture content.");
    }

    public PictureModule() {}

}


public class TechnicalModule : Module {

    public override void SomeModule() {
        Console.WriteLine("Technical content.");
    }

    public TechnicalModule() {}

}


public abstract class Page {
    protected ArrayList pageCompositor = new ArrayList();
    public abstract void AddModule();
    public abstract void DisplayPage();
}


public class CatalogPage : Page {
    public override void AddModule() {
        this.pageCompositor.Clear();
        this.pageCompositor.Add(new FeaturesModule());
        this.pageCompositor.Add(new PictureModule());
    }
    public override void DisplayPage() {
        foreach (Module c in this.pageCompositor)
            c.SomeModule();
    }


    public CatalogPage() {
        this.AddModule();
    }
}


public class ManualPage : Page {
    public override void AddModule() {
        this.pageCompositor.Clear();
        this.pageCompositor.Add(new TechnicalModule());
        this.pageCompositor.Add(new PictureModule());
        this.pageCompositor.Add(new InstructionModule());
    }
    public override void DisplayPage() {
        Console.WriteLine("Manual page contains:");
        foreach (Module c in this.pageCompositor)
            c.SomeModule();
        Console.WriteLine();
    }
    public ManualPage() { }
}
class Client {
    static void Main(string[] args) {
        Page p = new CatalogPage();
        p.AddModule();
        p.DisplayPage();
        p = new ManualPage();
        p.AddModule();
        p.DisplayPage();
    }
}