RSS

Two Principles of Object-oriented design

31 Dec

Abstract from Design patterns : Elements of Reusable Object-Oriented Software

1 Program to interface, not an implementation.

Advantages

  1. Clients remain unaware of the specific types of objects they use, as long as the objects adhere to the interface that client expect.
  2. Clients remain unaware of the classes that implement these objects. Clients only know about the abstract class defining the interface.

 

2 Favor object composition over class inheritance.

Despite of obvious situation we should apply class inheritance or objects composition, I discuss the situations that two methods are both practicable.

Advantages of Inheritance

Straightforward to use.

It is easy to modify the implementation of subclasses.

Disadvantages of Inheritance

Breaking the “Encapsulation”

Forcing the implementations of subclass change when implementations of parent class change, which making reusing subclasses difficult considering the changes of their parent class.

Advantages of Composition

  1. Maintaining the “Encapsulation”

 

Objects don’t know the implementation of others and they can deal with others’ interfaces. As a result, there is fewer implementation dependency.

2.    Class hierarchies remain small

To accomplish a functionality, composition take advantages of manipulating relationships and collaborations of several objects, which keep the scale of a class small and focused on a few functionality.

Disadvantages of Composition

Requirement of careful design avoiding accomplishing a functionality with too many objects.

Advertisements
 
Leave a comment

Posted by on December 31, 2010 in Technical notes

 

Tags: ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: