Factory methods and abstract factory models in Java

Source: Internet
Author: User

1. Concept

Factory method: an abstract product class derives from multiple specific product classes. An abstract factory class derives from multiple specific factory classes. Each factory class can only create an instance of a specific product class.

Define an interface for creating an object (that is, an abstract factory class), and let its subclass (a specific factory class) decide which class to instantiate (a specific product class ). "One-to-one" relationship.

 

Abstract Factory: Multiple abstract product classes are derived from multiple specific product classes. One abstract factory class is derived from multiple specific factory classes. Each factory class can create instances of multiple specific product classes.

That is, an interface is provided to create a series of related or mutually dependent objects without specifying their specific classes. "One-to-many" relationship.

2. UML

Factory method:

 

 

Abstract Factory:

3.Code

Factory method:

 

 Public   Interface Product
{
}

Public Interface Creator
{
Public Product Factory ();
}

Public Class Concreteproduct1 Implements Product
{
Public Concreteproduct1 ()
{
System. Out. println ("concreteproduct1 created ");
}
}

Public Class Concreteproduct2 Implements Product
{
Public Concreteproduct2 ()
{
System. Out. println ("concreteproduct2 created ");
}

}

Public Class Concretecreator1 Implements Creator
{
Public Product Factory ()
{
Return New Concreteproduct1 ();
}
}

Public Class Concretecreator2 Implements Creator
{
Public Product Factory ()
{
Return New Concreteproduct2 ();
}
}

Public Class Client
{
Private Static Creator creator1, creator2;
Private Static Product prod1, prod2;

Public Static Void Main (string [] ARGs)
{
Creator1 = New Concretecreator1 ();
Prod1 = creator1.factory ();
System. Out. println ("----------------------------");
Creator2 = New Concretecreator2 ();
Prod2 = creator2.factory ();
}
}

Abstract Factory:

 

 //  Define certain standards for different products and implement them using interfaces
// The method () method can be used to extract the commonalities of different products. For example, mobile phones have similar functions.
Interface Iproducta {
Public Void Method ();
}

Interface Iproductb {
Public Void Method ();
}

// A series of specific products that implement product standards
// Because A1 has been designed to be produced by vendor 1, the following output code is "vendor X"
Class Producta1 Implements Iproducta {
Public Void Method (){
System. Out. println ("manufacturer 1 produces producta1 ...");
}
}

Class Producta2 Implements Iproducta {
Public Void Method (){
System. Out. println ("vendor 2 produces producta2 ...");
}
}

Class Productb1Implements Iproductb {
Public Void Method (){
System. Out. println ("manufacturer 1 produces productb1 ...");
}
}

Class Productb2 Implements Iproductb {
Public Void Method (){
System. Out. println ("vendor 2 produces productb2 ...");
}
}

// Each brand of product production factory, that is, different manufacturers are responsible for the production of their own brand of Products
Abstract Class Factory1 {
Abstract Iproducta getproducta1 ();
Abstract Iproductb getproductb1 ();
}

Abstract Class Factory2 {
Abstract Iproducta getproducta2 ();
Abstract Iproductb getproductb2 ();
}

// The specific factory is used to produce related products
Class Concretefactory1 Extends Factory1 {
Public Iproducta getproducta1 (){
Return New Producta1 ();
}
Public Iproductb getproductb1 (){
Return New Productb1 ();
}
}

Class Concretefactoryb Extends Factory2 {
Public Iproducta getproducta2 (){
Return New Producta2 ();
}
Public Iproductb getproductb2 (){
Return New Productb2 ();
}
}

// Test class
Public Class Client {
Public Static Void Main (string [] ARGs ){
// Manufacturer 1 is responsible for producing products A1 and B1
Factory1 factory1 = New Concretefactory1 ();
Iproducta producta1 = factory1.getproducta1 ();
Iproductb productb1 = factory1.getproductb1 ();

Producta1.method ();
Productb1.method ();

// Manufacturer 2 is responsible for producing products A2 and B2
Factory2 factory2 = New Concretefactoryb ();
Iproducta producta2 = factory2.getproducta2 ();
Iproductb productb2 = factory2.getproductb2 ();

Producta2.method ();
Productb2.method ();
}
}

4. Application scenarios

Factory method:

The factory method mode is applicable in the following cases:

(1) When a class does not know the class of the object it must create.

(2) When a class wants its subclass to specify the object it creates.

(3) When the class delegates the responsibility of creating an object to one of multiple help sub-classes, and you want to localize the information of which help sub-classes are proxies.

Abstract Factory:

(1) A system should not depend on the details of how product instances are created, combined, and expressed. This is important for all forms of factory models.

(2) This system has more than one product family, and the system only consumes one of them.

(3) products belonging to the same product family are used together. This constraint must be reflected in the system design.

(4) The system provides a product library. All products use the same interface, so that the client does not rely on implementation.

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.