Simple factory and factory method mode

Source: Internet
Author: User

Simple Factory mode

The simple factory model is also called the Static factory method mode. Renaming can tell that this pattern must be simple. The purpose of its existence is simple: Define an interface for creating objects.

Let's take a look at its composition:

1) Factory class role: This is the core of this model, contains certain business logic and judgment logic. In Java it is often implemented by a specific class.

2) Abstract Product role: It is generally the product of the specific inheritance of the parent class or implementation of the interface. Implemented in Java by an interface or an abstract class.

3) Specific product roles: the object created by the factory class is an instance of this role. Implemented in Java by a concrete class.

Code:

Abstract Product Roles

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");
}
}

Factory class Roles

public class driver{

Factory method. Note The return type is abstract product role
Publicstatic Car Drivercar (String s) throws exception{
Judgment logic, return the specific product role to the client
if (S.equalsignorecase ("Benz"))
Returnnew Benz ();
ElseIf (S.equalsignorecase ("BMW"))
Returnnew BMW ();

}

}

Test

public class magnate{

publicstatic void Main (string[] args) {

Car Car =driver.drivercar ("Benz");

Car.drive ();

}

}

Factory method Mode

The factory method pattern removes the static property of the factory method in the simple Factory mode, allowing it to inherit from the quilt class. The pressure to concentrate on factory methods in a simple factory model can be shared by different factory subclasses in the factory method model.

Let's take a look at its composition:

1) Abstract Factory role: This is the core of the factory method pattern, which is independent of the application. is the interface that the specific factory role must implement or the parent class that must inherit. In Java it is implemented by an abstract class or interface.

2) Specific factory role: it contains code related to the specific business logic. Called by the application to create an object that corresponds to a specific product.

3) Abstract Product role: It is the parent of a specific product inheritance or an interface implemented. In Java, there are generally abstract classes or interfaces to implement.

4) Specific product roles: the object created by the specific factory role is an instance of this role. Implemented in Java by a specific class.

Abstract Product Roles

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");
}
}

Abstract Factory role

public interface driver{
Public Car Drivercar ();
}

Specific Factory roles

public class Benzdriver implements driver{
Public Car Drivercar () {
return new Benz ();
}
}

public class Bmwdriver implements driver{
Public Car Drivercar () {
return new BMW ();
}
}

Test

public class magnate{

publicstatic void Main (string[] args) {

Driver driver= new Benzdriver ();

Carcar = Driver.drivercar ();

Car.drive ();

}

}

Summarize:

1) Simple Factory mode is a specific class to create instances of other classes, the parent class is the same, the parent class is specific.

2) The factory method pattern is that there is an abstract parent class that defines the public interface, and the subclass is responsible for generating the concrete object, which is intended to defer the instantiation of the class to the child class.

Simple factory and factory method 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.