I have 2 projects: A and B that should interactwith each other.
Project A introduce interface names ISpecialTask and Project B should implement it.
Projet B has an entity named TaskWithListOfProperties that cannot implement ISpecialTask because it has different structure of properties (in addition, all the system knows how to work with TaskWithListOfProperties and I don't want to change its structure).
So I decided to create a class named SpecialTaskFromListOfProperties that implements ISpecialTask and uses TaskWithListOfProperties instance in order to use it for the interaction between the projects.
interface ISpecialTask {
long Id{get;}
long WorkerId{get;}
long VehicleId{get;}
long CustomerId{get;}
}
class TaskWithListOfProperties {
IDictionary<string, long> Properties {get;
}
class SpecialTaskFromListOfProperties : ISpecialTask {
public SpecialTaskFromListOfProperties(TaskWithListOfProperties ins) {
...
...
}
public long Id { get{ ... } }
public long WorkerId { get{ ... } }
public long VehicleId { get{ ... } }
public long CustomerId { get{ ... } }
}
Is SpecialTaskFromListOfProperties actually the Adapter pattern?
What is the difference between the adapter pattern and decorator pattern?
Depends on what you're actually trying to achieve. Adapter and Decorator are pretty much similar, however when you implement an Adapter you bring no new logic besides conversion. When implementing a Decorator you actually bring in some brand new functionality that never existed before in the object you're decorating.
So, long story short, should interface properties Id
, WorkerId
etc be naturally coming from TaskWithListOfProperties
- then you should consider it as an Adapter. Otherwise it's a Decorator.