Java design mode, java design mode pdf

Source: Internet
Author: User

Java design mode, java design mode pdf

This article is my learning notes, welcome to reprint, but please note the Source: http://blog.csdn.net/jesson20121020

Before looking at the basic principles of the design model, let's look at some basic object-oriented knowledge.

1. The establishment of Object-oriented thinking:

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

Encapsulation: hides Implementation Details and provides a public access method.

For example, private can hide some member variables and member methods.

Inheritance: You can have some existing functions.

Polymorphism: objects have different advantages at different times.

Eg. List list = new ArrayList ();

List = new vertex list ();

2. Differences in design ideas:

Abstract class: an abstraction that logically relates to a group of attributes and methods.

Is a relationship.

Interface: a set of logically unrelated abstractions with the same attributes and methods.

The relationship of like.

3. Basic principles of object-oriented design:

General principles: open and closed principles

The principle of opening/closing is to open the extension and disable the modification. When the program needs to be expanded, the original code cannot be modified, but the original code must be extended.


A: single responsibility

The first object in the system should have only one separate responsibility, and all objects should focus on the completion of their own responsibilities.

Basic Idea: High Cohesion and low coupling.

B: Open and Close principles

The extension of an object is open and the modification is disabled.

Basic Idea: Changes to the class are made by adding code, rather than modifying the existing code.

C: Lee's replacement principle

Any parent class can be replaced by a subclass.

D: dependency injection Principle

It depends on abstraction, not specific implementation.

Basic Idea: Try to develop interface-oriented programming.

E: interface separation principle

Do not use unnecessary features.

Basic Idea: an interface does not provide too many behaviors.

F: Demeter principles

One object should have as little knowledge as possible about other objects.

Basic Idea: reduce coupling.

G: Principle of merging and reuse

The combination rather than inheritance principle is preferred, that is, the synthesis/aggregation method should be used first, rather than inheritance.

Basic Idea: in the use of the lowest point of the object, the combination should be given priority, rather than inheritance, because any change to the parent class may directly affect the behavior of the subclass.

Eg. Packing elephants:

Inheritance implementation:

Class A {method () {"enable Refrigerator "}}

Class B extends A {method () {"pack elephant "}}

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

Combined implementation:

Class Demo {

New A (). Method ();

New B (). Method ();

New C (). Method ();

}

   

The above is the basic principle of the design model. It seems very simple, but there are still many problems in the actual application process, in order to urge yourself to apply design patterns as much as possible in future software development, so as to gain a deeper understanding. The above notes are made by myself in the video "51cto" design mode, which may be continued later.

 

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.