Factory mode of design mode

Source: Internet
Author: User

First, a small story

Ten years ago, there was an upstart who had three cars--benz Mercedes, BMW BMW and Audi, and hired a driver to drive him. However, the nouveau riche is always a strange ride: after Benz the car with the driver said "drive Mercedes!", after the BMW he said "drive a BMW!", the Audi said "open Audi car!". You must say: "This man is sick!" he said, "just drive."

Second, classification

Factory mode is primarily to provide a transition interface for creating objects, shielding the specific process of creating objects for the purpose of increasing flexibility.

Divided into three categories:

    • Simple Factory mode
    • Factory method Mode
    • Abstract Factory mode

The simple factory model consists of:

1) Factory role: The core of the model, containing certain logical judgments

2) Abstract product roles: typically a parent class or interface for a specific product inheritance

3) Specific product roles: objects created by the factory class

A simple factory model to transform the nouveau riche car-now the upstart just need to sit in the car and say to the driver: "Drive" on it.

Abstract product role public interface car{

public void Drive (); }

Specific product roles
public class Benz implements car{

public void Drive () {System.out.println ("Driving Benz");

}}

public class BMW implements car{public void Drive () {

System.out.println ("Driving Bmw");

}。。 (Audi I will not write: P)

Factory class role public class driver{

Factory method. Note The return type is abstract product role
Public Drivercar (String s) throws Exception {//Judgment logic, return specific product roles to Clientif (S.equalsignorecase ("Benz"))

return new Benz ();
else if (s.equalsignorecase ("BMW"))

return new BMW (), .....

else throw new Exception ();

。。。 Welcome to the upstart ...

public class magnate{
public static void Main (string[] args) {

try{
Tell the driver I'm riding today.
Car car = Driver.drivercar ("Benz");//Next command: Drive
Car.drive (); .....

Disadvantage: Non-conforming to the open and closed principle of the program (opening to the extension, to modify the closure), such as: each additional vehicle, the factory class to add the corresponding business logic and judgment logic.

The factory method pattern removes the static property of the factory method in the simple Factory mode, and yes it can be inherited by the quilt class, which can share the pressure from the factory method in the simple Factory mode to the different factory subclasses in the factory method pattern.

Constitute:

1) Abstract Factory role: Specific factory role nosebleed the interface that is implemented or the parent class that must inherit.

2) Specific factory role: code that contains specific business logic, called by the application to create a specific product object

3) Abstract Product role: The parent class of a specific product integration or an implemented interface

4) Specific product roles: Examples of specific factory role creation.

In the words of the upstart business more and more, their car is more and more. This can be bitter that driver master, what car it must remember, maintenance, all have to go through him to use! So the upstart sympathy he said: "Look at you and me for so many years, you do not have to work so hard, I assign you a few people, you just keep them on the line!" then the management of the factory method pattern appeared

Abstract product roles, specific product roles similar to the simple factory model, just become more complex, here slightly. Abstract Factory role
public interface driver{

Public Car Drivercar ();}

public class Benzdriver implements Driver{public Car Drivercar () {

return new Benz ();}

}
public class Bmwdriver implements driver{

Public Car Drivercar () {return new BMW ();

}}

should be the corresponding relationship with the specific products ...//please, Mr. Upstart.

public class magnate

{
public static void Main (string[] args) {

try{
Driver Driver = new Benzdriver (); Car car = Driver.drivercar (); car.drive ();

}......

}

Factory mode of design 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.