"Design pattern Refinement" Learning notes (15)------FactoryMethod (Factory method) mode

Source: Internet
Author: User

"Design pattern Refinement" Learning notes (15) ------FactoryMethod (Factory method) mode

GOF : defines a class that is used to create an object interface, so that subclasses decide which classes to instantiate. FactoryMethod delays the instantiation of a class to its subclasses.

The FactoryMethod pattern should be studied in conjunction with the abstract factory model, in order to follow the sequence of the book "Design Pattern Refinement". As with the abstract factory pattern, the FactoryMethod pattern is also defined as a series of identical objects. At some point it is necessary to create an existing class structure that wants to correspond to a layered class structure and let the new system contain the responsibilities of some agents. For example, a graphics system, Circle, Square is a hierarchical class structure. When we use this class object, we don't have to define and instantiate each object, I just need an agent system to generate these objects for us. Then we can use FactoryMethod mode to achieve this goal.

FactoryMethod patterns are often used in defining the process of a framework. This is because the framework exists at an abstract level. Often, they do not know and should not be concerned with instantiating a particular object. They need to defer the decision of a particular object to the user of the framework.

the following are standard simplified UML Figure:

Here is an implementation example , and we still use the shape example to explain the explanation. (I have to use the shape too quickly, beginners see here can never think that OO can only solve the problem of shape, oo strong is not I can casually comment, hehe.) )

Package FactoryMethod;

Public abstract class Shapefactory

{

public abstract void Draw ();

public static shapefactory shapefactory (String shape)//used to produce objects

{

if (Shape.equals ("Circle")) {

return new Circle ();

}

else if (shape.equals ("Square")) {

Return to New Square ();

}

Else

return null;

}//end shapefactory (...)

}//end Sbstract class Shapefactory

Package FactoryMethod;

public class Circle extends Shapefactory

{

public void Draw ()

{

System.out.println ("Draw a circle!");

}//end Draw ()

}//end class Circle

Package FactoryMethod;

public class Square extends Shapefactory

{

public void Draw ()

{

System.out.println ("Draw a square!");

}//end Draw ()

}//end class Square

Package FactoryMethod;

public class FactoryMethod

{

Private shapefactory shape;

public void Showfactorymethod ()

{

string[] str = {"Circle", "Square", "shape"};

for (int i = 0; i < str.length; ++i) {

Shape = Shapefactory.shapefactory (Str[i]);

if (Shape!= null)

Shape.draw ();

}

}//end Showfactorymethod ()

public static void Main (string[] args)

{

System.out.println ("The Factory Method pattern!");

FactoryMethod fm = new FactoryMethod ();

Fm.showfactorymethod ();

}//end Main (...)

}//end class FactoryMethod

The FactoryMethod pattern is a very intuitive pattern that will be reused in practice. It applies to situations where you want to defer the rule of an object instantiation to a derived class. In such a case, the most natural approach is to place the implementation of the method in the object responsible for the behavior.

trackback:http://tb.blog.csdn.net/trackback.aspx?postid=954070  

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.