Apply the design pattern in Java -- Factory Method

Source: Internet
Author: User
Tags abstract
In the design mode, the Factory Method is also relatively simple, but it is widely used. The shadows of this mode can be seen in EJB, RMI, COM, CORBA, and Swing, it is one of the most important models. in many places, we will see the class named xxxFactory. So, what is Factory Method? Why should we use this mode? How can we use Java to implement this mode, this is what this article will bring to you.
Basic concepts
Factory Method is a creation mode. It defines an interface for creating objects, but lets subclasses decide which class to instantiate. the Factory Method mode is used when a class cannot predict the type of object to be created or when a class needs to be specified by the subclass. to put it simply, the Factory Method can generate different instances according to different conditions. Of course, these different instances generally belong to the same type and share a common parent class. the Factory Method encapsulates the specific process of creating these instances, simplifies the application of the client, and improves the program scalability, this allows you to add new classes with minimal changes in the future. generally, we use Factory Method as a standard Method for object creation. When we find that more flexibility is required, we start to consider converting to other creation modes.
Simple analysis
Figure 1 shows the structure of the Factory Method mode. Some terms are provided here to facilitate the description:
Figure 1: Factory Method schema
1. Product: the abstract class of the Product to be created.
2. ConcreteProduct: a subclass of Product, a series of specific products.
3. Creator: abstract the createer interface, and declares that the Factory Method of the Product type object is returned.
4. ConcreteCreator: the specific Creator. Override the Factory Method in the Creator and return the ConcreteProduct-type instance.
From this we can clearly see the parallel correspondence: Product <===> Creator; ConreteProduct <==> ConreteCreator
Abstract products correspond to abstract creators, and specific products correspond to specific creators. What are the benefits of doing so? Why don't we directly use specific products and specific creators to fulfill our requirements? We can also do this. however, in the Factory Method mode, the customer (client) only needs to reference the abstract Product and Creater. It does not care about the specific ConcreteProduct and ConcreteCreator. In this way, we can gain additional benefits:
First, the client can obtain the generated instances from the abstract Creator. The role of the Creator separates the client from the product creation process. The customer does not have to worry about returning the specific product, you don't have to worry about how these products are created. at the same time, ConcreteProduct is also hidden behind the Product. ConreteProduct inherits all the attributes of the Product and implements the abstract methods defined in the Product. According to the cast principle in Java, the ConcreteProduct generated by ConcreteCreator can be automatically scaled to a Product. in this way, the ConcreteProduct with different substantive content can be unified as the Product in form and accessed through the Creator provided to the client.
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.