Singleton Pattern Demo

image_pdfimage_print
   
 


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

image_pdfimage_print
   
 

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

image_pdfimage_print
   
 

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

image_pdfimage_print
   
 

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

image_pdfimage_print
   
 

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();
    }
}

    


Facade Pattern Demo

image_pdfimage_print
   
 

using System;
public class PizzaDelivery {
    public void GetDeliveryStuff() {
        Console.WriteLine("Pizza Delivery stuff.");
    }

    public PizzaDelivery() {}
}

public class PizzaFinance {
    public void GetPizzaFinanceStuff() {
        Console.WriteLine("Pizza Finance stuff.");
    }

    public PizzaFinance() { }
}

public class PizzaInsurance {
    public void GetPizzaInsuranceStuff() {
        Console.WriteLine("Pizza Insurance stuff.");

    }

    public PizzaInsurance() {}
}

public class PizzaOrder {
    public void GetPizzaOrderStuff() {
        Console.WriteLine("Pizza Order stuff.");
    }

    public PizzaOrder() { }
}

public class PizzaRegistration {

    public void GetPizzaRegistrationStuff() {
        Console.WriteLine("Pizza Registration stuff.");
    }

    public PizzaRegistration() {}
}


public class PizzaCooking {

    public void GetPizzaServiceStuff() {
        Console.WriteLine("Pizza Service stuff.");
    }

    public PizzaCooking() { }
}


public class DealerRepresentative {
    private PizzaDelivery delivery;
    private PizzaFinance finance;
    private PizzaInsurance insurance;
    private PizzaOrder order;
    private PizzaRegistration registration;
    private PizzaCooking service;

    public void GetPizzaUpdate() {
        this.delivery.GetDeliveryStuff();
        this.finance.GetPizzaFinanceStuff();
        this.insurance.GetPizzaInsuranceStuff();
        this.order.GetPizzaOrderStuff();
        this.registration.GetPizzaRegistrationStuff();
        this.service.GetPizzaServiceStuff();
    }

    public DealerRepresentative() {
        delivery = new PizzaDelivery();
        finance = new PizzaFinance();
        insurance = new PizzaInsurance();
        order = new PizzaOrder();
        registration = new PizzaRegistration();
        service = new PizzaCooking();
    }
}


public class Client {
    static void Main(string[] args) {
        DealerRepresentative gopher = new DealerRepresentative();
        gopher.GetPizzaUpdate();
    }
}

    


Composite Pattern Demo

image_pdfimage_print
   
 
using System;
using System.Text;
using System.Collections;

public abstract class Unit {
    protected string name;
    public abstract void Add(Unit e);
    public abstract void Remove(Unit e);
    public abstract void GetChild(int level);

    public Unit(string name) {
        this.name = name;
    }
}


public class Office : Unit {
    public override void Add(Unit c) {
        Console.WriteLine("Can't use 'Add' in Office!");
    }

    public override void Remove(Unit e) {
        Console.WriteLine("Can't use 'Remove' in Office! ");
    }

    public override void GetChild(int level) {
        Console.WriteLine(new string('*', level) + this.name);
    }

    public Office(string name) : base(name) {}
}


public class Branch : Unit {
    private ArrayList node = new ArrayList();

    public override void Add(Unit e) {
        node.Add(e);
    }

    public override void Remove(Unit e) {
        node.Remove(e);
    }

    public override void GetChild(int level) {
        Console.WriteLine(new String('#', level) + this.name);
        foreach (Unit e in this.node)
            e.GetChild(level + 1);

    }

    public Branch(string name) : base(name) {}
}

public class Client {
    static void Main(string[] args) {
        Branch root = new Branch("US (Root)");
        Office ny = new Office("A (Unit)");
        Office ca = new Office("B (Unit)");

        root.Add(ny);
        root.Add(ca);

        Branch rootHawaii = new Branch("Canada Branch (Branch)");
        root.Add(rootHawaii);

        Branch branchUK = new Branch("UK Branch (Branch)");
        Office ldnc = new Office("C Office (Unit)");
        Office ldnw = new Office("D Office (Unit)");
        branchUK.Add(ldnc);
        branchUK.Add(ldnw);
        root.Add(branchUK);

        Office dummy = new Office("D Office");
        ldnc.Add(dummy);

        root.GetChild(0);

        root.Remove(rootHawaii);
        branchUK.Remove(ldnc);
        Console.WriteLine("Remove Hawaii branch and London City office");
        root.GetChild(0);
    }
}