JAVA design pattern-Create-style summary __java

Source: Internet
Author: User

The creation pattern includes:

Factory approach Mode: Provides a simple decision class that generates products based on conditions.

Abstract Factory Pattern: Provides an interface to create and return a range of products.

Singleton mode: A class can have only one instance. Provides a global access point. (Can be extended to a finite number of instances)

Builder pattern: Separate the construction of a complex object from the rendering so that different forms can be created according to different needs.

Prototype mode: Instantiate a class first, then clone or copy the class to build a new instance. You can further modify these instances in a common way.


Comparison & Summary:

There are two common ways to parameterize a system with classes of those objects created with one system. One is to generate subclasses of the class that created the object, and this corresponds to the factory method pattern. The main disadvantage of this approach is that you may need to create a new subclass just to change the product class. Such a change may be cascading (Cascade). For example, if the creator of the product itself is created by a factory method, you must also redefine its creator.

Another way to parameterize a system is to rely more on the composition of objects: Define a class that is responsible for defining a product object and use it as a parameter to the system. This is the key feature of the abstract Factory, Builder and prototype patterns. All three of these patterns involve creating a new "factory object" that is responsible for creating product objects. Abstract factory An object that produces more classes from this factory object. Builder by this factory object uses a relatively complex protocol to gradually create a complex product. Prototype the factory object to create the product object by copying the prototype object. In this case, because the prototype is responsible for returning the product object, all factory objects and prototypes are the same object.

Factory method enables a design to be customized and only slightly more complex. Other design patterns require a new class, and factory method requires only a new action. Factory method is often used as a standard way of creating objects. This is not necessary, however, when the instantiated class does not change at all or when the instantiation appears in an operation that can easily be redefined by subclasses (such as in an initialization operation).

Designs that use abstract Factory, Builder, and prototype are even more flexible than those designed to use Factory method, but they are also more complex. It is usually designed to start with factory method, and the design evolves to other created patterns when the designer discovers that greater flexibility is required. Knowing multiple patterns can provide you with more options when you weigh the design criteria.



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.