Adapter pattern is very useful when you want to use the methods/implementation that one particular class provides whose interface is incompatible with the current target interface. Adapter pattern provides one main feature:

- It lets classes work together whose interfaces are incompatible by implementing an adapter class.

Lets consider we have 2 classes that implement their own interface, “SingleLoader” implements “ISingleLoader” and “QueueLoader” implements “IQueueLoader”. “SingleLoader” can only load one item with “load()” method and “QueueLoader” can load several items at anytime with “queueLoad()” method.

Suppose we need to load one item and can only use the “queueLoad()” method that interface “IQueueLoader” provides. We can create an adapter for it to use with “SingleLoader” ’s “load()” method that only loads one item. Lets call the adapter “SingleLoaderAdapter” that implements “IQueueLoader”.

package {
	public class SingleLoaderAdapter implements IQueueLoader {
		// Properties
		private var __loader:SingleLoader;
		// Constructor
		function SingleLoaderAdapter(loader:SingleLoader) {
			__loader = loader;
		}
		// queueLoad method from IQueueLoader
		public function queueLoad():void {
			__loader.load()
		}
	}
}

We need “SingleLoader” instance reference that we are adapting and it is very similar to object composition, main usage:

package {
	public class Main extends Sprite {
		// Constructor
		function Main () {
			var singleLoader:ISingleLoader = new SingleLoader();
			var loaderAdapter:IQueueLoader= new SingleLoaderAdapter(singleLoader);
			// This will call singleLoader's load() method
			loaderAdapter.queueLoad();
		}
	}
}

We are creating an instance of “SingleLoaderAdapter” class that calls “queueLoad()” method defined in “IQueueLoader” interface.

Happy patterning:)