Object-oriented two caps

xiaoxiao2021-03-06  40

From the traditional way-oriented mode, in the process of learning to master object-oriented programming, some ideas can be difficult to transform, object-oriented, more is the way of thinking, rather than using methods. The more deep learning, the more you feel the profound of object-oriented technology, and there are some experiences recently.

In the object-oriented programming, the idea of ​​reimbursement is used, and two hats are used to achieve their goals.

Reconstructing two caps, referring to modifications and new features, when new features, do not change any original code, that is, wearing new features, if the original code is found, we also wear The upper reconstructed hat does not increase or change the functionality and interface execution of the original code. So what is the two hat facing object-oriented?

1. What is an object programming two hat?

The purpose of a software is to solve one or some problems, there is a need and implementation, in object-oriented programming, as an object, object, collaboration, call, and communication. In this process, it is simply used and used two relationships.

An object will always exhibit two forms, itself, and its interface. Or it is its body and a representation. The body is the structure and composition of the object itself, and the appearance is its interface, interact with other objects or performance.

So, when we pay attention to the object's own or interface, we need to wear different hats.

What hats are wear? We divide an object into two parts. First, the interface is realized, the interface is the performance of the object to the outside, and the implementation is the internal operation and processing mechanism, so there is these two caps.

Why is it two instead of three top four? From the perspective of understanding things, the second law is easy to understand and experience, just like white and black, good and bad, when paying attention to the interface, do not need to achieve the difficulties, will not be implemented, pay attention to implementation, Don't consider doing more features, do you want to change a name, add a parameter, etc., as long as you focus on implementing this interface.

The reason is to do, because the interface and implementation are one of the objects of the object, and the two factors have influenced each other to form a complete whole. The change of the interface must result in changes in implementation, and the change in implementation may affect the interface. Separate it, reduce this interaction.

How to wear these two hat? Of course, don't wear, wear a hacle that is realized. It is impossible, what do you realize without an interface? At the beginning, we should wear the top hat and then go to implement it. In the implementation, you will find that the code needs to be interacted with more other objects, it doesn't matter, it will exist, when you complete the compilation, You will naturally find it, but wear the hat of the interface through the compiler, so alternately.

What do you do after wearing a hat?

The hat is a symbol of a role, not a way to do things, because when encoding, write interfaces and write realms are written by writing code, do things, but the idea is different.

When you put on the hat of the interface, you are a user, consider the problem from the perspective of our customers, and put your current object. When you are your supplier, you are a picky customer.

We understand this problem from nature, an object is an object, this object is like this, not because it is so that it is like this, but because the effect of the surrounding environment will become like this, that is, the interface part is due to external The need is formed, can be seen as it does not belong to the object itself, but is owned by its customers, the object itself has an implementation of the interface, which provides the interface to the outside, has to evolve internally. Implement this interface. When wearing a hat, you turned into that supplier, facing a picky customer, that is, you just wearing the interface of the hat, you think about how to deal with it. To meet his requirements.

What is the relationship between this approach and the design pattern? The interface mentioned above refers to a method, attribute, event, etc. of a class. It is a fine-grained interface, which refers to the implementation of this class, attribute, event, etc., not the implementation of class level. It is some ideas that I have processed in the process of programming. Of course, this method is enlarged, and the relationship between design classes and classes has become secondary, and more concerned is more interface.

Alexander: Each mode describes a problem that has emerged in our environment and then describes the core of the solution. In this way, you can use the existing solutions countlessly without counting the same job.

Design mode refers to the mode in software development activities. It is a mode of software design. Generally, a design pattern is a result of multiple classes of collaboration. The implementation of a design pattern is a design, not a design implementation. A model can have n design, and each design, can also have N implementations. So two caps, describe how to face a design, how to implement it.

What is the relationship with TDD? In fact, these two hats are an understanding of learning TDD. TDD is driven by testing, and is tested to ensure reconstruction, it is a complete mechanism and methodology. Combine other XP ideas to form a whole XP development process pattern, from which to see the thoughts of foreigners, the logical system is strict. TDD test is the hat of the interface. In order to be implemented by testing the code, it is a hat, but the test drive is the entire development, including design and implementation, reconstruction, etc., is a large frame.

Of course, if the test, or the test code cannot be completely covered, it is also possible to wear these two caps, such as private methods, for an interior of a class, it is still an interface to use it.

By wearing these two caps, the ease of use, stability, and coupling of the interface can be improved. Because when writing every interface, it is considered at the user's perspective. At this time, it is convenient for the caller, from the small position, such as a private method, we will pay attention to what name, ready What parameters are given it, what is needed, and in the implementation of this method, pay attention to how to achieve this method better. Big a little, to the level, bigger, to a group of collaboration, bigger, to the components, modules, etc., the principle is the same.

转载请注明原文地址:https://www.9cbs.com/read-62702.html

New Post(0)