Java Design Pattern -------- abstract factory pattern of creation Pattern

Source: Internet
Author: User
Tags integer numbers

Java Design Pattern -------- abstract factory pattern of creation Pattern

This article is my learning notes, welcome to reprint, but please note the Source: http://blog.csdn.net/jesson20121020

The factory method mode and the abstract factory mode are difficult to distinguish clearly:

Factory method Mode features:

1. An abstract product class that can be derived from multiple specific product classes.
2. An abstract factory class can be derived from multiple factory classes.
3. Each factory class can only create one product class instance.

Abstract Factory model features:

1. Multiple abstract product classes. Each abstract product class can be derived from multiple specific product classes.
2. An abstract factory class can be derived from multiple factory classes.
3. You can create multiple product instances for each specific factory category, that is, you can create multiple products under a product line.

Differences between the two:

1. The factory method mode has only one abstract product class, while the abstract factory mode has multiple.
2. The factory method mode can only create one instance of a specific product class, while the abstract factory mode can create multiple instances.
3. the key to creating a "product" in the factory method is "How to create". That is to say, if you develop a product, a large amount of code may be structured around this product, initialize these details. Because of this, similar products have many reusable features, so they will be consistent with the template method. Abstract factories need to create products with the focus on "what to create" products. That is to say, if you develop, your main task is to divide different product lines, and try to keep the interfaces of each product line consistent, so that they can be inherited from the same abstract factory.

The preceding example is used to describe the four arithmetic operations. In this case, we not only need to add and subtract two double-type numbers, but also to add and subtract two integers, that is, add the addition and subtraction of Two integer numbers on the basis of the original one.

Each mode is a solution to certain problems. The biggest difference between the abstract factory mode and the factory method mode is that the factory method mode is for a product level structure, while the abstract factory mode needs to face multiple product level structures.

Before learning the specific example of an abstract factory, we should understand two important concepts: Product Family and product level.

A product family is a family of products with functions associated with different product levels. For example, you can add two integer numbers in the addition, and add two double numbers to form a product family.

After understanding this, how can we use the Abstract Factory method to add and subtract two integers and two double-type values? First, let's look at the UML class diagram:

With the UML class diagram, You can naturally write various classes and interfaces as follows:

Abstract Factory mode application steps: <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> Vc3ryb25np1_vcd4kpgjsb2nrcxvvdgu + release/release + CjxwcmUgY2xhc3M9 "brush: java;"> public interface operation peration {public int Operate (int a, int B );}

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

2. Create two types of addition and subtraction implementation classes respectively

public class IntAddOperation implements IntOperation {@Overridepublic int Operate(int a, int b) {// TODO Auto-generated method stubreturn a+b;}}
public class IntSubOperation implements IntOperation {@Overridepublic int Operate(int a, int b) {// TODO Auto-generated method stubreturn a-b;}}
public class DoubleAddOperation implements DoubleOperation {@Overridepublic double Operate(double a, double b) {// TODO Auto-generated method stubreturn a+b;}}
public class DoubleSubOperator implements DoubleOperation {@Overridepublic double Operate(double a, double b) {// TODO Auto-generated method stubreturn a-b;}}

3. Create an abstract factory Interface

public interface OperationFactory {public IntOperation createIntOperation();public DoubleOperation createDoubleOperation();}

4. Create 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 factory is responsible for producing the addition of two integer values and two double values. OperationFactory of1 = new SubOperationFactory (); operation peration IntAdd = of1.createoperation peration (); DoubleOperation 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); // The subtraction factory is responsible for producing the subtraction of two integers and the subtraction of two double numbers OperationFactory of2 = new AddOperationFactory (); operation peration IntSub = of2.createoperation peration (); DoubleOperation 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 is not very appropriate. I am still at the learning stage, but I am not very familiar with it. With the deep understanding of the design model, I will think about this abstract factory model again, if you have a better explanation of this post, please give me a rough picture and learn together.

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.