Java Design Patterns--------Basic principles

Source: Internet
Author: User

This article is your own study notes, welcome reprint, but please specify the source:http://blog.csdn.net/jesson20121020

before looking at the basic principles of design patterns, consider the basics of object-oriented knowledge.

1. The establishment of object-oriented thinking:

The three principles of object-oriented, namely inheritance, encapsulation and polymorphism, how are these defined and differentiated?

Encapsulation: Hides some of the implementation details and provides a way to access it openly.

eg. Private can hide some member variables and member methods.

Inheritance: You can have certain functions that already exist.

Polymorphic: Objects show different advantages at different times.

eg. List List = new ArrayList ();

List = new LinkedList ();

2. Differences in design ideas:

Abstract class: Is an abstraction that logically has a relationship to a set of properties and methods.

is a relationship.

Interface: An abstraction that is logically unrelated to a set of properties and methods.

The relationship of like a.

3. Several basic principles of object-oriented design:

general principle: Opening and closing principle

The open and closed principle is to say to the expansion opening, to modify the closure. When the program needs to be expanded, it is not possible to modify the original code, but to extend the original code.


A: Single responsibility

The first 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 extension of an object is open 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 any parent class appears, you can use subclasses instead.

D: Dependency Injection principle

To rely on abstraction, do not rely on a specific implementation.

Basic idea: interface-oriented programming as far as possible in development.

E: interface Separation principle

Do not use some features that you do not need 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: reduce coupling.

g: the principle of synthetic multiplexing

Instead of using inheritance, it is preferable to use a combination rather than an inheritance principle, which is to use the composition/aggregation method first.

Basic idea: In taking advantage of the object's lowest point, the combination is preferred, not the inheritance, because any changes to the parent class can directly affect the behavior of the subclass.

eg. To pack an elephant in a box:

Inheritance implementations:

Class A{method () {"Open refrigerator"}}

Class B extends A{method () {"Mount Elephant"}}

Class C extends B{method () {"Close refrigerator"}}

Combination implementations:

Class demo{

New A (). method ();

New B (). method ();

New C (). method ();

}

   

The above is the basic principle of design patterns, it seems very simple, but in the actual application process will encounter a lot of problems, in this record, in order to urge themselves in the future software development as far as possible to apply design patterns, thus a deeper understanding. The above notes are my own in listening to the "51cto" design mode in a video of the work done, it is possible to continue to do later.

 

Java Design Patterns--------Basic principles

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.