Chris
Chris

Reputation: 6315

Is this the best use of the Adapter pattern?

Would using the adapter pattern be the best design to use when all you're trying to adapt to is a class that is holding fields and not methods or functions?

For example, would the following be the best way to approach this? I'm using C# if that makes any difference in your answers.

    NewClass 
     private Guid _guidId;

    AdpaterClass : NewClass
     private  Guid _guidId;

    LegacyClass : LegacyBaseClass
     private  Guid  _guidId;
     private  String _stringDescription;
     private  DateTime _dateTimeValue;

Why am I doing this?

I'm trying to get the LegacyClass and another Class down to the same baseline so that I can use a single common method to sort/filter on the _guidId. The common method would bring in a list of Guid's, check the _GuidId and see if it matches and then perfrom some function based on that. My goal is not to write the same sort/filter function more than once since they would be almost identical.

Thanks for your suggestions.

Upvotes: 3

Views: 517

Answers (1)

jason
jason

Reputation: 241601

I think what you are looking for is something like the following:

interface ICanHazGuidId {
    Guid GuidId { get; }
}

class NewClass : ICanHazGuidId {
    public Guid GuidId { get; private set; }
    // etc.
}

class AdapterClass : ICanHazGuidId {
    private LegacyClass legacy;
    public Guid GuidId {
        get {
            // obtain and return legacy._guidId;
        }
    }
    // constructor eating instance of LegacyClass etc.
}

Then just implement an IComparer<ICanHazGuidId> etc. If so, this would be a good use of the adapter pattern.

Upvotes: 10

Related Questions