Search

Builder pattern

This pattern creates object based on the Interface, but also lets the subclass decide which class to instantiate. It also has finer control over the construction process. There is a concept of Director in Builder Pattern implementation. The director actually creates the object and also runs a few tasks after that.



In case of Builder pattern you can see the Director is actually using CreateBuilder to create the instance of the builder. So when the Builder is actually created, we can also invoke a few common task in it.

arwin.c - Address Resolution Program for windows

This program finds the absolute address of a function in a specified DLL. happy shellcoding!

#include
#include

/***************************************
arwin - win32 address resolution program
by steve hanna v.01
vividmachines.com
shanna@uiuc.edu
you are free to modify this code
but please attribute me if you
change the code. bugfixes & additions
are welcome please email me!
to compile:
you will need a win32 compiler with
the win32 SDK
***************************************/


int main(int argc, char** argv)
{
HMODULE hmod_libname;
FARPROC fprc_func;

printf("arwin - win32 address resolution program - by steve hanna - v.01\n");
if(argc < 3)
{
printf("%s \n",argv[0]);
exit(-1);
}

hmod_libname = LoadLibrary(argv[1]);
if(hmod_libname == NULL)
{
printf("Error: could not load library!\n");
exit(-1);
}
fprc_func = GetProcAddress(hmod_libname,argv[2]);

if(fprc_func == NULL)
{
printf("Error: could find the function in the library!\n");
exit(-1);
}
printf("%s is located at 0x%08x in %s\n",argv[2],(unsigned int)fprc_func,argv[1]);


}

Factory method pattern

Factory pattern deals with the instantiation of object without exposing the instantiation logic. In other words, a Factory is actually a creator of object which has common interface.

/*Factory Pattern*/

//interfce
public interface IPeople
{
string GetName();
}

public class Villagers : IPeople
{
public string GetName()
{
return "Village Guy";
}
}

public class CityPeople : IPeople
{
public string GetName()
{
return "City Guy";
}
}

public enum PeopleType
{
RURAL,
URBAN
}

///
/// Implementation of Factory - Used to create objects
///

public class Factory
{
public IPeople GetPeople(PeopleType type)
{
IPeople people = null;
switch (type)
{
case PeopleType.RURAL :
people = new Villagers();
break;
case PeopleType.URBAN:
people = new CityPeople();
break;
default:
break;
}
return people;
}
}

In the above code you can see the creation of one interface called IPeople and implemented two classes from it as Villagers and CityPeople. Based on the type passed into the factory object, We are sending back the original concrete object as the Interface IPeople.


A Factory method is just an addition to Factory class. It creates the object of the class through interfaces but on the other hand, it also lets the subclass to decide which class to be instantiated.


public interface IProduct
{
string GetName();
string SetPrice(double price);
}

public class IPhone : IProduct
{
private double _price;
public string GetName()
{
return "Apple TouchPad";
}

public string SetPrice(double price)
{
this._price = price;
return "success";
}
}

/* Almost same as Factory, just an additional exposure to do something with the created method */

public abstract class ProductAbstractFactory
{
protected abstract IProduct DoSomething();
public IProduct GetObject() // Implementation of Factory Method.
{
return this.DoSomething();
}
}

public class IPhoneConcreteFactory : ProductAbstractFactory
{
protected override IProduct DoSomething()
{
IProduct product = new IPhone();
//Do something with the object after you get the object.
product.SetPrice(20.30);
return product;
}
}

Abstract factory pattern

Abstract factory is the extension of basic Factory pattern. It provides Factory interfaces for creating a family of related classes.a Ti provides a way to encapsulate a group of individual factories that have a common theme without specifying their concrete classes. In other words, here I am declaring interfaces for Factories, which will in turn work in similar fashion as with Factories.


/*Abstract factory pattern*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DesignPatterns.AbstractFactory
{
public class GenericFactory
where T : new()
{
public T CreateObject()
{
return new T();
}
}

public abstract class CarFactory
{
public abstract SportsCar CreateSportsCar();
public abstract FamilyCar CreateFamilyCar();
}

public abstract class FamilyCar
{
public abstract void Speed(SportsCar abstractFamilyCar);
}

public abstract class SportsCar
{
}

public class MercedesFactory : CarFactory
{
public override SportsCar CreateSportsCar()
{
return new MercedesSportsCar();
}

public override FamilyCar CreateFamilyCar()
{
return new MercedesFamilyCar();
}
}



class MercedesSportsCar : SportsCar
{
}

class MercedesFamilyCar : FamilyCar
{
public override void Speed(SportsCar abstractSportsCar)
{
Console.WriteLine(GetType().Name + " is slower than "
+ abstractSportsCar.GetType().Name);
}
}

public class Driver
{
private CarFactory _carFactory;
private SportsCar _sportsCar;
private FamilyCar _familyCar;

public Driver(CarFactory carFactory)
{
CarFactory = carFactory;
SportsCar = CarFactory.CreateSportsCar();
FamilyCar = CarFactory.CreateFamilyCar();
}

private CarFactory CarFactory
{
get { return _carFactory; }
set { _carFactory = value; }
}

private SportsCar SportsCar
{
get { return _sportsCar; }
set { _sportsCar = value; }
}

private FamilyCar FamilyCar
{
get { return _familyCar; }
set { _familyCar = value; }
}

public void CompareSpeed()
{
FamilyCar.Speed(SportsCar);
}
}
}

The factory method is also implemented using common interface each of which returns objects.


// Abstract factory using common interface
public interface IFactory1
{
IPeople GetPeople();
}
public class Factory1 : IFactory1
{
public IPeople GetPeople()
{
return new Villagers();
}
}

public interface IFactory2
{
IProduct GetProduct();
}
public class Factory2 : IFactory2
{
public IProduct GetProduct()
{
return new IPhone();
}
}

public abstract class AbstractFactory12
{
public abstract IFactory1 GetFactory1();
public abstract IFactory2 GetFactory2();
}

public class ConcreteFactory : AbstractFactory12
{

public override IFactory1 GetFactory1()
{
return new Factory1();
}

public override IFactory2 GetFactory2()
{
return new Factory2();
}
}

The following is the real word example to abstract factory pattern, which makes it much easier to understand.

// Abstract Factory pattern -- Real World example

namespace Abstract.RealWorldExample

{

///
/// MainApp startup class for Real-World
/// Abstract Factory Design Pattern.
///


class MainApp
{

///
/// Entry point into console application.
///


public static void Main()

{
// Create and run the African animal world
ContinentFactory africa = new AfricaFactory();
AnimalWorld world = new AnimalWorld(africa);
world.RunFoodChain();

// Create and run the American animal world
ContinentFactory america = new AmericaFactory();
world = new AnimalWorld(america);
world.RunFoodChain();


// Wait for user input
Console.ReadKey();
}
}

///
/// The 'AbstractFactory' abstract class
///

abstract class ContinentFactory
{
public abstract Herbivore CreateHerbivore();
public abstract Carnivore CreateCarnivore();
}


///
/// The 'ConcreteFactory1' class
///

class AfricaFactory : ContinentFactory
{
public override Herbivore CreateHerbivore()
{
return new Wildebeest();

}

public override Carnivore CreateCarnivore()
{
return new Lion();

}
}

///
/// The 'ConcreteFactory2' class
///

class AmericaFactory : ContinentFactory
{
public override Herbivore CreateHerbivore()
{
return new Bison();
}

public override Carnivore CreateCarnivore()
{
return new Wolf();
}
}


///
/// The 'AbstractProductA' abstract class
///

abstract class Herbivore
{

}


///
/// The 'AbstractProductB' abstract class
///

abstract class Carnivore
{
public abstract void Eat(Herbivore h);
}


///
/// The 'ProductA1' class
///

class Wildebeest : Herbivore
{

}

///
/// The 'ProductB1' class
///

class Lion : Carnivore
{
public override void Eat(Herbivore h)
{
// Eat Wildebeest
Console.WriteLine(this.GetType().Name + " eats " + h.GetType().Name);
}
}

///
/// The 'ProductA2' class
///

class Bison : Herbivore
{

}

///
/// The 'ProductB2' class
///

class Wolf : Carnivore
{
public override void Eat(Herbivore h)
{
// Eat Bison
Console.WriteLine(this.GetType().Name + " eats " + h.GetType().Name);
}
}


///
/// The 'Client' class
///

class AnimalWorld
{
private Herbivore _herbivore;
private Carnivore _carnivore;

// Constructor
public AnimalWorld(ContinentFactory factory)
{
_carnivore = factory.CreateCarnivore();
_herbivore = factory.CreateHerbivore();
}

public void RunFoodChain()
{
_carnivore.Eat(_herbivore);

}
}
}