Thursday, May 10, 2012

Simple Observer Pattern


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace Observer
{
    class Program
    {
        static void Main(string[] args)
        {
            // Configure Observer pattern
            Subject s = new Subject();

            s.Attach(new Observer(s, "X"));
            s.Attach(new Observer(s, "Y"));
            s.Attach(new Observer(s, "Z"));

            // Change subject and notify observers
            s.SubjectState = "ABC";
            s.Notify();

            // Wait for user
            Console.Read();
        }
    }

    // "Subject"
    class Subject
    {
        public string SubjectState;

        private ArrayList observers = new ArrayList();

        public void Attach(Observer observer)
        {
            observers.Add(observer);
        }

        public void Detach(Observer observer)
        {
            observers.Remove(observer);
        }

        public void Notify()
        {
            foreach (Observer o in observers)
            {
                o.Update();
            }
        }
    }


    // "Observer"
    class Observer
    {
        private string name;
        private string observerState;
        private Subject subject;

        // Constructor
        public Observer(Subject subject, string name)
        {
            this.subject = subject;
            this.name = name;
        }

        public void Update()
        {
            observerState = subject.SubjectState;
            Console.WriteLine("Observer {0}'s new state is {1}", name, observerState);
        }
    }
}

Wednesday, May 9, 2012

Simple Builder Design Pattern


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Builder
{
    class Program
    {
        static void Main(string[] args)
        {
            Pizza pizza = new Builder(12).Cheese(true).Pepperoni(true).Bacon(true).Build();
            Console.ReadLine();
        }
    }

    public class Pizza
    {
        private int size;
        private bool cheese;
        private bool pepperoni;
        private bool bacon;

        public Pizza(Builder builder)
        {
            size = builder.size;
            cheese = builder.cheese;
            pepperoni = builder.pepperoni;
            bacon = builder.bacon;
        }

    }

    public class Builder
    {
        //required
        public int size;

        //optional
        public bool cheese = false;
        public bool pepperoni = false;
        public bool bacon = false;

        public Builder(int size)
        {
            this.size = size;
        }

        public Builder Cheese(bool value)
        {
            this.cheese = value;
            return this;
        }

        public Builder Pepperoni(bool value)
        {
            this.pepperoni = value;
            return this;
        }

        public Builder Bacon(bool value)
        {
            this.bacon = value;
            return this;
        }

        public Pizza Build()
        {
            return new Pizza(this);
        }
    }

}


//Example 2


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Builder
{
    class Program
    {
        static void Main(string[] args)
        {
            portfolio p = new builder().withA(false).withB(true).withC(true).build();
            Console.ReadLine();
        }
    }

    public class portfolio
    {
        public bool a = false;
        public bool b = false;
        public bool c = false;
    }

    public class builder
    {

        portfolio p = new portfolio();

        public builder withA(bool v)
        {
            p.a = v;
            return this;
        }

        public builder withB(bool v)
        {
            p.b = v;
            return this;
        }

        public builder withC(bool v)
        {
            p.c = v;
            return this;
        }

        public portfolio build()
        {
            return p;
        }


    }
}

Simple Factory


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;

namespace Factory
{
    class Program
    {
        static void Main(string[] args)
        {
            ProductFactory pf = new ProductFactory();
            Product p = pf.createProduct("OneProduct");
            Console.ReadLine();
        }
    }

    public class ProductFactory
    {
        public Product createProduct(String ProductID){
            //if (ProductID=="OneProduct")
            //    return new OneProduct();
            //else if (ProductID=="TwoProduct")
            //    return new TwoProduct();
            //else
            //    return null;


            Assembly assembly = Assembly.GetExecutingAssembly(); //Get the current assembly object
            AssemblyName assemblyName = assembly.GetName(); //Get the name of the assembly (this will include the public token and version number
            Type t = assembly.GetType(assemblyName.Name + "." + ProductID); //Use just the name concat to the class chosen to get the type of the object
            return (Product)Activator.CreateInstance(t); //Create the object, cast it and return it to the caller
        }

    }

    public abstract class Product
    {
    }

    public class OneProduct : Product
    {
        public OneProduct()
        {
        }
    }

    public class TwoProduct : Product
    {
        public TwoProduct()
        {
        }     
    }
}