views:

148

answers:

1

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.

+8  A: 

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.

Jason
interface ICanHazGuidID { ... }
Juliet
@Juliet: That's worth editing for.
Jason
+1 for the clever interface naming, while staying lockstep with MS's recommended naming conventions (lead "I" for interfaces.) Well played.
jro
Clever. I like it.
Chris