Use reflection to implement factory mode and reflection factory Mode

Source: Internet
Author: User

Use reflection to implement factory mode and reflection factory Mode

Requirement: the factory class generates instances of the corresponding class according to the parameter.

Example:

RoomParts. cs

Namespace ReflectionFactory {// <summary> // parts of the room products /// </summary> public enum RoomParts {Roof, Window, Pillar }}

ProductAttribute. cs

Using System; namespace ReflectionFactory {// <summary> /// this feature is used to attach a product type to indicate which product the type represents, convenient reflection usage // </summary> public class ProductAttribute: attribute {/// <summary> /// member indicating the part /// </summary> private RoomParts _ myRoomPart; public ProductAttribute (RoomParts part) {_ myRoomPart = part ;} public RoomParts RoomPart {get {return _ myRoomPart ;}}}}

ProductListAttribute. cs

Using System; namespace ReflectionFactory {// <summary> // This feature is used to attach the product interface to indicate the total number of product parts implemented, convenient reflection usage // </summary> public class ProductListAttribute: attribute {// <summary> // product Type set /// </summary> private Type [] _ myList; public ProductListAttribute (Type [] products) {_ myList = products;} public Type [] ProductList {get {return _ myList ;}}}}

IProduct. cs

Using System; namespace ReflectionFactory {// <summary> // product part interface, /// you need to add all the lists that implement this interface. /// </summary> [ProductList (new Type [] {typeof (Roof), typeof (Window ), typeof (Pillar)})] public interface IProduct {string GetName ();}}

Roof. cs

Namespace ReflectionFactory {// <summary> // Roof Type // </summary> [Product (RoomParts. roof)] public class Roof: IProduct {public string GetName () {return "Roof ";}}}

Window. cs

Namespace ReflectionFactory {// <summary> // window type // </summary> [Product (RoomParts. window)] public class Window: IProduct {public string GetName () {return "Window ";}}}

Pillar. cs

Namespace ReflectionFactory {// <summary> // column type // </summary> [Product (RoomParts. pillar)] public class Pillar: IProduct {public string GetName () {return "Pillar ";}}}

Ifacloud. cs

namespace ReflectionFactory{    public interface IFactory    {        IProduct Produce(RoomParts part);    }}

Factory. cs

Using System; using System. reflection; namespace ReflectionFactory {// <summary> // Factory type /// </summary> public class Factory: ifacloud {public IProduct Produce (RoomParts part) {// obtain attributes from the IProduct interface through reflection // obtain the list of all product parts ProductListAttribute attr = Attribute. getcustomattrict (typeof (IProduct), typeof (productlistattrict) as productlistattrict; // traverses all implementations of product part types foreach (Type type in attr. productList) {// use the launch function to find its Attribute ProductAttribute pa = Attribute. getCustomAttribute (type, typeof (ProductAttribute) as ProductAttribute; // determine whether the part is required if (pa. roomPart = part) {// use reflection to create a product part type Object product = Assembly. getExecutingAssembly (). createInstance (type. fullName); return product as IProduct ;}} return null ;}}}

FactoryManager. cs

Using System; namespace ReflectionFactory {// <summary> // factory management type /// </summary> public class FactoryManager {public static IProduct GetProduct (RoomParts part) {// there is only one Factory in total. You no longer need to select factory Factory factory = new Factory () based on the product type; IProduct product = factory. produce (part); Console. writeLine ("produced a product:" + product. getName (); return product ;}}}

Program. cs

Using System; namespace ReflectionFactory {class Program {static void Main (string [] args) {// output: a product is produced: pillar IProduct pillar = FactoryManager. getProduct (RoomParts. pillar); // output: a product is produced: roof IProduct roof = FactoryManager. getProduct (RoomParts. roof); // output: a product is produced: window IProduct window = FactoryManager. getProduct (RoomParts. window); Console. read ();}}}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.