Factory and Abstract Factory Sample


May not perfect, but suit yourself.

 

These are the Interface that will later be used in the abstract class. Please note that you can also use the interface directly from product classes.

public interface InterfaceHuman
{
    void DoActivity();
    void Kill();
    string GetWorkPlace();
}

 

interface InterfaceFactory
{
    void RecruitHuman(ref AbstractHuman singleHuman);
    void PrintEmployee();
}

These are the abstract classes which will use the interface. This showed how methods from interface can be implemented as an abstract method, and later on be overridden by the product.

public abstract class AbstractHuman : InterfaceHuman
{
    public string Name;
    public bool IsAlive;
    public int Age;
    public bool IsWorking;
    public AbstractFactory WorkPlace;
    #region abstract methods

    public abstract void DoActivity();

    #endregion
    #region constructor

    public AbstractHuman()
    {
        Name = "";
        IsAlive = true;
        Age = (int)(new Random(60)).Next(20, 60);
        IsWorking = false;
    }

    public AbstractHuman(string _name)
    {
        Name = _name;
        IsAlive = true;
        Age = (int)(new Random(60)).Next(20, 60);
        IsWorking = false;
    }

    #endregion
    #region InterfaceHuman Members

    public void Kill()
    {
        IsAlive = false;
    }

    public abstract string GetWorkPlace();

    #endregion
}

 

public abstract class AbstractFactory : InterfaceFactory
{
    #region abstract methods

    public IEnumerable<AbstractHuman> PeopleInFactory;
    public string Name;

    #endregion
    #region constructor

    public AbstractFactory()
    {
        Name = "";
        PeopleInFactory = new List<AbstractHuman>();
    }

    public AbstractFactory(string _name)
    {
        Name = _name;
        PeopleInFactory = new List<AbstractHuman>();
    }

    #endregion

    #region InterfaceFactory Members

    public abstract void RecruitHuman(ref AbstractHuman singleHuman);

    public void PrintEmployee()
    {
        if (((List<AbstractHuman>)PeopleInFactory).Count > 0)
        {
            Console.WriteLine("People working in " + this.Name + " are: ");
            foreach (AbstractHuman singleHuman in this.GetHumans())
            {
                Console.WriteLine(singleHuman.Name);
            }
        }
        else
        {
            Console.WriteLine("No people working in " + this.Name + ".");
        }
    }

    public IEnumerable<AbstractHuman> GetHumans()
    {
        return PeopleInFactory;
    }

    #endregion
}

These are the products from factory. Please notice that the abstract class is the parent, and in this example, the real implementation of the interface is happened here.

class AgricultureFactory : AbstractFactory
{
    public string Commodity;

    public AgricultureFactory(string _name,string _commodity)
        : base(_name)
    {
        this.Commodity = _commodity;
    }

    public override void RecruitHuman(ref AbstractHuman singleHuman)
    {
        if (!singleHuman.IsWorking)
        {
            EmployedHuman tempHuman = new EmployedHuman(singleHuman.Name, singleHuman.Age, this);
            ((List<AbstractHuman>)PeopleInFactory).Add(tempHuman);
            Console.WriteLine(singleHuman.Name + " is recruited at farm "
                + this.Name + " on Age " + singleHuman.Age + ".");
            tempHuman.WorkPlace = this;
            singleHuman = tempHuman;
        }
        else
        {
            Console.WriteLine("Farm " + this.Name + " cannot recruit " + singleHuman.Name
                + " because he currently working in " + singleHuman.GetWorkPlace() + ".");
        }
    }
}

 

class GiantFactory : AbstractFactory
{
    public string Product;
    public GiantFactory(string _name, string _product)
        : base(_name)
    {
        this.Product = _product;
    }

    public override void RecruitHuman(ref AbstractHuman singleHuman)
    {
        if (!singleHuman.IsWorking)
        {
            EmployedHuman tempHuman = new EmployedHuman(singleHuman.Name, singleHuman.Age, this);
            ((List<AbstractHuman>)PeopleInFactory).Add(tempHuman);
            Console.WriteLine(singleHuman.Name + " is recruited at industry "
                + this.Name + " on Age " + singleHuman.Age + ".");
            tempHuman.WorkPlace = this;
            singleHuman = tempHuman;
        }
        else
        {
            if (singleHuman.GetType() == typeof(EmployedHuman))
            {
                EmployedHuman tempHuman = singleHuman as EmployedHuman;
                ((List<AbstractHuman>)PeopleInFactory).Add(tempHuman);
                Console.WriteLine(singleHuman.Name + " is recruited at industry "
                    + this.Name + " on Age " + singleHuman.Age + ".");
                tempHuman.WorkPlace = this;
                singleHuman = tempHuman;
            }
        }
    }
}

 

class EmployedHuman : AbstractHuman
{
    #region attribs
    public new AbstractFactory WorkPlace
    {
        get
        {
            return base.WorkPlace;
            //return employer;
        }
        set
        {
            if (base.WorkPlace != null)
            {
                //((List<AbstractHuman>)employer.PeopleInFactory).Remove(this);
                //this.employer = value;
                ((List<AbstractHuman>)base.WorkPlace.PeopleInFactory).Remove(this);
                base.WorkPlace = value;
            }
            else
            {
                base.WorkPlace = value;
            }
        }
    }
    //private AbstractFactory employer;
    #endregion
    #region constructor
    public EmployedHuman(string _name)
        : base(_name)
    {
        base.IsWorking = true;
        //this.employer = null;
        base.WorkPlace = null;
    }
    public EmployedHuman(string _name, int _age, AbstractFactory _workplace)
        : base(_name)
    {
        this.Age = _age;
        //this.employer = null;
        base.WorkPlace = null;
        base.IsWorking = true;
    }
    #endregion

    public override void DoActivity()
    {
        Console.WriteLine(base.Name + "work around...");
    }

    public void SwitchWorkPlace(AbstractFactory newWorkplace)
    {
        WorkPlace = newWorkplace;
    }

    public override string GetWorkPlace()
    {
        return WorkPlace.Name;
    }
}

 

class JoblessHuman : AbstractHuman
{
    public JoblessHuman(string _name)
        : base(_name)
    {
        this.Name = _name;
    }

    public JoblessHuman(string _name, int _age)
        : base(_name)
    {
        this.Age = _age;
    }

    public override void DoActivity()
    {
        Console.WriteLine(base.Name + "laze around...");
    }

    public override string GetWorkPlace()
    {
        return "none";
    }
}

Result test:

AbstractFactory MetalIndustry = new GiantFactory("Metal Jaya", "metal");
AbstractFactory MangoFarm = new AgricultureFactory("Kebun Hebat", "mangga");
AbstractHuman manusiaA = new JoblessHuman("A");
AbstractHuman manusiaB = new JoblessHuman("B");

MetalIndustry.PrintEmployee();
MangoFarm.PrintEmployee();

Console.ReadKey();
MangoFarm.RecruitHuman(ref manusiaA);
MangoFarm.RecruitHuman(ref manusiaB);

MetalIndustry.PrintEmployee();
MangoFarm.PrintEmployee();

Console.ReadKey();
MetalIndustry.RecruitHuman(ref manusiaB);

MetalIndustry.PrintEmployee();
MangoFarm.PrintEmployee();

Console.ReadKey();

To really know and understand on how the factory work, these human and factories will exists in a City (world). The world exists as the main procedure in console call. Other version it might be: Reptile factory produce Alligator that live in AnimalWorld. Just like Human factory produce a JoblessHuman that live in a City.

Advertisements

Author: Aditya Yedija Situmeang

Developer, Lover, Christian, Omnomnomnivore, Gamer.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s