Now that we have highlighted the downsides of solving the objective without the adapter pattern, let’s see how the design pattern works and how it can help us.
The adapter pattern can be separated into three classes: The Target, the Adapter, and the Adaptee.
The target is the intended common functionality which we want all hotel classes to follow. This is therefore represented by an interface, which states the two main methods of all hotels: printAvailableRooms() and bookByRoomNumber.
The adapter is the implementation of the target interface. For each variation of hotel there needs to be a unique adapter, which specifies how to convert the functionality of the hotel into the desired functionality of the target interface.
The conversion logics are thereby hidden away inside its own class. This enables the main context to only work with the target interface. Thereby enabling decoupled code structure, as the interface versions of the individual hotels can be swapped between each other.
The final type of class, the adaptee, refers to the object, which the adapter is converting. In this case, that would be the individual hotel classes.
With the core concept of the adapter design pattern outlined, it has become time to investigate the class diagram.