Last active
December 30, 2015 09:53
-
-
Save SridharPasham/9ab68efa7e6fdedf4c2f to your computer and use it in GitHub Desktop.
AbstractFactory sample code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
namespace PatternsByVS | |
{ | |
public class Book | |
{ | |
public string Title { get; set; } | |
public string Pages { get; set; } | |
public override string ToString() | |
{ | |
var str = string.Format("Book, Title : {0}, Pages: {1}", Title, Pages); | |
return str; | |
} | |
} | |
public class Student | |
{ | |
public string Name { get; set; } | |
public int Age { get; set; } | |
public override string ToString() | |
{ | |
var str = string.Format("Student, Name : {0}, Age: {1}", Name, Age); | |
return str; | |
} | |
} | |
public class Program | |
{ | |
public static object CreateInstance(string className, Dictionary<string, object> values) | |
{ | |
Type type = Type.GetType("PatternsByVS." + className); | |
object instance = Activator.CreateInstance(type); | |
foreach (var item in values) | |
{ | |
type.GetProperty(item.Key).SetValue(instance, item.Value, null); | |
} | |
return instance; | |
} | |
static void Main(string[] args) | |
{ | |
var bookInstance = CreateInstance("Book", new Dictionary<string, object> | |
{ | |
{"Title", "Shiva trilogy"}, | |
{"Pages", "340"}, | |
}); | |
Console.WriteLine(bookInstance.ToString()); | |
var studentInstance = CreateInstance("Student", new Dictionary<string, object> | |
{ | |
{"Name", "Sridhar"}, | |
{"Age", 27}, | |
}); | |
Console.WriteLine(studentInstance.ToString()); | |
Console.ReadLine(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment