4

Decorators of the same name

 2 years ago
source link: https://www.codesd.com/item/decorators-of-the-same-name.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Decorators of the same name

advertisements

well, I'm going to be direct, it's about the decorator pattern you'll see, i made this graph to exemplify the situation

i have two classes (lets say: CAR and VAN) which are related as the graph says, and i need

to freely combine two decorator classes .. but i have one decorator that is applicable just for one of them since these decorators add specific functionality but this functionality is essentially the

same for the correspondent class..

can I name them just the same?

following the same graph i tried to inherit VAN from CAR. that way they both has the same decorators.. but as ia said

VAN can't have liquefied gas"

so my question remains.. can i name they both decorator the same?

thanks a lot in advance

gmocamilo

http://www.depositodeimagenes.com/images/h2bpM.jpg

pd. im sorry but the system doesn't allow me upload figures


Having two classes do the same thing and have the same name is generally a bad practice, like all code duplication. The FourWheelDrive decorator should be directly descending from Vehicle and accept a Vehicle as decoratee. The LiquidGas decorator can descend from SimpleCar and accept a SimpleCar as decoratee, if you want to restrict its use to SimpleCar:

class LiquidGasDecorator extends Vehicle
{
   private SimpleCar simplecar_;
   public LiquidGasDecorator(SimpleCar simplecar)
   {
     simplecar_ = simplecar;
   }
}

Vehicle fourwheelvan = new FourWheelDriveDecorator(new Van());
Vehicle fourwheelcarwithlpg = new LiquidGasDecorator(new FourWheelDriveDecorator(new SimpleCar)));




About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK