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