Swift - Design Pattern - Adapter

After seeing the basic of Swift comes the moment where you need to know some patterns. A design pattern, is a re-usable code that allow to solve common problem when designing an application. Today we’ll see the Adapter pattern.

Adapter, what for ?

An adapter is a wrapper that allow a class to work with a specific protocol where the parameter of the class are not sufficient to operate the said protocol. The most comprehensible post is IMHO, a problem that I found on the internet : You have a protocol with a function display that expect to receives “x1, y1, x2, y2” and a Rectangle class that is initialized with “x, y, width, height”. How are we going to make the Rectangle class compatible with the display function ? Yes, you are right: Adapter !

The different players

  • Target : The target is the interface that the adapter needs to implement
  • Adapter : The adapter is the class containing the logic allowing the adaptee and the target to work together.
  • Adaptee : The Adaptee is the class that need an adaptor

This is the rectangle class :

class Rectangle { // Adaptee
  let x: Double
  let y: Double
  let width: Double
  let height: Double
  
  init(x: Double, y: Double, width: Double, height: Double) {
    self.x = x
    self.y = y
    self.width = width
    self.height = height
  }
  
}

And here is the protocol uncompatible with the Rectangle class.

protocol Shape { // Target
  func display(x1: Double, y1: Double, x2: Double, y2: Double)
}

The adapter :

class RectangleAdapter: Shape { // Adapter
  var rectangle: Rectangle
  
  init(rectangle: Rectangle) {
    self.rectangle = rectangle
    let x2 = rectangle.x + rectangle.width
    let y2 = rectangle.y + rectangle.height
    
    self.display(x1: rectangle.x, y1: rectangle.y, x2: x2, y2: y2)
  }
  
  func display(x1: Double, y1: Double, x2: Double, y2: Double) {
    // BINGO
  }
}

Search