Object-oriented - Flanders blog

First, the three characteristics

1, the package

Package is a hidden object attributes and implementation details, only the external provision of public access.

benefit

(1) Access control: improved code robustness.

(2) Isolation change: Impacting internal logical changes do not diffuse.

(3) reduce the burden of thinking: less information, easier to understand and modify.

2, inheritance

Implements inheritance relations IS-A, for example, with Animal Cat it is a kind of IS-A relationship, therefore can be inherited from Animal Cat, thereby obtaining Animal no private properties and methods.

benefit

(1) Method inherits the base class, make their own changes and extended to achieve code reuse.

(2) declare that a certain subclass the base class compatible, external caller may not need to focus on their differences.

The use of

(1) to make use of inheritance, polymorphism and achieve most of the design patterns are based on inheritance. Object-oriented first of all to want to master the proper use of skilled inheritance.

(2) Do not implement code reuse, use inheritance.

3, polymorphism

Polymorphism allows you to become a parent set equal to or more sub-objects of his technique, after the assignment, the parent object can operate in different ways depending on the characteristics of the current assigned to its child objects.

benefit

Derived class functions can be called a base class methods or references to variables, called backward compatibility can be improved scalability and maintainability

Realization conditions

(1) Inheritance: there must be a subclass inheritance and parent.

(2) rewritten: subclasses of the parent class redefinition of certain methods, the method will be called when calling a subclass of these methods.

(3) Transformation upwards: you need to assign a reference the parent object, so that the only polymorphic in reference subclass will be able to have methods and subclasses skill call the parent class.

4, learning the three major characteristics

1, a simple understanding of the definition of the three major characteristics, as well as its implementation.

2, using the three properties under development.

3, learning in various design patterns, how they are using object-oriented three characteristics.

Second, the design principles

S.O.L.I.D

Logogram Quanpin Chinese translation
SICKLE The Single Responsibility Principle Single Responsibility Principle
OCP The Open Closed Principle Open Closed Principle
LSP The Liskov Substitution Principle Richter substitution principle
ISP The Interface Segregation Principle The principle of separation Interface
DIP The Dependency Inversion Principle Dependency Inversion Principle

1. Single Responsibility Principle

Modify a class should have only one reason.

In other words, to make a class is only responsible for one thing, when this class need to do too many things, you need to break down this category.

If a class too many responsibilities to bear, it means these functions coupled together, a change in responsibilities may undermine the ability of the class to perform other duties.

2. Open Closed Principle

Class should be open for extension, but closed for modification.

Extended meaning is to add new features, this principle requires no code change when adding new functionality.

The most typical patterns meet design principles are opened and closed by decorative patterns, which can dynamically attach to an object responsibility, without having to modify the code class.

3. Richter substitution principle

Subclass object must be able to replace all of the parent object.

Inheritance is an IS-A relationship, the subclass as the parent needs to be able to use, and require more specific than the parent class.

If this principle is not met, then there will be very different behavior of each sub-category, increasing the complexity of the inheritance system.

4. Interface Separation Principle

Customers should not be forced to rely on the method of their unused.

Thus the use of a plurality of dedicated interfaces than using a single interface to better overall.

5. Dependency Inversion Principle

High-level modules should not depend on low-level modules, both of which should depend on abstractions; </br> abstract should not depend on the details, the details should depend on the abstract.

Large columns  of object-oriented - Flanders blog senior application module contains a significant policy choice and the service module, if the module depends on the lower level modules, then the changes will directly affect low-level module to module level, forcing high-level modules We need to change.

It relies on abstract meaning:

  • Any variable should not hold a pointer or reference to a particular class;
  • Any class should not derive from specific classes;
  • Any method should not override the method has been implemented in any of its base class.

Other common principles

In addition to the classic principle of the above, in the actual development as well as following these common design principles.

Logogram Quanpin Chinese translation
LOD The Law of Demeter Demeter
CRP The Composite Reuse Principle Synthesis of multiplexing principles

1. Demeter

Demeter also called the principle of least knowledge (Least Knowledge Principle, shorthand LKP), that is a subject should have as little understanding of other objects, do not talk to strangers.

2. Synthesis multiplexing principle

Try to use a combination of objects, rather than to achieve the purpose of reuse through inheritance.

3. programming interface rather than the achievement of program

Inheritance should inherit the interface, rather than implementation.

4. loosely coupled design

Notes: learning design principles in design mode should carefully understand their avatar.

Third, 类图

The following class diagram used PlantUML drawing.

Generalization (Generalization)

Used to describe inheritance, use the extends keyword in Java.


Note: The final code, the generalization performance of non-abstract class inheritance;

Realization relationship (Realization)

It is used to implement an interface, use the implements keyword in Java.


Note: The final code, the realization of the relationship between the performance of inheriting an abstract class;

Aggregation relationship (Aggregation)

Represented by the whole part, but part of the whole and not strongly dependent, there is no part of the whole will still exist.


Combination relationship (Composition)

And the polymerization is different in overall composition and are strongly dependent part, the overall absence of a part does not exist. For example, companies and sectors, the company did not exist the department. But the company and the employees belong to the aggregation relationship, because the company is still not the employees.


Association (Association)

It represents the association between different types of objects, which is independent of a state of static relationships, and processes running at the beginning can be determined. You too can be 1 to 1, a plurality of, such a many to many association relationship expressed. For example, students and schools is a kind of relationship, a school can have a lot of students, but only a part of a school student, so this is a relationship of many to one, it can be determined before the start of operation.


Note: In the final code, usually in the form associated object member variables to achieve;

Dependencies (Dependency)

And relationships are different, the dependency is functional during the operation. Class A and B are dependencies three major forms: (Properties)

  • A class is a class B of the process of local variables;
  • A class is a class B parameter among the methods;
  • A 类向 B 类发送消息,从而影响 B 类发生变化。


注:在最终代码中,依赖关系体现为类构造方法及类方法的传入参数,箭头的指向为调用关系;依赖关系除了临时知道对方外,还是“使用”对方的方法和属性;

参考

面向对象思想


Guess you like

Origin www.cnblogs.com/lijianming180/p/12376093.html