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 that can derive multiple detailed product classes.
2. An abstract factory class that can derive multiple detailed factory classes.
3. Each detailed factory class can only create an instance of a detailed product class.

Abstract Factory mode Features:

1. Multiple abstract product classes, each abstract product class can derive a number of detailed product classes.
2. An abstract factory class. The ability to derive multiple detailed factory classes.   
3. Each detailed factory class can create instances of multiple detailed product classes. This 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. The detailed factory class of the factory method pattern can only create an instance of a detailed product class. Abstract Factory mode can create multiple.
3. Factory method Create "one" product, his focus is "how to create." That is to say you develop. Your large amount of code is very likely to surround the construction of such a product, initializing these details above. Also because of this. There are very many features that can be reused between similar products. So it will be with the template method. Abstract factories need to create some column products, focusing on "what" products, that is to say. Suppose you develop, your main task is to divide the product lines of different differences and try to keep each product line interface consistent so that it can inherit from the same abstract factory.

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

Each pattern is a solution to a certain 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. Abstract Factory mode requires multiple product hierarchies.

  Before you learn a detailed example of an abstract factory, you should identify 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.

Such as. It is possible to add two integers in addition, and add two double types to form a family of products.

Understanding of these after. So how to use the abstract factory method to achieve the addition and subtraction of two integers and two double-type number of addition minus. Look at the UML class diagram first:

With UML class diagrams, it is natural to be able to write out the classes and interfaces in detail, such as the following:

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 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));}}
Operation Result:

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

This sample always feels inappropriate, and now I am in the learning phase. Not very understanding. With the learning design pattern in-depth, and so on to understand again to think about this abstract factory model, see the post of everyone, suppose there is a better explanation. Please give me about the next, study together

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.