Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Factory method pattern
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
====[[C Sharp (programming language)|C#]]==== <syntaxhighlight lang="csharp"> // Empty vocabulary of actual object public interface IPerson { string GetName(); } public class Villager : IPerson { public string GetName() { return "Village Person"; } } public class CityPerson : IPerson { public string GetName() { return "City Person"; } } public enum PersonType { Rural, Urban } /// <summary> /// Implementation of Factory - Used to create objects. /// </summary> public class PersonFactory { public IPerson GetPerson(PersonType type) { switch (type) { case PersonType.Rural: return new Villager(); case PersonType.Urban: return new CityPerson(); default: throw new NotSupportedException(); } } } </syntaxhighlight> The above code depicts the creation of an interface called <code>IPerson</code> and two implementations called <code>Villager</code> and <code>CityPerson</code>. Based on the type passed to the <code>PersonFactory</code> object, the original concrete object is returned as the interface <code>IPerson</code>. A factory method is just an addition to the <code>PersonFactory</code> class. It creates the object of the class through interfaces but also allows the subclass to decide which class is instantiated. <syntaxhighlight lang="csharp"> public interface IProduct { string GetName(); bool SetPrice(double price); } public class Phone : IProduct { private double _price; public string GetName() { return "Apple TouchPad"; } public bool SetPrice(double price) { _price = price; return true; } } /* Almost same as Factory, just an additional exposure to do something with the created method */ public abstract class ProductAbstractFactory { protected abstract IProduct MakeProduct(); public IProduct GetObject() // Implementation of Factory Method. { return this.MakeProduct(); } } public class PhoneConcreteFactory : ProductAbstractFactory { protected override IProduct MakeProduct() { IProduct product = new Phone(); // Do something with the object after receiving it product.SetPrice(20.30); return product; } } </syntaxhighlight> In this example, <code>MakeProduct</code> is used in <code>concreteFactory</code>. As a result, <code>MakeProduct()</code> may be invoked in order to retrieve it from the <code>IProduct</code>. Custom logic could run after the object is obtained in the concrete factory method. <code>GetObject</code> is made abstract in the factory interface.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)