Java Design Pattern Notes

Source: Internet
Author: User

1, why to learn design patterns
A: Let us have a certain understanding and understanding of the design pattern
B: Deepen our understanding of object-oriented abstract classes and interfaces
C: Interview for design mode
Single Case design mode
Factory mode adapter template Mode agent ...

2 What is design mode
On the construction site, workers are building houses, but no one can see the bricks.
In a car factory, you see people who assemble cars, not people who make parts.

In software development, UI designers, system analysts, code personnel

As a result, some experts summarize some of the experiences they have accumulated in the process of solving a problem to help later people better master the abstraction to the specific approach, which is the design pattern

3. Learning route for Design patterns
A: The establishment of object-oriented thinking (encapsulation of inheritance polymorphism)
Show our thoughts through some symbols that any software developer can read.
C: Understanding Common design Patterns
D: Learn and use design patterns

Encapsulation: Hide some of the implementation details and provide some public access
Inheritance: can have certain functions that already exist
Polymorphism: The different states of an object at different times

The difference between design ideas;
Abstract class: Is an abstraction (is a) that has a logical relationship to a set of properties and methods.
Interface: An abstraction (like a) for a set of transactions that have the same properties and methods that are not logically related.

Several basic principles of object-oriented design:
A: Single Responsibility
Each object in the system should have only one separate responsibility, and all objects should be concerned with the completion of their own duties. Basic idea: cohesion, low coupling

B: Opening and closing principle
An object is open to the extension and closed for modification
Basic idea: Changes to a class are made by adding code instead of modifying existing code

C: the Richter replacement principle
Wherever the parent class appears, you can use subclasses to replace

D: Dependency Injection principle
To rely on abstraction, don't rely on specific implementations
Basic idea: interface-oriented programming as far as possible in development

E: interface Separation principle
Do not use features that you do not want to use
Basic idea: An interface does not provide too much behavior.

F: Dimitri Principle
An object should know as little as possible about other objects.
Basic idea: reducing coupling

G: Prioritize use of combinations rather than inheritance principles
Because changes to the parent class can affect the behavior of the subclass.

10. Design Pattern Classification
Create pattern: Create an object.
Structural patterns: The composition of objects and their relationship to objects
Behavioral Patterns: Object behavior, what an object can do (function)

11. Create pattern
Simple Factory mode
Animal (abstract Class), Dog,pig,animalfactory, Animaltest
Factory method Mode
Abstract Factory mode


1. Singleton Mode (interview)
Singleton mode: a single instance. Ensure that the class has only one object in memory
Example: Windows Print Service, website counter
Applications in Java: Runtime

2. How do I guarantee that there is only one object in memory?
A: The construction method is private, to walk away from the outside to create objects.
B: To create an object in the class
C: Provide an entrance to the outside world by means of a public access

3. A hungry man single-case mode

4. Lazy single-case mode
Lazy Loading thought: When we need (object new, etc.), you will give.

5. So, who are we using in the development?
In general, we develop the first method, that is, a hungry man-type

Premise: Multithreading security issues.
During the interview, I will interview lazy-type.
And, the interview lazy type is mainly the interview the following several questions:
A: Lazy Loading thought
B: Thread safety issues:
A: How thread safety issues are generated
B: How the thread safety problem is solved (add a keyword: sync synchronized)

6. In fact, in the JDK provided by the class, there are already a singleton mode application, such as: Runtime class

1. Decoration Mode:
As the name implies, decoration mode, refers to the packaging of XXX.

The fact is to add new functionality to the object.

2. Adding new features to an object
Object-oriented: inheritance.
Decorative mode.

3. Example: We all have mobile phones, mobile phones have the function of calling. Ringtone Phone Music
Phone, Colorphone,musicphone,musiccolorphone
Inheritance implementation and decoration mode implementation IO Stream (embodiment of decoration mode)

Java Design Pattern Notes

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: 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.