design-patternsiteratoradaptor

Iterator vs. Adaptor design patterns


I am new to design patterns and lately I started reading Head First Design Patterns.

It seems to me that the Iterator design pattern is really similar to the Adaptor design pattern. I know that the Adaptor pattern creates an adaptor that helps codes A and B communicate while the client only interacts with the interface of code A.
I find the Iterator pattern really similar to that, especially in the example of the diner and the pancake house merging.You can check the example here.

So here is my question, is Iterator pattern a form of the Adaptor pattern?


Solution

  • These are two different patterns. Adapter is a Structural Pattern while Iterator is a Behavioral Pattern.

    Adapter:- Convert the interface of a class into another interface clients expect. Adapter lets classes work together that couldn't otherwise because of incompatible interfaces.

    Iterator: Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation.

    Reference: http://www.dofactory.com/Patterns/Patterns.aspx