The composition of the object facilitates code reuse. (T / F why)

I am studying an exam and trying to understand this question. Specific question: “Inheritance and compilation of objects both contribute to code reuse (T / F),” but I believe that I understand some of the inheritance of the question.

I believe that inheritance promotes code reuse because similar methods can be placed in an abstract base class, so similar methods should not be identically implemented in several child classes. For example, if you have three kinds of shapes, and each method of the form "getName" simply returns the data member "_name", then why reinstall this method in each of the child classes when it can be implemented once in the abstract base class "form" .

However, my best understanding of object composition is the “has-a” relationship between objects / classes. For example, a student has a school, and the school has several students. This can be considered as a composition of the object, since they cannot exist without each other (a school without students is not really a school, is it?). But I do not see that these two objects, "having" each other as a data element, will facilitate code reuse.

Any help? Thanks!

+4
source share
5 answers

The composition of objects can facilitate code reuse, as you can delegate the implementation to another class and include this class as a member.

Instead of putting all your code in the methods of your outer classes, you can create smaller classes with smaller areas and smaller methods and reuse these classes / methods in all your code.

class Inner { public: void DoSomething(); }; class Outer1 { public: void DoSomethingBig() { // We delegate part of the call to inner, allowing us to reuse its code inner.DoSomething(); // Todo: Do something else interesting here } private: Inner inner; }; class Outer2 { public: void DoSomethingElseThatIsBig() { // We used the implementation twice in different classes, // without copying and pasting the code. // This is the most basic possible case of reuse inner.DoSomething(); // Todo: Do something else interesting here } private: Inner inner; }; 

As you mentioned in your question, this is one of the two basic principles of object-oriented programming, called "has-a relationship". Inheritance is a different relationship and is called is-replationhip.

You can also combine inheritance and composition in very useful ways that often increase your potential for using code (and design). Any real world and a well-designed application will constantly combine both of these concepts to get as much reuse as possible. You will learn about this when you learn about design patterns.

Edit:

At Mike's request in the comments, a less abstract example:

 // Assume the person class exists #include<list> class Bus { public: void Board(Person newOccupant); std::list<Person>& GetOccupants(); private: std::list<Person> occupants; }; 

In this example, instead of re-implementing the linked list structure, you delegated it to the list class. Each time you use this list class, you reuse code that implements the list.

In fact, since list semantics are so common, the C ++ standard library gave you std::list , and you just had to reuse it.

+5
source

1) The student is aware of the school, but this is not really a HAS-A relationship; while you would like to keep track of which school a student is attending, it would be illogical to describe the school as part of a student.

2) There are more people in the school than just students. One that includes reuse. You do not need to redefine the things that make up the school every time you describe a new type of student in school.

+3
source

I have to agree with @Karl Knechtel - this is a pretty bad question. According to him, it is difficult to explain why, but I will do it.

The first problem is that it uses a term that does not define it, and “code reuse” means a lot of different things for different people. For some people, cutting and pasting qualifies as code reuse. As much as I like it, I have to agree with them, at least to some extent. Other people define cod reuse in ways that exclude cutting and pasting as code reuse (classifying another copy of the same code as a separate code, rather than reusing the same code). I also see this point of view, although I am inclined to think that their definition is intended more to serve a specific end than to be really meaningful (i.e., “Code Reuse” → good, “cut-paste” → bad, cut-n-paste "! =" code reuse "). Unfortunately, what we are looking at here is right on the border where you need a very specific definition of what code reuse means before you can answer question.

The definition used by your professor will most likely depend on the degree of enthusiasm he experiences for OOP - especially in the 90s (or so), when OOP simply became the main one, many people decided to define it in such a way that it only turned on cool new OOP "stuff." In order to achieve the nirvana of code reuse, you had to not only register for your OOP religion, but really believe in it! Something as mundane as composition cannot be qualified - no matter how strange they were to flip the language to make it true.

As a second important point, after decades of using OOP, several people have done quite thorough research on which code got reused and what not. Most of what I saw reached a fairly simple conclusion: it is rather difficult (i.e. essentially impossible) to match the coding style with reuse. Almost any rule that you are trying to make that will or will not lead to code reuse can and will be violated on a regular basis.

Thirdly, and what I suspect tends to be central to many people's minds is that the question that the question generally sounds as if it is something that can / can affect a typical encoder , - that you might want to choose between composition and inheritance (for example), on the basis of which “code reuse is facilitated”, or something in that order. The reality is that (for example) you have to choose between composition and inheritance, first of all, on the basis of which the problem you are trying to solve is more accurately modeled and which helps you solve this problem.

Although I do not have serious research to support the claim, I would say that the chances of reusing this code will depend on several factors that are rarely considered in most studies: 1) how similar are the problems that someone else has to solve, and 2) do they think it will be easier to adapt their code to their problem than to write new code.

I should add that in some of the studies I saw, factors were found that seemed to influence code reuse. As far as I remember, the one that was the most important / telling was not the code itself, but the documentation available for that code. Being able to use code without fundamental reverse engineering, it makes a big contribution to its reuse. The second point was simply the quality of the code - a number of studies were conducted in places / situations where they tried to promote code reuse. In a large number of cases, people tried to reuse some more code than it actually was, but had to abandon it simply because the code was not good enough - everything from errors to awkward interfaces to poor portability prevented reuse.

Summary. I'll talk about how code reuse was probably the most common, undelivered promise in software development, at least in the last few decades. Even in the best case, code reuse remains a pretty elusive goal. Trying to simplify it to the point of considering it as a true / false question, based on two factors, simplifies the question to such an extent that it is not only pointless, but also completely ridiculous. It seems to trivialize and humiliate almost all software development practices.

+3
source

I have a Car object and an Engine object:

 class Engine { int horsepower; } class Car { string make; Engine cars_engine; } 

The car has an Engine; this is a composition. However, I don’t need to redefine Engine to put the engine in the car - I just say that the car has an Engine. In this way, composition really facilitates code reuse.

+2
source

The composition of objects really facilitates code reuse. Without composition of an object, if I understand your definition correctly, each class can have only primitive data members, which would be terrible.

Let's consider classes

 class Vector3 { double x, y, z; double vectorNorm; } class Object { Vector3 position; Vector3 velocity; Vector3 acceleration; } 

Without composition of the object you would have to have something like

 class Object { double positionX, positionY, positionZ, positionVectorNorm; double velocityX, velocityY, velocityZ, velocityVectorNorm; double accelerationX, accelerationY, accelerationZ, accelerationVectorNorm; } 

This is just a very simple example, but I hope you can see how even the most basic structure of an object promotes code reuse. Now think about what happens if Vector3 contains 30 data members. Does this answer your question?

+2
source

All Articles