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

    


Chain of Responsibility Pattern

image_pdfimage_print
   
 

using System;
public abstract class Chain {

    protected Chain theNextInChain;
    public abstract void DealWithRequirement(string requirement);

    public void NextInChain(Chain next) {
        this.theNextInChain = next;
    }

}

public class Employee : Chain {
    public override void DealWithRequirement(string requirement) {
        switch (requirement) {
            case "Job":
                Console.WriteLine("{0} Staff", this);
                break;
            default:
                if (theNextInChain != null)
                    theNextInChain.DealWithRequirement(requirement);
                break;
        }
    }
}


public class Manager : Chain {
    public override void DealWithRequirement(string requirement) {
        switch (requirement) {
            case "Manager":
                Console.WriteLine("{0} requirement.", this);
                break;
            default:
                if (theNextInChain != null)
                    theNextInChain.DealWithRequirement(requirement);
                break;
        }
    }
}


public class Senior : Chain {
    public override void DealWithRequirement(string requirement) {
        switch (requirement) {

            default: Console.WriteLine("{0} has managed the " + requirement + " requirement.", this);
                break;
        }
    }

    public Senior() { ;}

}
class Client {
    static void Main(string[] args) {
        Chain staff = new Employee();
        Chain manager = new Manager();
        Chain seniorManager = new Senior();

        staff.NextInChain(manager);
        manager.NextInChain(seniorManager);

        staff.DealWithRequirement("Corporates");
        staff.DealWithRequirement("Job");
        staff.DealWithRequirement("Manager");
        staff.DealWithRequirement("Agency");
    }
}

    


Adapter Pattern Demo

image_pdfimage_print
   
 
using System;

public sealed class ForeignExchange {
    public string UStoUK() {
        return "USD to GBP is...";
    }

    public string UStoCAN() {
        return "USD to CND is...";
    }

    public ForeignExchange() { ;}
}

public class AdapterWrapper {
    private Adapter adapt;

    public string AdapterWrapper_USD_GBP() {
        return this.adapt.USD_GBP();
    }

    public string AdapterWrapper_USD_CND() {
        return this.adapt.USD_CND();
    }

    public string AdapterWrapper_USD_AUD() {
        return this.adapt.USD_AUD();
    }

    public AdapterWrapper() {
        adapt = new Adapter();
    }
}


public class Adapter {
    private ForeignExchange sourceCode;

    public string USD_GBP() {
        return "Conversion " + this.sourceCode.UStoUK();
    }

    public string USD_CND() {
        return "Conversion " + this.sourceCode.UStoCAN();
    }

    public string USD_AUD() {
        return "Conversion USD to AUD is...";
    }

    public string USD_JPY() {
        return "Conversion USD to JNY is...";
    }

    public Adapter() {
        sourceCode = new ForeignExchange();
    }
}

public class Client {

    static void Main(string[] args) {
        AdapterWrapper afx = new AdapterWrapper();
        Console.WriteLine(afx.AdapterWrapper_USD_GBP());
        Console.WriteLine(afx.AdapterWrapper_USD_CND());

        Console.WriteLine(afx.AdapterWrapper_USD_AUD());
    }
}

    


Event Tracker

image_pdfimage_print


   

/*
User Interfaces in C#: Windows Forms and Custom Controls
by Matthew MacDonald

Publisher: Apress
ISBN: 1590590457
*/
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace EventTracker
{
    /// <summary>
    /// Summary description for EventTracker.
    /// </summary>
    public class EventTracker : System.Windows.Forms.Form
    {
        internal System.Windows.Forms.GroupBox GroupBox1;
        internal System.Windows.Forms.Label Label4;
        internal System.Windows.Forms.Label Label1;
        internal System.Windows.Forms.PictureBox pic;
        internal System.Windows.Forms.TextBox txt;
        internal System.Windows.Forms.Button cmd;
        internal System.Windows.Forms.Label Label2;
        internal System.Windows.Forms.Label Label3;
        internal System.Windows.Forms.ListBox lstLog;
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.Container components = null;

        public EventTracker()
        {
            //
            // Required for Windows Form Designer support
            //
            InitializeComponent();

            //
            // TODO: Add any constructor code after InitializeComponent call
            //
        }

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        protected override void Dispose( bool disposing )
        {
            if( disposing )
            {
                if (components != null) 
                {
                    components.Dispose();
                }
            }
            base.Dispose( disposing );
        }

        #region Windows Form Designer generated code
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.GroupBox1 = new System.Windows.Forms.GroupBox();
            this.Label4 = new System.Windows.Forms.Label();
            this.Label1 = new System.Windows.Forms.Label();
            this.pic = new System.Windows.Forms.PictureBox();
            this.txt = new System.Windows.Forms.TextBox();
            this.cmd = new System.Windows.Forms.Button();
            this.Label2 = new System.Windows.Forms.Label();
            this.Label3 = new System.Windows.Forms.Label();
            this.lstLog = new System.Windows.Forms.ListBox();
            this.GroupBox1.SuspendLayout();
            this.SuspendLayout();
            // 
            // GroupBox1
            // 
            this.GroupBox1.Anchor = ((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
                | System.Windows.Forms.AnchorStyles.Right);
            this.GroupBox1.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                                    this.Label4,
                                                                                    this.Label1,
                                                                                    this.pic,
                                                                                    this.txt,
                                                                                    this.cmd,
                                                                                    this.Label2});
            this.GroupBox1.FlatStyle = System.Windows.Forms.FlatStyle.System;
            this.GroupBox1.Location = new System.Drawing.Point(8, 5);
            this.GroupBox1.Name = "GroupBox1";
            this.GroupBox1.Size = new System.Drawing.Size(384, 148);
            this.GroupBox1.TabIndex = 9;
            this.GroupBox1.TabStop = false;
            // 
            // Label4
            // 
            this.Label4.Location = new System.Drawing.Point(92, 108);
            this.Label4.Name = "Label4";
            this.Label4.Size = new System.Drawing.Size(56, 16);
            this.Label4.TabIndex = 5;
            this.Label4.Text = "And here:";
            // 
            // Label1
            // 
            this.Label1.Location = new System.Drawing.Point(6, 24);
            this.Label1.Name = "Label1";
            this.Label1.Size = new System.Drawing.Size(144, 16);
            this.Label1.TabIndex = 2;
            this.Label1.Text = "Test keyboard events here:";
            // 
            // pic
            // 
            this.pic.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
            this.pic.Location = new System.Drawing.Point(156, 48);
            this.pic.Name = "pic";
            this.pic.Size = new System.Drawing.Size(192, 48);
            this.pic.TabIndex = 3;
            this.pic.TabStop = false;
            this.pic.Click += new System.EventHandler(this.pic_Click);
            this.pic.MouseEnter += new System.EventHandler(this.pic_MouseEnter);
            this.pic.MouseHover += new System.EventHandler(this.pic_MouseHover);
            this.pic.MouseUp += new System.Windows.Forms.MouseEventHandler(this.pic_MouseUp);
            this.pic.DoubleClick += new System.EventHandler(this.pic_DoubleClick);
            this.pic.MouseLeave += new System.EventHandler(this.pic_MouseLeave);
            this.pic.MouseDown += new System.Windows.Forms.MouseEventHandler(this.pic_MouseDown);
            // 
            // txt
            // 
            this.txt.Location = new System.Drawing.Point(156, 20);
            this.txt.Name = "txt";
            this.txt.Size = new System.Drawing.Size(192, 21);
            this.txt.TabIndex = 1;
            this.txt.Text = "";
            this.txt.KeyDown += new System.Windows.Forms.KeyEventHandler(this.txt_KeyDown);
            this.txt.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.txt_KeyPress);
            this.txt.TextChanged += new System.EventHandler(this.txt_TextChanged);
            this.txt.KeyUp += new System.Windows.Forms.KeyEventHandler(this.txt_KeyUp);
            // 
            // cmd
            // 
            this.cmd.FlatStyle = System.Windows.Forms.FlatStyle.System;
            this.cmd.Location = new System.Drawing.Point(156, 100);
            this.cmd.Name = "cmd";
            this.cmd.Size = new System.Drawing.Size(88, 28);
            this.cmd.TabIndex = 4;
            this.cmd.Text = "Button1";
            this.cmd.Click += new System.EventHandler(this.pic_Click);
            this.cmd.MouseUp += new System.Windows.Forms.MouseEventHandler(this.pic_MouseUp);
            this.cmd.MouseEnter += new System.EventHandler(this.pic_MouseEnter);
            this.cmd.MouseHover += new System.EventHandler(this.pic_MouseHover);
            this.cmd.MouseLeave += new System.EventHandler(this.pic_MouseLeave);
            this.cmd.MouseDown += new System.Windows.Forms.MouseEventHandler(this.pic_MouseDown);
            // 
            // Label2
            // 
            this.Label2.Location = new System.Drawing.Point(20, 52);
            this.Label2.Name = "Label2";
            this.Label2.Size = new System.Drawing.Size(128, 16);
            this.Label2.TabIndex = 2;
            this.Label2.Text = "Test mouse events here:";
            // 
            // Label3
            // 
            this.Label3.Location = new System.Drawing.Point(24, 105);
            this.Label3.Name = "Label3";
            this.Label3.Size = new System.Drawing.Size(64, 24);
            this.Label3.TabIndex = 8;
            this.Label3.Text = "Label3";
            // 
            // lstLog
            // 
            this.lstLog.Anchor = (((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
                | System.Windows.Forms.AnchorStyles.Left) 
                | System.Windows.Forms.AnchorStyles.Right);
            this.lstLog.IntegralHeight = false;
            this.lstLog.Location = new System.Drawing.Point(8, 161);
            this.lstLog.Name = "lstLog";
            this.lstLog.Size = new System.Drawing.Size(384, 212);
            this.lstLog.TabIndex = 7;
            // 
            // EventTracker
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 14);
            this.ClientSize = new System.Drawing.Size(400, 378);
            this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                          this.GroupBox1,
                                                                          this.Label3,
                                                                          this.lstLog});
            this.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
            this.Name = "EventTracker";
            this.Text = "Event Tracker";
            this.GroupBox1.ResumeLayout(false);
            this.ResumeLayout(false);

        }
        #endregion


        private void Log(String data)
        {
            lstLog.Items.Add(data);
            int itemsPerPage = (int)(lstLog.Height / lstLog.ItemHeight);
            lstLog.TopIndex = lstLog.Items.Count - itemsPerPage;
        }
                                                                                                    

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main() 
        {
            Application.Run(new EventTracker());
        }

        private void txt_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
        {
            Log("Key Down: " + e.KeyCode.ToString() + e.KeyValue.ToString());
        }

        private void txt_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
        {
            Log("Key Press: " + e.KeyChar.ToString());
        }

        private void txt_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e)
        {
            Log("Key Up: " + e.KeyCode.ToString() + e.KeyValue.ToString() + " Text is: " + txt.Text);
        }

        private void txt_TextChanged(object sender, System.EventArgs e)
        {
            Log("Changed: " + " Text is: " + txt.Text);
        }

        private void pic_MouseEnter(object sender, System.EventArgs e)
        {
            Log("Mouse Enter");
        }

        private void pic_MouseHover(object sender, System.EventArgs e)
        {
            Log("Mouse Hover");
        }

        private void pic_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            Log("Mouse Down: X=" + e.X.ToString() + "Y=" + e.Y.ToString() + " Button=" + e.Button.ToString());
        }

        private void pic_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            Log("Mouse Down: X=" + e.X.ToString() + "Y=" + e.Y.ToString() + " Button=" + e.Button.ToString());
        }

        private void pic_Click(object sender, System.EventArgs e)
        {
            Log("Click");
        }

        private void pic_DoubleClick(object sender, System.EventArgs e)
        {
            Log("Double Click");
        }

        private void pic_MouseLeave(object sender, System.EventArgs e)
        {
            Log("Mouse Leave");
        }
    }
}



           
          


Get event name for sender

image_pdfimage_print


   


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

public class Form1 : Form
{
   Label Label1;
   TextBox TextBox1;
   Button Button1;

  public Form1()
  {
        InitializeComponent();
  }
   private void ctrlClick(System.Object sender, EventArgs e)
   {
     Control ctrl = (Control)sender;
     MessageBox.Show("You clicked: " + ctrl.Name);
   }

  private void InitializeComponent()
  {
    this.Label1 = new System.Windows.Forms.Label();
    this.TextBox1 = new System.Windows.Forms.TextBox();
    this.Button1 = new System.Windows.Forms.Button();
    this.SuspendLayout();
    // 
    // Label1
    // 
    this.Label1.FlatStyle = System.Windows.Forms.FlatStyle.System;
    this.Label1.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
    this.Label1.Location = new System.Drawing.Point(14, 97);
    this.Label1.Name = "Label1";
    this.Label1.Size = new System.Drawing.Size(112, 24);
    this.Label1.TabIndex = 8;
    this.Label1.Text = "Label1";
    this.Label1.Click += new System.EventHandler(this.ctrlClick);
    // 
    // TextBox1
    // 
    this.TextBox1.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
    this.TextBox1.Location = new System.Drawing.Point(12, 61);
    this.TextBox1.Name = "TextBox1";
    this.TextBox1.Size = new System.Drawing.Size(156, 21);
    this.TextBox1.TabIndex = 7;
    this.TextBox1.Text = "TextBox1";
    this.TextBox1.Click += new System.EventHandler(this.ctrlClick);
    // 
    // Button1
    // 
    this.Button1.FlatStyle = System.Windows.Forms.FlatStyle.System;
    this.Button1.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
    this.Button1.Location = new System.Drawing.Point(12, 21);
    this.Button1.Name = "Button1";
    this.Button1.Size = new System.Drawing.Size(96, 28);
    this.Button1.TabIndex = 6;
    this.Button1.Text = "Button1";
    this.Button1.Click += new System.EventHandler(this.ctrlClick);
    // 
    // Form1
    // 
    this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
    this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
    this.ClientSize = new System.Drawing.Size(367, 281);
    this.Controls.Add(this.Label1);
    this.Controls.Add(this.TextBox1);
    this.Controls.Add(this.Button1);
    this.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
    this.Name = "Form1";
    this.Text = "Control Medley";
    this.ResumeLayout(false);
    this.PerformLayout();

  }


  [STAThread]
  static void Main()
  {
    Application.EnableVisualStyles();
    Application.Run(new Form1());
  }

}