Design Pattern: simple factory Mode

Source: Internet
Author: User

Http://www.35java.com/zhibo/forum.php? MoD = viewthread & tid = 262 & extra = Page % 3d2

Simple factory mode (also known as static factory mode), a simple factory produces finished products, while hiding the details of the products produced by the client. Define a product interface during implementation, and establish a finished product through a specific static method.

Suppose there is a music box factory. The guest who buys the music box does not need to know how to make the music box. He only needs to know how to play the music box. The above concepts are represented by the UML class diagram:

As shown in, musicboxdemo represents the customer's role. It only depends on the imusicbox interface, but does not care about the specific implementation. The actual generation of imusicbox instances is completed by musicboxfactory, use a simple program to implement the above UML class diagram:

    • Imusicbox. Java

Public interface imusicbox {
Public void play ();
}

    • Pianobox. Java

Public class pianobox implements imusicbox {
Public void play (){
System. Out. println ("playing piano music ");
}
}

    • Violinbox. Java

Public class violinbox implements imusicbox {
Public void play (){
System. Out. println ("playing violin music ^_^ ");
}
}

    • Musicboxfactory. Java

public class musicboxfactory {
Public static imusicbox createmusicbox (string name)
throws instantiationexception,
illegalaccessexception,
classnotfoundexception {
// here, the Java reflection mechanism is used to generate instances.
// However, the client does not need to worry about it.
// it will be changed later. the program here, client programs do not need to be changed
return (imusicbox) class. forname (name ). newinstance ();
}< BR >}

    • Musicboxdemo. Java

Public class musicboxdemo {
Public static void main (string [] ARGs) throws exception {
Playmusicbox (musicboxfactory. createmusicbox ("ianobox "));
Playmusicbox (musicboxfactory. createmusicbox ("violinbox "));
}

Public static void playmusicbox (imusicbox musicbox ){
Musicbox. Play ();
}
}
Because the client only depends on the imusicbox interface, even if you change the implementation method in createmusicbox () in the future, it has no impact on the client.

Let's take a look at the class structure of simple factory:

As long as the customer faces the factory, the customer depends on the product interface, and the specific product implementation can be separated from the customer, they can also be switched.

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.