Abstract Factory mode

Source: Internet
Author: User

Definition of the abstract factory pattern

Abstract Factory mode ( Abstract Factory pattern) is a more commonly used model, which is defined as follows:

Provide an interface for creating families of related or dependent objects without specifying their

Concrete classes. (Provides an interface for creating a set of related or interdependent objects without specifying their specific classes.) )

Abstract Factory mode is an upgraded version of the factory method pattern, with multiple business symbols, business classifications, and abstract

Factory mode produces the desired object is a very good way to solve. Let's look at the generic source code for the abstract factory, the first

First, there are two product lines (also called product families) that affect each other.

Abstract Product class

Public abstract class Abstractproducta {

Methods common to each product

public void Sharemethod () {

}

Same method for each product, different implementations

public abstract void dosomething ();

}

Two specific product implementation classes such as

Product Implementation class of A1

public class ProductA1 extends Abstractproducta {

public void dosomething () {

System.out.println ("Method of realization of Product A1");

}

}

Product Implementation class of A2

public class ProductA2 extends Abstractproducta {

public void dosomething () {

System.out.println ("Method of realization of Product A2");

}

}

Product b Similar to this, do not repeat. Abstract Factory class Abstractcreator's job is to define what each factory is going to achieve.

function, in general code, the abstract factory class defines product creation for two product families, such as code

Abstract Factory class

Public abstract class Abstractcreator {

Create a product family

Public abstract abstractproducta createproducta ();

Create a B product family

Public abstract ABSTRACTPRODUCTB CREATEPRODUCTB ();

}

Note there N product families, there should be N creation methods in the abstract factory class.

How to create a product is done by a specific implementation class, Creator1 and Creator2 such as code

Implementation class for product level 1

public class Creator1 extends Abstractcreator {

Only a product with a product grade of 1 is produced

Public Abstractproducta createproducta () {

return new ProductA1 ();

}

Only B products with a product grade of 1 are produced

Public ABSTRACTPRODUCTB CREATEPRODUCTB () {

return new ProductB1 ();

}

}

Implementation class for product level 2

public class Creator2 extends Abstractcreator {

Only a product with a product grade of 2 is produced

Public Abstractproducta createproducta () {

return new ProductA2 ();

}

Only B products with a product grade of 2 are produced

Public ABSTRACTPRODUCTB CREATEPRODUCTB () {

return new ProductB2 ();

}

}

Note there M product grade should have m to implement the factory class, in each implementation plant, the implementation of different product families

of production tasks.

How do you produce an object that is not related to implementation in a specific business? such as code

Scene class

public class Client {

public static void Main (string[] args) {

Define two plants

Abstractcreator Creator1 = new Creator1 ();

Abstractcreator Creator2 = new Creator2 ();

Generating A1 objects

ABSTRACTPRODUCTA a1 = Creator1.createproducta ();

Generating A2 objects

Abstractproducta A2 = Creator2.createproducta ();

Generating B1 objects

ABSTRACTPRODUCTB B1 = CREATOR1.CREATEPRODUCTB ();

Generating B2 objects

ABSTRACTPRODUCTB b2 = CREATOR2.CREATEPRODUCTB ();

/*

* Then you can do whatever you like here ...

*/

}

}

In a scene class, there is no one method that is related to the implementation class, and for a product we just need to know it

Factory method can directly produce a product object, without concern for its implementation class.

Advantages of the abstract factory model

encapsulation, the implementation of each product class is not a high-level module to be concerned about, what is it to care about? Is the interface, is

Abstract, it doesn't care how objects are created

The constraints within the product family are non-public.

disadvantages of the abstract factory model

The biggest drawback of abstract Factory mode is that product family expansion is very difficult

The usage scenario definition for the abstract factory pattern is very simple: an object family (or a group of objects without any relationship)

Have the same constraints, you can use the abstract factory pattern.

Considerations for Abstract Factory mode

In the shortcomings of the abstract factory model, we refer to the product family expansion of the abstract factory model is difficult, but must

Clearly, it is difficult to expand the product family, not the product grade. In this mode, the product level is very easy to expand, increasing

Add a product level, as long as a factory class is responsible for the newly added product production tasks. That means landscape.

Easy to extend, difficult to scale vertically.

Abstract Factory mode

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.