Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Special pages
Niidae Wiki
Search
Search
Appearance
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
Factory method pattern
(section)
Page
Discussion
English
Read
Edit
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
View history
General
What links here
Related changes
Page information
Appearance
move to sidebar
hide
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.
Summary:
Please note that all contributions to Niidae Wiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
Encyclopedia:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Search
Search
Editing
Factory method pattern
(section)
Add topic