Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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()
{
 internal_model.Do_Some_Complex_Thing_To_Retrieve_The_Triangles();
}
...
}

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

share|improve this question

2 Answers 2

up vote 5 down vote accepted

What you accomplish is exactly correct.

However...

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.

share|improve this answer

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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