Design mode 04:factory Methord Factory method Mode (creation mode)

Source: Internet
Author: User

Factory Methord Factory method Mode (creation mode)

Starting from the coupling relationship
Coupling relationship directly determines the behavior of software in the face of change


-The tight coupling between the module and the module allows the software to change in response to changes in the relevant modules.


-Loose coupling between modules and modules allows the software to be replaced or changed in the face of changes, but other modules remain unchanged


The focus of the code is to understand the design pattern, but the same type of problem is solved for different code, and they are the same design pattern. It solves what kind of problem is which kind of design pattern.

The change of software requirement is part of software engineering, it is the problem that we should solve.

The module is divided into main module, sub-module
The main module part changes slowly, the sub-module part changes quickly
The abstract part changes slowly, the specific part changes quickly

Design mode can not go up to use, when it is not clear between the module and the relationship between the module, do not use.

Motive (motivation)
In the software system, it often faces the creation of "an object", and because of the change of the demand, the concrete implementation of this object often faces drastic changes, but it has a relatively stable interface.
How to deal with this change? How do you provide a "encapsulation mechanism" to isolate changes to "this volatile object" so that "other objects that depend on that object" in the system do not change as demand changes?

Intentions (Intent)
Defines an interface for creating objects, letting subclasses decide which class to instantiate. Factory method allows instantiation of a class to be deferred to subclasses-design mode GoF

Example code:

 Public Abstract class abstractcar{    publicabstractvoid  Startup ();      Public Abstract void Run ();      Public Abstract void Turn (Direction Direction);      Public Abstract void Stop ();}
Pubicclasshongqicar:abstractcar{ Public Override voidStartup () {//...    }     Public Override voidRun () {//...    }     Public Override voidTurn (Direction Direction) {//...    }     Public Override voidStop () {//...    }}
Pubicclassdongfengcar:abstractcar{ Public Override voidStartup () {//...    }     Public Override voidRun () {//...    }     Public Override voidTurn (Direction Direction) {//...    }     Public Override voidStop () {//...    }}
 Public Abstract class abstractcarfactory{    publicabstract  abstractcar createcar ();}
 Public class hongqicarfactory:abstractcarfactory{    publicoverride  Abstractcar Createcar ()    {        returnnew  Hongqicar ();    }}
 Public class dongfengcarfactory:abstractcarfactory{    publicoverride  Abstractcar Createcar ()    {        returnnew  Dongfengcar ();    }}
class cartestframework{    publicvoid  Buildtestcontext (abstractcarfactory Carfactory)    {        Abstractcar c1=carfactory.createcar ();    }     // ...}
 class   app{ public  static  void   Main () {cartestframework cartestframework  =new   Cartestframework (); Cartestframework.buildtestcontext ( new          Hongqicarfactory ()); Cartestframework.buildtestcontext ( new          Dongfengcarfactory ());  //  You can also use configuration files and reflections to resolve different new  // activator.createinstance (string TypeName)   

Several points of Factory method mode
The Factory method pattern is primarily used to isolate the coupling between the consumer of a class object and the specific type. In the face of a specific type that is constantly changing, tight coupling relationships can lead to software fragility.
The Factory method pattern usually delays the task of creating concrete objects to subclasses by object-oriented approach, thus implementing a strategy of extension (not change), which solves this tight coupling relationship better.
Factory method mode solves the requirement change of "single object", the Abstract Factory mode solves the requirement change of "series object", and the builder mode solves the requirement change of "object part".

Design mode 04:factory Methord Factory method Mode (creation 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.