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

diagram

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

    hunter.hunt(WildDogAdpater(WildDog()))
}
class Hunter
{
    func hunt(_ lion: Lion)
    {
        lion.roar()
    }
}
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 WildDogAdpater: Lion
{
    let wildDog: WildDog

    init(_ wildDog: WildDog)
    {
        self.wildDog = wildDog
    }

    func roar()
    {
        print("adapter's wild dog bark")
    }
}