The Java design pattern--------The abstract factory pattern for creating patterns

Source: Internet
Author: User

This article is your own study notes, welcome reprint, but please specify the source:http://blog.csdn.net/jesson20121020

The factory method pattern and the abstract factory model are not well differentiated:

Factory method Pattern Features:

1. An abstract product class, you can derive a number of specific product classes.
2. An abstract factory class that can derive a number of specific factory classes.
3. Each specific factory class can only create an instance of a specific product class.

Abstract Factory mode Features:

1. Multiple abstract product classes, each abstract product class can derive a number of specific product classes.
2. An abstract factory class that can derive a number of specific factory classes.   
3. Each specific factory class can create multiple instances of a specific product class, which is the creation of multiple products under a product line.

The difference between the two:

1. The factory method pattern has only one abstract product class, and the abstract factory pattern has multiple.
2. Factory method mode the specific factory class can only create an instance of a specific product class, while the abstract factory pattern may create multiple.
3. Factory method Create "one" product, his focus is "how to create", that is, if you develop, your large number of code is likely to revolve around the structure of this product, the initialization of these details. Because of this, there are a lot of features that can be reused between similar products, so it will work with the template approach. Abstract factories need to create some column products, focusing on "what" products, that is, if you develop, your main task is to divide the product line of different differences, and try to keep each product line interface consistent, so that can inherit from the same abstract factory.

Or the above example, that is, arithmetic to illustrate, assuming such a situation, now not only to achieve the two double type of the addition and subtraction, but also to achieve the addition and subtraction between two integers, that is, to increase the original base of two integer number of the addition and subtraction.

Each model is a solution to a problem. The biggest difference between the abstract factory model and the factory method pattern is that the factory method model is for a product hierarchy, while the abstract factory model requires multiple product hierarchies.

  Before you learn the concrete examples of an abstract factory, you should understand two important concepts: product family and product grade.

The so-called product family, is located in the different product hierarchy structure, the function is related to the product composition family. For example, you can add two integers in an addition, and add two double to form a family of products .

After understanding these, then how to use the abstract factory method to achieve the addition and subtraction of two integers and two double type of the number of addition and subtraction, first look at the UML class diagram:

With UML class diagrams, it is natural to write the classes and interfaces as follows:

Abstract Factory mode application steps:

1. Create an int type and double type operation interface respectively

Public interface Intoperation {public int Operate (int a,int b);}

Public interface Doubleoperation {public double Operate (double a,double b);}

2. Create two types of add and subtract implementation classes, respectively

public class Intaddoperation implements intoperation {@Overridepublic int Operate (int a, int b) {//TODO auto-generated Me Thod Stubreturn a+b;}}
public class Intsuboperation implements intoperation {@Overridepublic int Operate (int a, int b) {//TODO auto-generated Me Thod Stubreturn A-B;}}
public class Doubleaddoperation implements Doubleoperation {@Overridepublic double Operate (double A, double b) {//TODO Au To-generated method Stubreturn a+b;}}
public class Doublesuboperator implements Doubleoperation {@Overridepublic double Operate (double A, double b) {//TODO Aut O-generated method Stubreturn A-B;}}

3. Creating an abstract Factory interface

Public interface Operationfactory {public intoperation createintoperation ();p ublic doubleoperation Createdoubleoperation ();}

4. Creating a factory implementation class for addition and subtraction

public class Addoperationfactory implements Operationfactory {@Overridepublic intoperation createintoperation () {// TODO auto-generated Method Stubreturn new Intaddoperation ();} @Overridepublic doubleoperation createdoubleoperation () {//TODO auto-generated method Stubreturn New Doubleaddoperation ();}}
public class Suboperationfactory implements Operationfactory {@Overridepublic intoperation createintoperation () {// TODO auto-generated Method Stubreturn new Intsuboperation ();} @Overridepublic doubleoperation createdoubleoperation () {//TODO auto-generated method Stubreturn New Doublesuboperator ();}}
5. Create a test class

public class Operationtest {/** * @param args */public static void main (string[] args) {//TODO auto-generated method stub The addition plant is responsible for the production of two integers and the addition of two double-type operationfactory of1 = new Suboperationfactory (); Intoperation Intadd = Of1.createintoperation ();D oubleoperation doubleadd = Of1.createdoubleoperation (); System.out.println ("10+25=" +intadd.operate (10, 25)); System.out.println ("10.2+25.3=" +doubleadd.operate (10.2, 25.3));//subtraction Factory is responsible for the production of two integer subtraction, And a subtraction of two double types operationfactory of2 = new Addoperationfactory (); Intoperation intsub = Of2.createintoperation ();D Oubleoperation doublesub = Of2.createdoubleoperation (); System.out.println ("10-25=" +intsub.operate (10, 25)); System.out.println ("10.2-25.3=" +doublesub.operate (10.2, 25.3));}}
Execution Result:

10+25=-1510.2+25.3=-15.10000000000000110-25=3510.2-25.3=35.5

This example always feel inappropriate, at present I also in the study stage, not very understand, with the study design pattern in depth, and so on to think again to consider this abstract Factory mode, see the post of everyone, if there is a better explanation, also please give me probably say, common study

The Java design pattern--------The abstract factory pattern for creating patterns

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.