Java Basics (a) - Object-Oriented

Process for the difference between object-oriented and

What is process-oriented (pop)

Process-oriented (Process-oriented programming) is an event-centric programming ideas. It is to analyze the steps required to solve the problem, then use the function to achieve all of these steps, and then follow the order called.

What is Object-Oriented (oop)

Object is centered on object-oriented programming ideas

For example understanding

Launch vehicle, car station. Car start is an event that car station is another event, process-oriented thinking is that we are concerned about the event, rather than the car itself. Forming two functions for these two events, followed by calls. For object-oriented thinking, we are concerned about is the car such objects, but such objects are two events have behavior. And these acts did not order. Process-oriented way of thinking is a comprehensive analysis, object-oriented way of thinking is constructed.

Advantages and disadvantages of process-oriented and object-oriented

Process-oriented

Advantages: performance than object-oriented, because of the need to instantiate the class when called, the overhead is relatively large, more consumption of resources; such as microcontrollers, embedded development, Linux / Unix and other commonly used process-oriented development, the performance is the most important factor.

Cons: no object-oriented and easy to maintain, easy to reuse, easy to expand

Object-Oriented

Advantages: easy to maintain and reuse, easy to expand, since there are object-oriented encapsulation, inheritance, polymorphism characteristic, low coupling system can be designed to make the system more flexible and easier to maintain

Disadvantages: process-oriented performance lower than

The three basic features of object-oriented

Package

Package is to hide the object properties and implementation details, only open to the public interface to control read and modify the program attribute access level, abstract data obtained and acts (or function) are combined, forming an organic whole , i.e. the source code data and operation data of organically combined to form a "class", the data and functions which are members of the class.

The purpose of the package is to enhance security and simplify programming, the user need not know the specific implementation details, but only through external interfaces to specific access rights to the use of members of the class.

inherit

Inheritance is one of the basic features of object-oriented inheritance mechanism allows the creation of hierarchical level classes. Inheritance is the subclass inherits the characteristics and behavior of the parent class , so that the subclass object (instance) instance fields and methods having a parent class or subclass inherits methods from parent classes, subclasses that have the same parent class behavior.

FIG Similar following:

Above, we've packaged the class rabbit, other animals, too, can be encapsulated. We found that during encapsulation rabbit, sheep two classes having similar properties or functions, such as grazing, so we can extract a common feature and a method of forming a layer of a high class , as here herbivores, carnivores. The relationship between the child inherits the parent class. Inheritance mechanism can be a good description of the ecological class, but also improve the rate of code reuse, Object class in Java is the superclass of all classes, often referred to as God class.

Polymorphism

Capacity having a plurality of different polymorphic forms or forms of the same behavior. Refers to a method of the same class instances (objects) have different forms in different situations. Of polymorphism so that objects having different internal structures can share the same external interface. This means that, although different specific actions for different objects, but through a common class, they (those operations) can be invoked in the same way.

Five basic principles of object-oriented

Single Responsibility Principle (SRP)

A class should have one and only one reason to change it, which means that a class should have only one job.

For example, in class staff, the engineers, salespeople, sales managers of these cases are on the staff of class considerations, the result will be very confusing, under this assumption, the staff class of each method must determine which if else case, from the class structure, it will be very bloated.

Open Closed Principle (OCP)

Object or entity should be open for extension, closed for modification.

Change that is closed when we extend the module, you do not need to have the source code and modify or recompile the DLL file! This principle is useful for when we design class, adhere to this principle must also consider the interface encapsulation, abstraction, and polymorphism technology

Richter Substitution Principle (LSP)

Q (x) x is established when the target type T, then when S is a subclass of T, the object type y is S when Q (y) should be established. (Ie calls to the parent class is equally applicable to a subclass) vernacular translation: a software If you are using a parent, then the child must apply to their class and imperceptible parent and child class object distinction. That is, the software inside, replacing the parent class to its subclasses, the behavior will not change, simply put, subtype must be able to replace their parent type.

Dependency Inversion Principle (DIP)

High-level modules should not depend on low-level modules, they should depend on abstractions. Specific implementation should depend on abstractions, rather than an abstract implementation dependent.

For example, today met a rabbit and sheep, then introduced herbivores. But if tomorrow Recognizing cattle, horses and other herbivores, we will find that we need to constantly adjust the description of herbivores, which is obviously unreasonable. So we should not let subclasses depends on the entity, should not allow the parent module depends on the subclass module. Therefore, when the herbivores designed as an abstract class ( abstract class or interface without affecting the parent class) so that the lower layer corresponding implementation details.

Interface Segregation Principle (ISP)

Should not be forced to implement a client interface that do not have access, or that the client should not be forced to rely on their method is not used, the use of multiple specialized interface is much better than using a single interface!

For example, in order to reduce the interface definition, the number of similar methods in an interface, and finally will find, maintain and implement the interface when it took too much energy, and the interface defined by the equivalent of operating a commitment to client this commitment is of course better, more concise as possible, to bring too much commitment is your time and a lot of energy to maintain!

Guess you like

Origin juejin.im/post/5d8734d86fb9a06ad005abc9