Java Design Pattern Learning three------Factory mode

Source: Internet
Author: User

Factory mode

Factory mode (Factory pattern) is one of the most common design patterns in Java. This type of design pattern belongs to the Create pattern, which provides an optimal way to create an object.

In Factory mode, creating an object does not expose the creation logic to the client, and it is by using a common interface to point to the newly created object.

The idea of a factory model: Define an interface that creates an object, letting its subclasses decide which factory class to instantiate, and the factory pattern to defer its creation to subclasses. Use the process to do the interface selection.

Advantages: 1, a caller to create an object, as long as the name can be known. 2, high scalability, if you want to add a product, as long as the expansion of a factory class can be. 3, the specific implementation of the shielding products, the caller value care of the interface of the product.

Disadvantage: Each time you add a product, you need to add a specific class and object to implement the factory, so that the number of classes in the system multiplied, to a certain extent, increase the complexity of the system, but also increase the system specific class dependency. That's not a good thing.

Usage Scenario: 1, Logger: Records may be logged to local hard disk, system events, remote server, etc., the user can choose where to log logs.

As a way to create a class pattern, you can use the factory method pattern in any place where complex objects need to be generated. Complex objects are suitable for Factory mode, while simple objects do not need to use Factory mode. If the Factory mode is introduced, the complexity of the system is increased.

The first is the interface of a factory class, then the concrete class that will be used later, these concrete classes need to inherit this interface, there is no construction method, and then create a factory class to get the object of these concrete classes, and the member method of this factory class can get the object corresponding to the specific class according to the parameters. The creation process is sufficient, and later use only the objects that create the factory class, you can create objects of the specific class.

Java Design Pattern Learning three------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.