The application of abstract factory pattern in Java single case mode programming with example Analysis _java

Source: Internet
Author: User

Definition: Provides an interface for creating a set of related or interdependent objects without specifying their specific classes.
Type: Creating class Mode
Class Diagram:

The difference between abstract Factory mode and factory method model
Abstract Factory mode is an upgraded version of a factory method pattern that he uses to create a set of related or interdependent objects. He differs from the factory method pattern in that the factory method pattern is targeted at a product hierarchy, while the abstract factory pattern is for multiple product hierarchy structures. In programming, a product structure usually behaves as an interface or abstract class, that is, all products provided by the factory method pattern are derived from the same interface or abstract class, and the product provided by the abstract factory pattern is derived from different interfaces or abstract classes.
In the abstract factory model, there is the concept of a product family: the so-called product family, refers to the different product hierarchy in the function of the product composed of family. The abstract factory model provides a range of products that form a family of products, while the range of products provided by the factory method is called a hierarchical structure. We still take the example of making cars to illustrate the difference between them.

In the above class diagram, the hatchback and the three vans are called two different grade structures, while the 2.0 displacement vehicles and the 2.4 displacement vehicles are called two different product families. A little more specific, 2.0 hatchback and 2.4-displacement hatchback belong to the same grade structure, the 2.0-volume three-car and the 2.4-displacement three-car belong to another grade structure, and the 2.0-car hatchback and the 2.0-displacement three-compartment car belong to the same product family, 2.4-car hatchback and 2.4-displacement three-car belong to another product family.
Understanding the concept of hierarchy and product family, we understand the difference between the factory method model and the abstract factory model, if the factory's products all belong to the same hierarchical structure, it belongs to the factory method mode; If the factory's products come from multiple hierarchies, they belong to the abstract factory pattern. In this case, If a factory model offers 2.0-displacement hatchback and 2.4-car hatchback, then he belongs to the factory method model; If a factory model is to provide two products for 2.4-engined hatchback and 2.4-car vans, then the factory model is an abstract factory model because the products he offers are of two different hierarchies. Of course, if a factory offers all four types of products, because the product is divided into two grade structure, he certainly belongs to the abstract factory model.

Example

Let's look at the abstract class example of the factory class:

Package abstractfactory;
 
Public abstract class Abstractcreator {
   
  //Create a product method public
  abstract Abstractproducta createproducta ();
   
  Create B Product method public
  abstract ABSTRACTPRODUCTB CREATEPRODUCTB ();
}

Abstract class of Product a

Package abstractfactory;
 
Public abstract class Abstractproducta {
   
  //Product A common method public
  void Sharemethod () {
    System.out.println () Business logic processing method shared by product a ... ");
   
  Product a different sub products to achieve public
  abstract void dosomething ();
   
}

Abstract class of Product B

Package abstractfactory;
 
Public abstract class ABSTRACTPRODUCTB {
   
  //Product B common method public
  void Sharemethod () {
    System.out.println () Business logic processing method shared by Product B ... ");
   
  Product B different Sub products to achieve public
  abstract void dosomething ();
   
}
  Product Line 1 of the factory implementation class

package abstractfactory;
 
public class Creator1 extends Abstractcreator {
 
  @Override public
  abstractproducta createproducta () {
    return new ProductA1 ();
  }
 
  @Override public
  ABSTRACTPRODUCTB CREATEPRODUCTB () {return
    new ProductB1 ();
  }
 
}
  Product line 2 of the factory implementation class


package abstractfactory;
 
public class Creator2 extends Abstractcreator {
 
  @Override public
  abstractproducta createproducta () {
    return new ProductA2 ();
  }
 
  @Override public
  ABSTRACTPRODUCTB CREATEPRODUCTB () {return
    new ProductB2 ();
  }
 
}

Product A1

Package abstractfactory;
 
public class ProductA1 extends Abstractproducta {
 
  @Override public
  void DoSomething () {
    System.out.println ("Business logic processing method for product A1 ...");
  }
 

Product A2

Package abstractfactory;
 
public class ProductA2 extends Abstractproducta {
 
  @Override public
  void DoSomething () {
    SYSTEM.OUT.PRINTLN ("Business logic processing for product A2 ...");
  }
 

Product B1

Package abstractfactory;
 
public class ProductB1 extends abstractproductb{
 
  @Override public
  void DoSomething () {
    SYSTEM.OUT.PRINTLN ("B1 business logic Processing method ...");
  }
 

Product B2

Package abstractfactory;
 
public class ProductB2 extends abstractproductb{
 
  @Override public
  void DoSomething () {
    System.out.println ("B2 business logic Processing method ...");
  }
 


the advantages of the abstract factory model
In addition to the advantages of the factory method model, the main advantage of the abstract factory pattern is that the product family can be constrained within the class. The so-called product family, generally more or less there is a certain correlation, abstract factory model can be within the class to define and describe the relationship of the product family, without having to introduce a new class to manage.

the drawbacks of the abstract factory model
Product family expansion will be a very laborious thing, if the product family needs to add a new product, then almost all plant classes need to be modified. Therefore, the classification of product hierarchy is very important when using abstract factory model.

Applicable Scenarios
Abstract factory patterns can be used when an object that needs to be created is a series of interconnected or interdependent product families. To be more clear, is an inheritance system, if there are multiple hierarchical structure (that is, there are multiple abstract classes), and the various hierarchical structure of the implementation of the class has a certain degree of association or constraints, you can use the abstract factory model. If there is no association or constraint between the implementation classes in each hierarchy, it is more appropriate to use multiple separate factories to create the product.

Summary
Whether it's a simple factory model, a factory approach model, or an abstract factory model, they all belong to the factory model and are very similar in form and feature, and their ultimate goal is to understand the decoupling. When used, we don't have to care about whether the pattern is Factory mode or abstract factory model, because the evolution between them is often unpredictable. Often you will find that the factory method model is clearly used, when new requirements come in, slightly modified, a new method is added, because the products in the class form the product family in different hierarchy structure, it becomes abstract Factory mode, and for abstract Factory mode, when reducing a method so that the supplied product no longer constitutes a product family, It evolved into a factory method model.
Therefore, in the use of Factory mode, only care to reduce the degree of coupling to achieve.

Related Article

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.