Posts

Showing posts from October, 2015

Observer design Pattern

When many objects needs to recieve updates when there is a change in another objects, Benifit = Loosely coupled – publishers need not be aware of the type of subscriber (observer) Flaws The publishers( subject ) may send updates that dont matter to the subscriber (observer) using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; // Liskov Substitution Principle namespace ManualResetEvent_ {     interface IPublisher     {         void register(Subscriber subscriber);         void unregister(Subscriber subscriber);         void notify();     }     class publisher_ : IPublisher     {         private int no_of_items;         private List<Subscriber> subscriber_list = new List<Subscriber>();         public int inventory         {             get { return no_of_items; }             set             {                 if (no_of_items < value)                     notify();                 no_

Strategy Design Pattern

We have a bunch of algorithms/functionality and can be applied to the data set only by Depending upon the data, at runtime. So we use strategy design pattern Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from clients that use it using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; // Liskov Substitution Principle namespace ManualResetEvent_ { //class which calls the interface and based on which the mehods are called.     class Filetoberead     {         public string fileName { get; set; }         public string filetype { get; set; }         public IFileRead fileread_;         public Filetoberead(IFileRead file_name_new)         {             fileread_ = file_name_new;         }         public void CallFileReadClass(string Filename, string type)         {             fileread_.CallFileReadClass(fileName, filety

Adaptor design Pattern

It consiste of a class which acts a a bridge between two different and independent classes. When an interface is not compatible with a class , An adapter is used to make it compatible     customer_list  is a class which contains employee list in two dimensional string array. the client class accept employee name in array list . The adaper design pattern becomes handy in such scenario but doing the required conversion of the two dimensional string array to list of name for the employees. using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; // Liskov Substitution Principle namespace ManualResetEvent_ {     // Adapter class//     class customer_list     {         public string[][] customer;         public string[][] getCustomerNameList()         {             customer = new string[5][];             customer[0] = new string[] { "100", "Amit", "INDIA" };             custom

Factory Design Pattern

Factory Design Pattern In Factory pattern, we create object without exposing the creation logic. In this pattern, an interface is used for creating an object, but let subclass decide which class to instantiate. The creation of object is done when it is required. The Factory method allows a class later instantiation to subclasses. Template: interface Product { } class ConcreteProductA : Product { } class ConcreteProductB : Product { } abstract class Creator { public abstract Product FactoryMethod(string type); } class ConcreteCreator : Creator { public override Product FactoryMethod(string type) { switch (type) { case "A": return new ConcreteProductA(); case "B": return new ConcreteProductB(); default: throw new ArgumentException("Invalid type", "type"); } } } using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; // Liskov Substitution Principle

Combo Box or Drop Down List . Adding Objects

In a combo box we can add any object –lets start by creating a class Student as shown in the below Inside   But the default implementation of the toString() method in Student class which is inherited from the object class return the class name when a new object is created and called upon. We created the Student objects and added to the collection of the combo box as a list of items on form load event as seen in the below code snippet private void Form1_Load(object sender, EventArgs e)         {             comboBox4.Items.Add(new Student(1,"Amit"));             comboBox4.Items.Add(new Student(2,"Arun"));             comboBox4.Items.Add(new Student(3,"Arup"));             comboBox4.Items.Add(new Student(4,"Ajit"));             comboBox4.Items.Add(new Student(5,"Anup"));         } This event is registered as shown below this.Load += new System.EventHandler(this.Form1_Load); So we need to overrride the toString() method in the below sh

POC on Restaurant ordering system using factory pattern

POC on Restaurant ordering system using factory pattern using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ManualResetEvent_ {     //---------------------------------------------Factory Design Pattern  for createing Food Items---------------------------------------------------//     abstract class IFood     {         public string name { get; set; }         public int quantity { get; set; }         public int price { get; set; }        public abstract void GetValues();     }     class Rice : IFood     {         public string name { get; set; }         public int quantity { get; set; }         public int price { get; set; }         public Rice()         {                    }         public override void GetValues()         {             this.name = "Rice";             this.price = 20;             this.quantity = 1;         }     }     class Tea : IFood     {         string name;         int quantity

Builder Design Pattern

Image
In Elizabeth's day care center, the teacher helps the kids to build all kinds of toys to develop their creative skills. One of Elizabeth's favorite activities is to make animals with play-dough. A set of molds is the tool that Elizabeth always uses to create her favorite cool animals. One mold tool set includes five parts, including the head, body, arm, leg, and tail. Whenever Elizabeth wants to build an animal, she will use one set of each tools to make a head, body , leg, arm, and tail for that animal, and then assembles them with glue to build an animal. There are many types of animal mold tool sets that the kids can choose from. For example: if Elizabeth wants to make a monkey, then she will pick the set of monkey molds to start. Step 1. Make monkey head. Step 2. Make monkey body. Step 3. Make monkey leg. Step 4. Make monkey arm. Step 5. Make monkey tail. Once all the five parts are finished, then Elizabeth will glue them a