Jump to content
  • Advertisement
Sign in to follow this  
ExErvus

Adapter pattern and delegate objects

This topic is 703 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am studying Design Patterns by the gof, and I am having a little bit of a time trying to wrap my head around the idea of delegate objects in the context of the Adapter pattern. Is the purpose of a delegate to simply keep a narrow interface of the Adapter object to the client? That does not even seem necessary because the adapter already has an association with the client. The delegate pattern forms a circular association.

 

What am I missing here?

 

For those with the book, I am referring to page 144 - 145, Chapter 4

Share this post


Link to post
Share on other sites
Advertisement
Roughly speaking, the adapter pattern is when you make a wrapper to retrofit one interface to work as a different interface. Delegation is when you forward functionality to another object. A delegate is a very natural way to implement an adapter when you, assuming you approach problems by trying to slap design patterns into everything (which is terrible engineering).
 
class MyObject {
  public void something();
};

class Interface {
  abstract void act();
};

class Adapter inherits Interface {
  private ref MyObject _delegate;

  void act() { // Adapter is _adapting_ MyObject to Interface...
    _delegate.something(); // ... by delegating/forwarding to the Real object
  }
};

Share this post


Link to post
Share on other sites

Think of "delegation" in terms of composition (when we speak in terms of object oriented design).

You include a different behavior rather than contain it directly. Composition is one of the most powerful tools in design.

The adapter uses both composition and inheritance to chain the relevant behavior.

An example:

LegacySword is an old component which we want to wrap around our new design.

Class LegacySword

{ void Stab(...) {...} // Stab is a method from the old design, we might not change it so easily, so we create adaptation.

 

// The new mechanics.

interface IWeapon{

 void Attack();

}

 

class sword extends IWeapon{

 private:

  LegacySword instance;

public:

void Attack(...)

{ ... instance.Stab(...) ... }

 

}

 

It's a silly example but you might get the point. 

 

Use adapter when you deal with old code, if you write adapters in your new designs then something might be wrong with them

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!