Factory Method)

Source: Internet
Author: User

Factory Method: defines an interface used to create an object, so that the subclass decides which class to instantiate. The Factory Method delays the instantiation of a class to its subclass. When implementing the factory method mode, the client needs to decide which factory to instantiate and which class object to create, that is, in the factory method mode, the internal logic judgment of the simple factory is moved to the client code. Originally, the factory class is changed, but now the client is modified. In general, the factory method pattern fixes the open-closed principle that is not followed in a simple factory. UML class diagram of Factory Method: the biggest advantage of the simple Factory mode is that the Factory class contains the necessary logical judgment, and the related class is dynamically instantiated according to the customer's selection conditions, for the client, remove the dependencies with specific products. However, in the face of function expansion, the factory class must be modified when adding a function class in the simple factory mode, which violates the open-closed principle; the factory method mode solves this problem. From the structure diagram, we can see that the factory method class is added to the factory method mode, and the factory method implements instantiation of specific function class objects, you do not need to modify the internal code when extending the function. Example of the Factory Method Application: (1) specific product object interface [csharp] using System; using System. collections. generic; using System. linq; using System. text; namespace FactoryMethod2 {// interface for defining the object created by the factory method abstract class LeiFeng {public abstract void buyRice (); public abstract void wash (); public abstract void sweep ();} class Student: LeiFeng {public override void buyRice () {Console. writeLine ("students learn Lei Feng to buy rice... "); // throw new NotImplementedException ();} public override void wash () {Console. writeLine ("students learn Lei Feng laundry... "); // throw new NotImplementedException ();} public override void sweep () {Console. writeLine ("Student Lei Feng cleaning... "); // throw new NotImplementedException () ;}} class Volunteer: LeiFeng {public override void buyRice () {Console. writeLine ("volunteer learns Lei Feng to buy rice... "); // throw new NotImplementedException ();} public override void was H () {Console. writeLine ("volunteers learn Lei Feng laundry... "); // throw new NotImplementedException ();} public override void sweep () {Console. writeLine ("volunteer Lei Feng cleaning... "); // throw new NotImplementedException () ;}}( 2) Factory method [csharp] using System; using System. collections. generic; using System. linq; using System. text; namespace FactoryMethod2 {// declare the factory method. This method returns a LeiFeng type object abstract class ifacloud {public abstract LeiFeng createLeiFeng ();} class StudentFactory: ifacloud {public override LeiFeng createLeiFeng () {return new Student (); // throw new NotImplementedException () ;}} class VolunteerFactory: ifacloud {public override LeiFeng createLeiFeng () {return new Volunteer (); // throw new handle ();}}} (3) client code [csharp] using System; using System. collections. generic; using System. linq; using System. text; namespace FactoryMethod2 {class Program {static void Main (string [] args) {ifac1_ifactory1 = new StudentFactory (); LeiFeng leifeng1 = require (); leifeng1.buyRice (); lift (); leifeng1.sweep (); IFactory iFactory2 = new VolunteerFactory (); LeiFeng leifeng2 = lift (); leifeng2.wash (); leifeng2.sweep (); Console. read () ;}}( 4) running result

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.