Design Pattern Learning Notes (10)-factory method mode

Source: Internet
Author: User
Tags abstract

The book "Design Patterns" describes this in the factory method model:

Defines an interface for creating objects, letting subclasses decide which class to instantiate. FactoryMethod delays the instantiation of a class to its subclasses.

My understanding: The Fatorymethod pattern is a creation pattern, and defining an interface for creating objects means that we define an interface for creating objects (or abstract classes, which are actually abstract factory abstractfactory), It has an internal method of creating objects, and the return value of this method is the type of an interface (or abstract class), which is the FactoryMethod Let subclasses decide which class to instantiate which means that we want to define a subclass of the interface (or abstract class) that implements the creation object (the specific factory class Concretefactory), and let subclasses decide which instance of the specific type of object to create (implement FactoryMethod). Here's an example of the following code:

abstract class ballfactory{
protected abstract Ball makeball ();//factory Method
}
Class Basketballf ACT extends ballfactory{
Public Ball Makeball () {//Subclass implementation Factory method determines which class to instantiate
return new basketball ();
}
}
Class Footballfact extends ballfactory{
public Ball Makeball () {//Subclass implementation Factory method determines which class to instantiate
return New Football ();
}
}
Class Basketball extends ball{
public void Play () {
System.out.println (' Play the Basketball ');

}
Class Football extends ball{
public void Play () {
System.out.println ("Play the Football");
}
}
Abstract class ball{
protected abstract void play ();
}  Class test{
public static void Main (string[] args) {
Ballfactory ballfactory=new basketballfact ();
Ball Basketball=ballfactory.makeball ();
Basketball.play ();  

Ballfactory=new footballfact ();
Ball Football=ballfactory.makeball ();
Football.play ();
}
}

The output is as follows:

Play the Basketball

Play the Football

This pattern has been used in the abstract Factory mode, except that it was not known to have factory method, the Getdisplaydrvr () and Getprintdrvr () of the Resfactory class in my example. is actually the factory method. Specifically, see design mode learning Notes (v)-abstract factory abstract factory model.

Summary: The Factory method pattern is a very common pattern. It mainly applies to:

1 when a class does not know the class of the object it must create.

2 When a class wants its subclasses to specify the objects it creates.

3 completes the task of creating objects when the class delegates the responsibility of creating the object to one of several helper subclasses.

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.