Pattern - Factory

//Lets the Factory class decides which class to instantiate. Classes must be of same base type. using System; class Program { abstract class Position //or interface { public abstract string Title { get; } } class Manager : Position { public override string Title { get { return "Manager"; } } } class Clerk : Position { public override string Title { get { return "Clerk"; } } } class Programmer : Position { public override string Title { get { return "Programmer"; } } } static class Factory { /// <summary> /// Decides which class to instantiate. /// </summary> public static Position Get(int id) { switch (id) { case 0: return new Manager(); case 1: case 2: return new Clerk(); case 3: default: return new Programmer(); } } } static void Main() { for (int i = 0; i <= 3; i++) { var position = Factory.Get(i); Console.WriteLine("Where id = {0}, position = {1} ", i, position.Title); } } }

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.