inheritance or decorator pattern

royjayd
royjayd used Ask the Experts™
on
one of the question asked today in interview was how does Inheritance differ from
using decorator pattern?
I said inheritance is adding behaviour during compile time v/s decorator adds behaviour to object at runtime. The interviewer wasnt satisfied. she was hinting more towards what is the drawback of inheritance which makes decorator pattern better? or are they very similar or same?

your take?

thx
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Awarded 2011
Awarded 2011
Commented:

This has both points which you mentioned:
http://www.coderanch.com/t/503582/patterns/Decorator-Pattern-Vs-Inheritance

Adding new behavior to the base class by creating separate subclass for every possible combination could let to a maintenance nightmare. There are better alternative available to Inheritance.

I very much agree with this statment about inheritance - in many cases just a nightmare
Awarded 2011
Awarded 2011

Commented:

http://www.patternsforphp.org/doku.php?id=decorator
This is a good discussion of all aspects of these, based on PHP, not java, but in such general discussions it does not  matter much

Altering the behaviour of objects is a tricky business. In many cases you do not wish to alter the original object but rather tack on additional or modified behaviours. The easiest way to achieve this is using inheritance. Creating a series of subclasses allows a programmer to add additional methods and properties or alter those which already exist in the parent class. Unfortunately, as other Design Patterns often point out, inheritance can be a path full of pitfalls and traps for the unwary. The more layers exist in a class hierarchy, the more cumbersome and difficult to manage the whole structure becomes.

If we decide to set a goal of maintaining the original class as is and of disallowing any further inheritance, we immediately gain some measure of control. By preventing further inheritance we have a smaller discrete unit perfect for reuse in other projects minus the specific modifications current circumstances might require. To extend the original class and add new or modified behaviour to it we can use the Decorator Pattern.

The Decorator Pattern creates a separate class family which adds extended behaviour to an existing class while leaving the existing class unchanged. As the class is ported for reuse, we can transparently add new Decorators specific to each new application as we see fit. Additionally, since the Decorator classes are in a separate hierarchy they have a variable type which is distinct from the original class, i.e. we can tell if a class is a Decorator.

Of course the Decorator Pattern is not a silver bullet. Inheritance remains the simplest option and the Decorator Pattern is not suitable for all scenarios. However it remains an extremely useful option and is one of the more popular Design Patterns programmers are intimately familiar with, especially in web application development.
Mick BarryJava Developer
Top Expert 2010

Commented:
Inheritance forces you to create a new class to extend functionality
Whereas with a decorator you can extend the functionality without creating a new class
Exploring SQL Server 2016: Fundamentals

Learn the fundamentals of Microsoft SQL Server, a relational database management system that stores and retrieves data when requested by other software applications.

Java Developer
Top Expert 2010
Commented:
Further to that Inheritance requires you to create a new subclass for every different extension you require, so you could end up with a large number of subclasses.
Decorator pattern on the other hand allows you to do it with a single class.

Author

Commented:
>>Inheritance requires you to create a new subclass for every different extension you require, so you could end up with a large number of subclasses.
Decorator pattern on the other hand allows you to do it with a single class.

can you give an example ?

thanks guys
Mick BarryJava Developer
Top Expert 2010

Commented:
Awarded 2011
Awarded 2011

Commented:


This is a very lively text talking about complexity of inheritance with examples
and of advantages of composition in many cases
http://tiedyedfreaks.org/eric/CompositionVsInheritance.html
Very short answer

inheritance has is-a relationship while decorator has has-a relationship

Author

Commented:
ajay, intresting, i thought has-a relationship was the strategy pattern :)

has-a relationship is used for making aggregation, composition. Now,
depending on scenario, the usage might be called as decorator, strategy etc

Author

Commented:
I have a base class and 3 concrete classes. The 3 concrete classes extend the base class.
public class BaseBusinessService {
      protected String getTemplateName(String id){
            String templateName = null;
             //application logic
                  }
            return templateName;            
      }
}

I have 3 concrete classes
public class ManagerBusinessService extends BaseBusinessService{
public boolean process(String Id){            
            boolean managerprocessStatus = true;                   
            String template = getTemplateName(Id);
            //work with template
            return managerprocessStatus ;
      }
}

public class CustomerBusinessService extends BaseBusinessService{
public boolean process(String Id){            
            boolean customerprocessStatus = true;            
            String template = getTemplateName(Id);
            //work with template
            return customerprocessStatus ;
      }
}

public class DirectorBusinessService extends BaseBusinessService{
public boolean process(String Id){            
            boolean directorprocessStatus = true;            
            String template = getTemplateName(Id);
            //work with template
            return directorprocessStatus ;
      }
}

So clearly i am using is- relation ship above. How can i convert the above code to has-a relationship Decorator pattern?
thanks

Author

Commented:
can anyone help here in putting the thoery into practice?

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial