Factory method mode

Source: Internet
Author: User

FactoryMethod mode: defines a factory interface for creating a product object, which delays the actual creation to the subclass. That is to say, the core factory category is no longer responsible for product creation, but only responsible for the interface that must be implemented by the specific factory subclass. The benefits of this abstraction are obvious. We no longer need to modify the role of a specific factory. For example, the following code is a graduating college student who helps an old man in the name of studying Lei Feng's good deeds: [csharp] // LeiFeng {public void Sweep () {Console. writeLine ("sweep");} public void Wash () {Console. writeLine ("laundry");} public void BuyRice () {Console. writeLine (" Mi") ;}// "" class, inheriting from Lei Feng class UnderGraduate: LeiFeng {}// client code static void Main (string [] args) {LeiFeng xueleifeng = new UnderGraduate (); xueleifeng. wash (); xueleifeng. sweep (); xuele Ifeng. buyRice ();} if the number of students is added, for example, three students are required to do these tasks, three "College Students Studying Lei Feng" are instantiated: [csharp] // client code static void Main (string [] args) {LeiFeng student1 = newUnderGraduate (); student1.Wash (); LeiFeng student2 = newUnderGraduate (); student2.Sweep (); leiFeng studen3 = newUnderGraduate (); studen3.BuyRice? Our middle school students can do their best. Therefore, it is not just the problem of instantiating multiple objects. So the Lei Feng factory came. [Csharp] // Lei Feng factory interface ifacloud {LeiFeng CreateLeiFeng ();} // "" factory class UnderGraduateFactory: ifacloud {public LeiFeng CreateLeiFeng () {return new UnderGraduate () ;}// "" factory class MiddleStudentFactory: ifacloud {public LeiFeng CreateLeiFeng () {return new MiddleStudent ();}} the client only needs to do this when calling: [csharp] // client code static void Main (string [] args) {// factory method mode ifacw.factory = NewUnderGraduateFactory (); // change to "middle school student". Modify LeiFeng student = factory. createLeiFeng (); student. buyRice (); student. wash (); student. sweep ();} How is it now? Isn't it easy to change to a middle school student? Through the above code, we can see that the factory method overcomes the disadvantages of a simple factory that violates the "open-closed principle" and maintains the advantages of the encapsulated object creation process, reduces the coupling between customer programs and product objects. It can be said that the factory method mode is further abstract and promoted by the simple factory mode. But there is a problem. When we add a product, we need to add a product factory class. Obviously, it will increase the additional development volume. What should we do? This is to say-"reflection" again. However, this blog will not be detailed, but will be detailed in the future abstract factory model.

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.