Quite frequently, I use an Adapter-like design to expose different aspects of data objects.

For example, in a 3D modelling application I use an object hierarchy to represent the 3d models in the scene. This hierarchy contains lots of internal information and is quite complex to traverse.

For exporting the 3D model data to different file formats (which involves quite significant transformation and omitting or converting some objects), I have a separate class called ModelAdapter. It extracts the important information of a 3D model and provides them in a different format, usable for export. The export plugins, in turn, read the data from the ModelAdapters and have nothing to do with the internal scene data structure.

For Example:

class 3DModelAdapter
private 3DModel internal_model;
public List GetTriangles()

Is this a valid use of the Adapter pattern? Is there a better way to accomplish the same thing?


What you accomplish is exactly correct.


Adapter is primarily when you have an existing expected interface that you need to which you need to adapt: "Convert the interface of a class into another interface clients expect. Adpater lets the classes work together that couldn't otherwise because of incompatible interfaces"

Facade is primarily when you have want to abstract the complications of a subsystem to make it easier to consume: "Provide a unified interface to a set of interfaces in a subsystem. Facade defines a higher-level interface that makes the subsystem easier to use"

If your intent is to be able to use a 3d modeling subsystem but its actual interface differs from the interface that you expect (perhaps because you are able to swap out 3d modeling subsystems at run-time) then I would call it an adapter.

If your intent is really that you want to make the actual 3d modeling subsystem easier to use, I would call it a facade.

Just semantics (and one of the benefits of patterns is the power that a single word carries in its ability to communicate intent), what you did is a good way to accomplish your goal.

| improve this answer | |

Yes. This is exactly when you want to use the adapter pattern.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.