Factory method Pattern of design pattern

Source: Internet
Author: User

Factory method Mode

Defines an interface for creating objects, letting subclasses decide which class to instantiate. The factory method defers the instantiation of a class to a subclass.

Operation class
 PackageCom.hml.factory.method; Public Abstract classOperation {Private intNumbera; Private intNumberb;  Public intGetnumbera () {returnNumbera; }     Public voidSetnumbera (intNumbera) {         This. Numbera =Numbera; }     Public intGetnumberb () {returnNumberb; }     Public voidSetnumberb (intNumberb) {         This. Numberb =Numberb; }     Public Abstract intGetResult ();}
Addoperation class
 Package Com.hml.factory.method;  Public class extends Operation {    publicint  getresult () {        return Getnumbera () + Getnumberb ();    }}
Suboperation class
 Package Com.hml.factory.method;  Public class extends Operation {    publicint  getresult () {        return Getnumbera ()- Getnumberb ();    }}
Ifactory class
 Package Com.hml.factory.method;  Public Interface ifactory {    public  Operation CreateOperation ();}
Addoperationfacotry class
 Package Com.hml.factory.method;  Public class Implements ifactory {    public  Operation CreateOperation () {        returnnew addoperation ();    }}
 Package Com.hml.factory.method;  Public class Implements ifactory {    public  Operation CreateOperation () {        returnnew suboperation ();    }}
Class diagram

When the factory method pattern is implemented, the client needs to decide which factory to instantiate to implement the Operation class, the problem of choosing judgment is still there, that is, the factory method moves the internal logic judgment of the simple factory to the client code, wants to add the function, it is to modify the factory class, and now is to modify the client code.

Factory method Pattern of design pattern

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.