Simple factory and factory model in factory Mode

Source: Internet
Author: User
Tags ibase

Simple factory and factory model in factory Mode

Factory mode definition: instantiate an object and use the factory method instead of the new operation.

Interface (IBase):

public interface IBase{    void Excute();}

Implementation Class A (BaseAImpl ):

Public class BaseAImpl: IBase {public void Excute () {Type type = this. getType (); Console. writeLine ("Class A Name: {0}, Class Name: {1}, whether to implement the interface IBase: {2}", type. fullName, type. name, typeof (IBase ). isAssignableFrom (type ));}}

Implementation Class B (BaseBImpl ):

Public class BaseBImpl: IBase {public void Excute () {Type type = this. getType (); Console. writeLine ("Class B Full name: {0}, Class Name: {1}, whether to implement interface IBase: {2}", type. fullName, type. name, typeof (IBase ). isAssignableFrom (type ));}}

Class C (BaseC): (Special Class for difference)

Public class BaseC {public void Excute () {Type type = this. getType (); Console. writeLine ("Class A Name: {0}, Class Name: {1}, whether to implement the interface IBase: {2}", type. fullName, type. name, typeof (IBase ). isAssignableFrom (type ));}}

Factory ):

/// <Summary> /// Factory class /// </summary> public class Factory {// <summary> // Factory method (where the instance is created, what do you need) /// </summary> /// <param name = "fullName"> type full name </param> /// <returns> implement IBase Instance Object </returns> public IBase CreateBase (string fullName) {// obtain all types of Type [] types = this. getType (). assembly. getTypes (); // traverse all types and search for foreach (Type type in types) {// determine whether all types are named and whether the IBase interface if (type. fullName = fullName & typeof (IBase ). isAssignableFrom (type) return Activator. createInstance (type) as IBase;} return null ;}}

Demo result:

Class Program {static void Main (string [] args) {Factory factory = new Factory (); IBase instanceA = factory. createBase (typeof (BaseAImpl ). fullName); IBase instanceB = factory. createBase (typeof (BaseBImpl ). fullName); IBase instanceC = factory. createBase (typeof (BaseC ). fullName); // special example for distinguishing instanceA. excute (); instanceB. excute (); if (instanceC = null) {Console. writeLine ("Class C: {0} has not implemented the interface IBase", typeof (BaseC ). fullName);} Console. readLine ();}}

 

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.