Creation Mode Java

Source: Internet
Author: User

Creation type

The Creation Mode hides how instances of these classes are created and put together. What the entire system knows about these objects is interfaces defined by abstract classes. In this way, the creation mode provides great flexibility in what it creates, who it creates, how it is created, and when it is created.

The Creation Mode abstracts the instantiation process. They help a system to create, combine, and express its objects independently. The Creation Mode encapsulates information about the specific classes used by the system. Allows the customer to configure a system with products with different structures and functions. The configuration can be static, that is, specified during compilation or dynamic, that is, specified during runtime.

 

Abstract Factory (Open-Close principle, dependency reversal principle)

Provides an interface for creating a series of or related dependent objects without specifying their specific classes.

 

Advantages:

It is easy to switch product series. Because a specific factory class only needs to appear once during initialization in an application, it makes it very easy to change the specific factory of an application, it only needs to change the specific factory to use different product configurations.

 

It separates the specific instance creation process from the client. The client uses their abstract interfaces to manipulate the instance, and the specific product class names are also separated by the specific factory implementation, does not appear in customer code.

 

Disadvantages:

Although the Code for accessing two databases can be easily switched, if you need to add a function, you need to change the class. Therefore, we need to look at the problem dialectically. When using the design model, we need to see whether it is applicable.

 

Builder (dependency reversal principle)

The construction of a complex object is separated from its representation, so as to realize the loose coupling, so that different representations can be created in the same construction process and different products can be created for the customer through the same building process.

First, let's take a look at what cohesion and coupling are. Cohesion describes the closeness between components of a routine. Coupling describes the closeness between a routine and other routines. The purpose of software development should be to create such a routine: internal integrity, that is, high cohesion, and the relationship with other routines is small, direct, visible, and flexible.
This is loose coupling.

 

 

Factory method (Principles of synthesis/aggregation reuse and On-Off)

Defines an interface used to create objects so that the subclass determines which class to instantiate. The factory mode delays the instantiation of a class to its subclass.

Generally, the design should start with the factory method. When the designer finds that more flexibility is required, the design will evolve to other creation modes. When the designer balances the design standards, understanding multiple creation modes gives the designer more options.

The factory method allows us to add new products without the need to change the original product system and factory class. We only need to expand the new class. This is a very important criterion for judging whether a model is excellent.

 

Prototype (dependency reversal principle)

Use a prototype instance to specify the type of the object to be created, and copy the prototype to create a new object.

Creating prototype and cloning them is usually easier than manually instantiating this type with the appropriate state each time.

 

Singleton (single responsibility principle)

Ensure that a class has only one instance and provides a global access point to it.

For some classes, an instance is very important. A global variable can make an object accessible, but it cannot prevent the customer from instantiating multiple objects. The advantage of Singleton mode is that the class itself is responsible for saving its unique instance. This class ensures that no other instance can be created, and the singleton also provides a method to access the instance. This allows you to strictly control how and when a customer accesses a unique instance.

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.