Detailed description of design patterns

Source: Internet
Author: User
Design Mode (factory mode, Singleton mode, template Mode)

Simple factory mode:

1. overview: The factory mode provides a transitional interface for object creation to hide the specific process of object creation (New Keyword and specific constructor) and replace it with a factory method, only a factory method is provided to improve flexibility.

2. Advantages:

1. concealed the New Keyword and constructor

2. Reduced the coupling between this object and other classes and improved the scalability of the program.

Cause: When the subclass is replaced by another class or the parameters of the constructor change, you only need to change the new value in the factory method, and the change volume is minimized. If the factory mode is not used, however, if the new keyword is used directly, there will be a lot of changes.

3. The design and implementation of objects are separated, so that the code is highly scalable and flexible.

3. Applicability of the factory model:

In the following situations, developers can consider using the factory model:

· You cannot predict the type of instance to be created during encoding.

· A class uses its subclass to create objects.

· Developers do not want to create any class of instances and expose the information about how to create instances to external programs.

In addition to the example above, the implementation method of the factory model allows some small changes, such:

· The base class can be an abstract class. In this case, the factory class must return a non-abstract class.

· The base class provides some default methods. Only when these default methods cannot meet special requirements can they be rewritten in the subclass.

· You can directly decide which subclass instance to return by passing the parameter to the factory class.

4. The factory mode is used. The design period is different from the objects in the runtime, which enhances the code scalability.

It hides the constructor, reduces the code coupling, and enhances code reusability.

Compare the factory mode with new: if the new keyword is used, if the class object is used in many places, multiple new operations must be used, this will easily lead to repeated use of code. If you need to change or replace it with the subclass object of this class, you need to change all the places where the new operation is executed, it is troublesome. The factory mode, because it replaces the constructor and the New Keyword, and it uses interface-oriented, so when you need the object of this class, you only need to call this method. If you need to change or replace it with the subclass object of this class, you only need to modify the content in this factory, and do not need to change anything else.

The structure of the factory mode is: replace the New Keyword and constructor with a method.

 

 

Singleton mode:

Category:

1. Four features of Singleton mode:

Lazy style:

1. Declare a private, static class object, but it is not initialized when declared. Therefore, its value is null.

2. Private Constructor

3. Provide a global, public, static, and unique method for obtaining the instance, but note that the thread synchronization must be manually maintained (synchronized)

4. in this method, it is used to determine whether the object is null. If it is null, it indicates that this class has not been instantiated and the object will be initialized. If it is not null, it will be returned directly.

Hungry Chinese style:

1. Declare a private, static class object, and initialize it when declaring

2. Private Constructor

3. Provide a global, public, static, and unique method for obtaining the instance (the thread in the hungry mode is synchronized)

4. In this method, you can directly return this object.

From the perspective of resource utilization efficiency, this is slightly worse than the lazy Singleton class. In terms of speed and response time, it is better than the lazy Singleton class.

2. What problems does it solve: it ensures that a class has only one instance in the Java virtual machine, so that a class has only one object, and the entire system shares this object.

3. When to use lazy and ELE. Me:

1. When the probability of use is very small, use the lazy style.

2. If the probability is high, the hungry Chinese will be used.

4. Benefits of Singleton mode:

All classes in the system share an instantiated object, which can effectively save resources.

 

 

Template mode:

In the template mode, multiple implementation classes inherit an abstract parent class, control the top-level logic by abstracting the parent class, and postpone the implementation of basic operations to the Child class, this can effectively simplify the code of various classes at the control layer. With this mode, we can place fixed exception handling code in the base class, and let the child classes implement specific services, if an exception occurs during service execution, such as database connection failure, the user directly throws the exception to the base class for processing after the exception is not found, in this way, the Service Processing and exception handling are successfully separated into child classes and two types of basic classes, and the subclass Code involving specific service processing is greatly simplified, making it easier to read, modify, and manage.

1. Define the parent class flow and implement the sub-classes:

The template mode solves the problem of repeated processes, repeated methods, and repeated variables in sub-classes. Different sub-classes have different methods, which are extracted and placed in the parent class. All sub-classes have flows, and their flows are defined in the parent class. The specific implementation of flows is implemented by sub-classes.

2. Purpose of the template method:

A. reduce repeated code.

B. Prevent call errors.

3. Steps for using the template method:

A. Recognition: find out from a series of algorithms where their common parts are called recognition.

B. Separation: extract different codes, which is called separation.

C. Implementation: Use inheritance to implement the extracted method in the subclass.

4. Benefits of the template mode:

In the template mode, you can share all the common code in the subclass to the common parent class, which increases code reusability. Then, you can define the program process in the parent class, child classes only need to implement the abstract methods defined in the process. This makes it easier for a project team to develop and the code is clearer. Because the parent class is an abstract class, this makes the code in the parent class only visible in the subclass and increases the code security. However, when there are changes in the system, the template mode can be used to solve unnecessary troubles.

 

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/maxracer/archive/2010/03/01/5334732.aspx

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.