Recently, I was watching the DESIGN PATTERNS written by the four, even the experience, so I wrote a small procedure using reflection to realize the factory method. This is not there, and I don't know if anyone thinks. Factory methods often use parameters to implement Creator. The method of parameters here is also used, but the traditional parameters are very different. Create a C # project, put a ComboBox in the form, a button, and enter MyProduct and YourProduct in the ComboBox Items property, and we will create objects through these two string. The form is shown as shown. Change the output in the properties of the project to the console application.
Then enter the following code outside the Form class. Public class create {public static product create copyProduct (String ProductName) {Product Product = NULL;
/ / You can also inherit the pruduct interface, generate a DLL, then load the DLL assembly a = askEMBLY.GETCALLINGASSEMBLY (); // class String s = @ "reflectingFactoryMethod." ProductName; // Create Object Object O = a.CreateInstance (s); // convert the object Pruduct Interface product = o as Product; return product;}} public class TheirCreator: Creator {override public static Product CreateProduct (string ProductName) {Product product = null;
/ / You can also inherit the pruduct interface, generate a DLL, then load the DLL assembly a = askEMBLY.LOADFILE (@ "c: / windows/system32/a.dll"); // class String s = @ "SomenameSpace." ProductName; // Create object object o = a.createInstance (s); // Translate the object to Pruduct interface product = o as product;}}}}}}}
Public interface product {void showproduct ();
Public class myproduct: product {public void showProduct () {// Todo: Add myProduct.showProduct implementation console.writeline ("my product");}}
PUBLIC CLASS YourProduct: Product () {public void showProduct () {// Todo: Add YourProduct.showProduct implementation console.writeline ("Your Producted"); private void button1_Click (object sender, System.EventArgs e) {Product product = Creator.CreateProduct (this.comboBox1.Text); product.ShowProduct ();} Here we can see, we come from this.comboBox1.Text Creating an object, this is a String. You can also read another DLL, where you create an object. If we would write the Creator class: public class Creator {public static Product CreateProduct () {Product product = null; StreamReader sr = new StreamReader ( "config.txt"); string AssemblyName = sr.ReadLine (); string NameSpaceName = sr .ReadLine (); string ProductName = sr.ReadLine (); Assembly a = Assembly.LoadFile (AssemblyName); string s = NameSpaceName ProductName; object o = a.CreateInstance (s); product = o as Product; return product; }} This Creator can also achieve the functionality of just now, and you can implement function extensions without writing Creator. Just inherit the Product interface, compile into a DLL, then properly modify the config.txt configuration file, you can easily implement it. Program vendors can only provide a Product interface, but do not need to provide Creator interface. Customers only need to inherit the Product interface according to their needs, and do not need to change the original executable release. Although this Creator also has some defects, such as the constructor that achieves a variety of structural functions, it is more difficult, but if it is a unified constructor, it can achieve endless extension. But this Creator is contradictory and classic factory methods. The classic approach needs to write Creator's subclasses to achieve expansion, and this Creator does not need. This is due to the introduction of reflection. Before you do not reflect this function, we must know that we need to create objects, that is, it is impossible to have a Creator class to create this class, and use reflection Creator You can create all products, even if this product is time than Creator is too late.