Java's "Common Design Patterns"

Source: Internet
Author: User

Prerequisites: UML Summary:

Hollow Triangle + Solid line: inheritance, triangle section for the parent class;

Hollow Triangle + Dashed line: Implements the interface, the Triangle section is the interface;

Hollow Diamond + Solid Arrow: An aggregation relationship, a can be part of B, but B cannot be a part of a; the arrows point to A; the two classes are equal to each other through instance variables;

Solid Diamond + Dashed arrow: Combined relationship, a must be part of B, A and B's life cycle is the same, the arrows point to A; Generally through instance variable performance, two classes are subordinate relations;

Dashed arrows: Dependency, A is a parameter of the method in B, B relies on a to make the method work normally, usually appears in the parameter of the method;

Solid arrows: Associative relationships, classes that refer to instances of Class B, an attribute arrow in a class that points to B, and a general occurrence in a member variable;

Each of these attributes will be decorated in front: "+" on behalf of public, "-" on behalf of Private, "#" for protected;

Common design patterns are in the following categories:

"Singleton Mode"--guarantees that a class has only one instance, and provides a global access point to access it, which is generally implemented by static variables (i.e., global variables) and static methods;

"Lazy Mode": The first time to use the time of instantiation, may be a thread problem;

"A Hungry Man mode": instantiated when loaded, compared to occupy resources;

"Factory mode"-provides an over-interface (a class) for creating objects, which masks the specific process of creating objects and increases flexibility, (using object-oriented features, separating the problem with object-oriented thinking, using a single class to process the creation of instances, rather than constructing the instance in the main process; involving: loose coupling , UML diagram;)

[Simple Factory mode]: Defines an interface for creating objects (a specific class); Altogether three roles: factory class role, abstract product role, specific product role;

[Factory method Mode]: No longer determined by a factory class which instance to create, but by the subclass of the abstract factory to determine which instances are common; there are four roles: Abstract factory roles, specific factory roles, abstract product roles, specific product roles;

[Abstract Factory mode]: Abstract factory is not only a class of products, there may be multiple abstract products;

(Reference: https://www.cnblogs.com/cr330326/p/5627658.html)

"Policy Mode"--encapsulation of the algorithm, so that each algorithm does not interfere with each other; According to the requirements, design an algorithm interface, there are several algorithm implementation classes, in the call can use Polymorphic to (overload) call different algorithm implementation class, but no need to change the code, there is a new demand, you can directly add a new computing class;

"Builder Mode"-defines the corresponding abstract class, and the calling method (i.e., the conductor), when different classes inherit the abstract class, all the methods need to be implemented, so there is no missing element, and the user invokes it with the command to prevent contact with the implementation class method; Like a builder, a general module is developed, The rest is refinement;

"Observer Mode"--defines a one-to-many relationship, when the state of an object changes, notifies all observer objects, causes multiple observer objects to be updated accordingly, generally in the "target class" and "Observer class" in the logical processing, so that the target class once the operation, the object will be updated to update the observer;

"Adapter Mode"--

[Class Adapter]: The interface of a class (an implementation class) into the user's desired interface (perhaps more than the original method), through the inheritance of the class; There are three roles: Target role (interface for implementation) A, source role (class to be adapted) B, adapter role (must be a specific class) C; Implementation Mode C Extends B implements A;

[Object Adapter]: implementation, C in the method of invoking B, so that the final match a interface, but do not need to implement a interface;

"Decoration Mode"--dynamic to an object to add a variety of accusations, using abstract classes to inherit the interface, but not necessarily fully implemented (so is the abstract class), the change can be selectively modified abstract class or new implementation of the abstract class;

The construction of the work, the single-policy fit;

Java's "Common Design Patterns"

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.