contactmatt
contactmatt

Reputation: 18630

Design Patterns : Question about "Types"

Would someone please explain to me what the below paragraph means? This is a snippet from "Design Patterns: Elements of Reusable OO software"

Part of an object's interface may be characterized by one type, and other parts by other types. Two objects of the same type need only share parts of their interfaces. Interfaces can contain other interfaces as subsets. - Design Patterns - Elements of Reusable OO software, pg 13

Upvotes: 2

Views: 99

Answers (4)

pc1oad1etter
pc1oad1etter

Reputation: 8617

It seems to be describing the ability for classes to implement multiple interfaces.

For example, a Car and a Motorcycle might both implement iVehicle, but they each may have other methods and members that are unique to each other. For example, the Car might implement iWindowedVehicle whereas the Motorcycle would not.

One part of the quotation which may be misleading is "Two objects of the same type need only share..." (emphasis mine). I assume they are using the word type there loosely, to mean, two objects which implement the same interface (like Car and Motorcycle both implement iVehicle, as opposed to the same actual class, like two Cars in my example.

Upvotes: 0

jamesmortensen
jamesmortensen

Reputation: 34078

Perhaps what the author is saying is that an object can implement more than one interface. For instance, a "RaceCar" class might implement the "Drivable" interface while also implementing the "PotentiallyDangerous" interface.

In this example the RaceCar class might implement the "useNitrous()" method as declared by the "PotentiallyDangerous" interface and also implement the "startIgnition()" method declared by the "Drivable" interface.

Sorry, nothing against RaceCar enthusiasts, but it's the best car example I could get someone to edit for me.


EDIT drachenstern: thought you might appreciate the humorous edit, feel free to revert.

Upvotes: 2

Senthil Kumaran
Senthil Kumaran

Reputation: 56951

I think, he a means Composite Object created by a class which which has two parents providing different interfaces each useful to the object created. Look for the Java Example in the referenced link.

Upvotes: 0

vy32
vy32

Reputation: 29707

To understand what this means you first need to remember that the Gang of Four wrote this book back in 1994, when the vast majority of programmers in the world were not using (and had never heard of) object-oriented programming.

Gamma, Helm, Johnson and Vlissides are basically introducing the concept of interfaces here. The idea is that a piece of code that interacts with an object doesn't really need to understand how the underlying implementation happens, and that two different objects can have the same interface but have different implementations. We do that all the time today with interfaces in Java and Objective-C.

But they go further, and imply that an object may have several interfaces, one from one type (or interface), and one from another. You can do this explicitly with multiple inheritance in C++, or with multiple interfaces in Java, or just by using the same naming convention.

Upvotes: 6

Related Questions