Wednesday, December 10, 2008

some programming issues

Design patterns have picked up a lot of importance off late and rightfully so. To define design patterns in simple words they are "popular solutions for common design problems".
As an individual involved in the academe, I firmly believe that students must have background in this aspect to make them ready when they face the actual IT world...

I include in this article a very simple creational design pattern - the Factory Design pattern. I hope this can give some knowledge to you guys out there....

The factory method pattern is an object-oriented design pattern. Like other creational patterns, it deals with the problem of creating objects (products) without specifying the exact class of object that will be created. The factory method design pattern handles this problem by defining a separate method for creating the objects, whose subclasses can then override to specify the derived type of product that will be created. More generally, the term factory method is often used to refer to any method whose main purpose is creation of objects.

Check this very simple example...

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

namespace ConsoleApplication1
{
class MainFactory
{
public BaseFactory GetObject(int x)
{
BaseFactory b = null;
switch (x)
{
case 1: b = new FirstObject();
break;
case 2: b = new SecondObject();
break;
default: Console.WriteLine("invalid");
break;
}
return b;
}
}

interface BaseFactory
{
void CreateObject();
}
public class FirstObject: BaseFactory
{
public void CreateObject()
{
Console.WriteLine("First object created...");
}
}
public class SecondObject : BaseFactory
{
public void CreateObject()
{
Console.WriteLine("Second object created...");
}
}
class Program
{
static void Main(string[] args)
{

MainFactory f = new MainFactory();
BaseFactory bf = f.GetObject(1);
bf.CreateObject();
Console.ReadKey();
}
}
}

Note: Depending on the integer value of the GetObject argument, the MainFactory class will create the object...

4 comments:

  1. sir tried this bah. ang i butang ra lageh is sa output kay "first object created" ra
    am i missing something?
    what ouput are we expeting here?

    ReplyDelete
  2. To Mr zion
    The output of the given code is "first object created..." since the argument that i put in my GetObject argument is 1.

    if you look at the GetObject method in the MainFactory class, there is a "switch" statement with two options: if case is 1 then it will create the first object, otherwise the second object. ok? you are not missing something.

    by the way you can enhance this program by making it dynamic, meaning prompt the user to interact with by entering a number for whatever object he wants to create. thank you.

    ReplyDelete
  3. To mr zion
    i'm sorry, it's not GetObject argument - it's GetObject Method.

    ReplyDelete
  4. thanks sir.
    done the things uve just said.
    thought i miss something
    thanks for elaborating more on this..
    keep up the good work
    BTW.. im on 1 of ure class.
    ^_^

    ReplyDelete