Flylib.com

Books Software

 
 
 

Composition

 <  Day Day Up  >  

Composition

It is natural to think of objects as containing other objects. A television set contains a tuner and video display. A computer contains video cards, keyboards, and drives. Although the computer can be considered an object unto itself, the drive is also considered a valid object. In fact, you could open up the computer and remove the drive and hold it in your hand. Both the computer and the drive are considered objects. It is just that the computer contains other objects ”such as drives .

In this way, objects are often built, or composed , from other objects ”this is composition.

Has-a Relationships

Although an inheritance relationship is considered an is-a relationship for reasons already discussed, a composition relationship is termed a has-a relationship. Using the example in the previous section, a television has-a tuner and has-a video display. A television is obviously not a tuner, so there is no inheritance relationship. In the same vein, a computer has-a video card, has-a keyboard, and has-a disk drive. The topics of inheritance, composition, and how they relate to each other is covered in great detail in Chapter 7, "Mastering Inheritance and Composition."

 <  Day Day Up  >  
 <  Day Day Up  >  

Conclusion

There is a lot to cover when discussing OO technologies. However, you should leave this chapter with a good understanding of the following topics:

  • Encapsulation ” Encapsulating the data and behavior into a single object is of primary importance in OO development. A single object contains both its data and behaviors and can hide what it wants from other objects.

  • Inheritance ” A class can inherit from another class and take advantage of the attributes and methods defined by the superclass.

  • Polymorphism ” Polymorphism means that similar objects can respond to the same message in different manners. For example, you might have a system with many shapes. However, a circle, a square, and a star are each drawn differently. Using polymorphism, you can send each of these shapes the same message (for example, Draw ), and each shape is responsible for drawing itself.

  • Composition ” Composition means that an object is built from other objects.

This chapter covers the fundamental OO concepts. By now you should have a good grasp of what OO concepts are all about.

 <  Day Day Up  >  
 <  Day Day Up  >  

Chapter 2. How to Think in Terms of Objects

In Chapter 1, "Introduction to Object-Oriented Concepts," you learned the fundamental object-oriented (OO) concepts. The rest of the book digs more deeply into these concepts. Many factors go into a good design, whether it is an OO design or not. The fundamental unit of OO design is the class. The desired end result of OO design is a robust and functional object model ”a system.

As with most things in life, there is no single right or wrong way to approach a problem. There are usually many different ways to tackle the same problem. So, when attempting to design an OO solution, don't get hung up in trying to do a perfect design the first time. What you really need to do is brainstorm and let your thought process go wild. Do not try to conform to any standards or conventions when trying to solve a problem, because the whole idea is to be creative. Thus, before you start to design a system, or even a class, think the problem through and have some fun! In this chapter we explore the fine art and science of OO thinking.

The move from the procedural world to an OO world is not trivial. Changing from FORTRAN to COBOL, or even to C, requires that you learn a new language; however, making the move from COBOL to C++, C# .NET, Visual Basic .NET, or Java requires that you learn a new thought process. This is where the overused phrase OO paradigm rears its ugly head. When moving to an OO language, you must go through the investment of learning OO concepts and the corresponding thought process first. If this paradigm shift does not take place, one of two things will happen: Either the project will not truly be OO in nature (for example, it will use C++ without using OO constructs), or the project will be a complete object-disoriented mess.

Three important things you can do to develop a good sense of the OO thought process are covered in this chapter:

  • Knowing the difference between the interface and implementation

  • Thinking more abstractly

  • Giving the user the minimal interface possible

We have already met some of these concepts in Chapter 1, and here we will now go into much more detail.

 <  Day Day Up  >