• Category: structural
  • Type: class
  • Motivation: make hunter hunt wild dog without change the hunter

diagram

do {
    let hunter = Hunter()
    hunter.hunt(AfricanLion())

    hunter.hunt(WildDogAdapter())
}
class Hunter
{
    func hunt(_ lion: Lion)
    {
        lion.roar()
    }
}
// hunter(client) -> lion (target)
protocol Lion
{
    func roar()
}

class AfricanLion: Lion
{
    func roar()
    {
        print("african lion roar")
    }
}
// hunter(client) -> WildDog (adpater) -> wildDog (adaptee)
class WildDog
{
    func bark()
    {
        print("wild dog bark")
    }
}

class WildDogAdapter: WildDog
{
    override func bark()
    {
        print("adapter's wild dog bark")
    }
}

extension WildDogAdapter: Lion
{
    func roar()
    {
        self.bark()
    }
}