The basic training of engineers - six principles of object-oriented presentation

First, the object simply face down. Software running on the machine, used to solve practical problems, there is a certain order to solve a problem, as long as the issue to dismantle, then a sequence of a complete, most problems can be solved, which is process-oriented programming.

But for more complex models, process-oriented programming if continued use, some programs will become difficult to control. In order to solve the problem, the need for this world abstract, to a task, a program easier to control and split into small pieces to understand, among pieces defined the use of principles, then in chunks, the logic of all modules are up and running, have the same characteristics of the module can be inherited to better management, you can also define interface constraints, so that modules have the same appearance, and so on, which is probably the object-oriented.

Object-oriented basic principle: Everything Is an Object, the first object-oriented language should be SmallTalk.

This world exists regardless of specific transactions or virtual thing, as it can be when an object, such as in the real world there is a specific ship, aircraft, as well as virtual things, such as task progress, product reviews, you can put them as objects only they need to be abstract, you can put all constructed objects, and then use the programming language assemble them together, to be logical processing, display interface, object-oriented can solve practical problems.

Three characteristics of object-oriented

There are three characteristics of object-oriented: encapsulation, inheritance, polymorphism. This is the whole object-oriented core, once you understand these points, you can write object-oriented programs.

The package may be packaged as a separate thing, encapsulated, an object on the shape. For example, to ship the package, it has many properties, such as weight, size, speed, service life, etc., there are many features, such as starting, stopping, loading and unloading method, all of these properties, methods are wrapped in a class, then providing out, so people get the package, can be used directly, this is the package.

Inheritance can make better use of existing design package. For objects with common characteristics, the inheritance, the inheritance can be easily shared by the parent class logic, better control programs.

Polymorphic mainly through interface at runtime depending on the type of call in the same manner, but to achieve control by the implementation class. Use of people say good.

Six principles of object-oriented

Object-oriented good, but the same is object-oriented, different people may write a completely different program, both good and bad. In order for us to write good programs like objects, in addition to the six principles predecessors summary, as long as they understand and apply these principles, we can write good object-oriented programming. At the same time so, if we follow these principles are written, the code to see each other, to understand the code will become much easier.

The following six principles described in detail with reference to data from the network, object-oriented six principles and design patterns | Melo's Blog

Single Responsibility

A class, it should be only one reason for his cause change. That is a class should only be responsible for one thing.

That is a class, a method should only do one thing, so you can keep class, a simple method, without any clutter factor, use and maintenance will become very easy, Less is more.

Open Closed Principle

Try to address the needs of different scenarios by extending the application of classes, modules and functions, rather than by directly modifying existing classes, modules and functions.

A software entity classes, modules and functions should be open for extension, but closed for modification. In the software life cycle because of changes, upgrades and maintenance reasons, the need to modify existing software code, the old code could have introduced errors. Therefore, when the software needs to be changed, we should try to achieve change through the extended, rather than by modifying existing code.

Existing classes, methods may have been running for a long time, they may have problems, but try not to go directly to change them, very risky, as they are packaged, they raise through external means.

Richter replacement

Richter substitution principle is dependent on inheritance, polymorphism, these two characteristics. Richter replace the simple principle that all references to base classes, where the interface must be able to transparently use the object of its subclasses. Popular speak, as long as the parent class where the subclass that can occur can occur, and replaced subclass does not produce any error or abnormal, the user may simply need to know is the parent class or subclass. However, not vice versa, where there appears subclass, the parent may not be able to use.

Dependency Inversion

Layer module should not rely on the underlying module, both of which should depend Abstract; should not depend on the details of the abstract; abstract details should depend

  • High-level modules should not depend on the underlying module, both of which should rely on their abstract.
  • Abstract should not depend on the details.
  • Details should depend on the abstract.

Interface Segregation

The client should depend on its unwanted interfaces: a class depends on another class should be based on the smallest interface. According to the interface segregation principle, when an interface is too big, we need to split it into some of the finer interface, use the interface of the client only needs to know the method associated with it.

Demeter

An object should be kept to a minimum understanding of other objects.

Only the provider of their own, only the understanding of their understanding. This practice guide is not to expose too much of methods, properties, they are designed to give priority to private, only need to use up, go to consider open access.

Reference links

Guess you like

Origin www.cnblogs.com/mgudong/p/11965295.html