Decorator drawing in C # without inheritance. It's right?

public interface IMovable { void Move(); } public interface IUnloadable { void Unload(); } public class Vehicle : IMovable { public void Move() { Console.Write("moving"); } } public class Truck : IMovable, IUnloadable { private Vehicle Veh; public Truck(Vehicle veh) { this.Veh = veh; } public void Move() { Veh.Move(); Console.Write("reverse with beepy noise as well"); } public void Unload() { Console.Write("Unload"); } } 

If this is a decorator template. What is the difference between decorator pattern and composition? I saw examples of this pattern where inheritance is used. For example, a Java example on Wikipedia.

I don’t see the need to use inheritance at all, or am I missing something?

+8
inheritance c # design-patterns composition
source share
2 answers

A decorator’s drawing, by definition, will implement the same interface as the component that it decorates. The idea is that client code does not need to be changed. He can rely on the same abstraction as before.

For example:

 public interface IMovable { void Move(); } public class Truck : IMovable { public void Move() { Console.Write("moving"); } } public class NoisyMovable : IMovable //1.Implement same interface { private IMovable movable; public NoisyMovable(IMovable movable)//2.Wrap same interface { this.movable = movable; } public void Move() { movable.Move(); Console.Write("Make noise"); } } 

If you check out the NoisyMovable class, this is a decorator because it implements the IMovable abstraction and wraps the same thing.

With this you do not need to create many classes such as NoisyVehicle , NoisyTruck , NoisyCar , etc. Just enough Car , Truck ; You can add noise using one decorator.

 IMovable movable = new NoisyMovable(new Truck ());//Noisy Truck IMovable movable = new NoisyMovable(new Car());//Noisy car //etc 

On the other hand, on the other hand, it is not necessary to wrap the same implementation of the interface that it implements. It can implement one interface and wrap any other.

That you are mistaken, your Truck class accepts an instance of Vehicle . It should not; instead, it should accept any instance of IMovable . It should work with any IMovable implementation.

+8
source share

What is the difference between decorator pattern and composition?

Conceptually, the decorator changes the behavior of the (one) object that he is wrapping, while the composite (template) has behavior based on the combination of (several) objects that he aggregates.

I don’t see the need to use inheritance at all, or am I missing something?

Inheritance is useful, so you can have multiple decorators and embed them. For example, SelfDriver , which wraps Truck , which wraps Vehicle . Ignoring IUnloadable , the second decorator will be SelfDriver :

Class diagram in PlantUML

The code will look like this:

 IMovable sd = new SelfDriver(new Truck(new Vehicle()))); 

The object diagram will look like this:

Object diagram in PlantUML

+1
source share

All Articles