Factory method pattern: Difference between revisions

Content deleted Content added
Java: add image
Line 166:
/// Implementation of Factory - Used to create objects.
/// </summary>
public class FactoryPersonFactory
{
public IPerson GetPerson(PersonType type)
Line 182:
}
</syntaxhighlight>
In the above code you can see the creation of one interface called <code>IPerson</code> and two implementations called <code>Villager</code> and <code>CityPerson</code>. Based on the type passed into the <code>FactoryPersonFactory</code> object, we are returning the original concrete object as the interface <code>IPerson</code>.
 
A factory method is just an addition to <code>FactoryPersonFactory</code> class. It creates the object of the class through interfaces but on the other hand, it also lets the subclass decide which class is instantiated.
 
<syntaxhighlight lang="csharp">