2 Simple Factory mode

Source: Internet
Author: User

   Public classCreatefactory { Public StaticIcreate Generatorcreate (stringcreatename) {            Switch(createname) { Case "people":                    return Newpeople ();  Case "Animal":                    return NewAnimal (); default:                    Throw Newnotimplementedexception (); }        }    }    /// <summary>    ///human implementation of factory interfaces/// </summary>     Public classPeople:icreate {#regionIcreate Members Public stringCreate () {return "Create human"; }        #endregion    }    /// <summary>    ///Animal implements factory interface/// </summary>     Public classAnimal:icreate {#regionIcreate Members Public stringCreate () {return "Creating Animals"; }        #endregion    }    /// <summary>    ///Simple Factory interface, return value type/// </summary>     Public Interfaceicreate {stringCreate (); }
View Code

Simple factory: Returns an instance of a class based on the data supplied to it, usually the class it returns has a common parent class (or interface object)
The core of a simple factory is an instantiated object, a simple factory-instantiated class with the same interface or base class, in which the subclasses are relatively fixed and do not need to be extended, can use a simple factory,
The advantage of a simple factory is that it can obtain the corresponding instance according to the parameters, avoids the direct instantiation, reduces the coupling,
The disadvantage is that the types that can be instantiated are already determined during compilation, and if you add new types, you need to modify the factory, not the OCP (open and close principle), the simple factory needs to know all the types to be generated, when there are too many subclasses, or too many sub-levels are not suitable for simple factory use

Call:

Createfactory.generatorcreate ("people"). Create ()

2 Simple Factory mode

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.