Protocol extension to implement various behaviors

Quick question: for example, you have a Bark protocol:

protocol MakeSound {
   func bark()
}

The superclass Dog, which implements the cortex and also floats:

class Dog: MakeSound {
}

Then the various types of dogs that expand this:

class Poodle: Dog {
}

class GermanShephard: Dog {
}

class SheepDog: Dog {
}

But the poodles ride, they don’t bark ... all the dogs bark, they just do it differently ... How can I give them Bark's specific behavior?

Extend the protocol? ...

extension MakeSound {
    func bark()
    func yapper()
}

But then Poodles and German Shepards have both behaviors (German Sheppard giggles ?!)

If I make 2 extensions and check the type class using where Self =?

extension MakeSound where Self: GermanShephard {
    func bark() {
        print("Bark")
    }
}

extension MakeSound where Self: Poodle{
    func yapper() {
        print("yap yap")
    }
}

But I can only check one type of class or type of dog. SheepDogs also bark, but I can't check here ...

. Java . , Swift, ?

+4
4

, , .

bark() , . , , :

protocol Bark {
   func bark()
}

//default implementation
extension Bark {
      func bark() { print("Bark") }
}

class Dog: Bark {}

//By calling bark func in Poodle, you change the default implementation.
class Poodle: Dog {
   func bark() { print("Yap") }
}

class GermanShephard: Dog {
     func bark() { print("Woof") }
}

let dog = Dog()
let poodle = Poodle()
let germanShephard = GermanShephard()

dog.bark()
//Bark
poodle.bark()
//Yap
germanShephard.bark()
//Woof

- .

:

, . , . , , Dog DogRepresentable , . DogRepresentable, self: UIViewController :

protocol Barkable {
    func bark()
}

protocol DogRepresentable: Barkable {
//properties and functions all dogs will have with same implementation
}

extension DogRepresentable where Self: UIViewController {
//default implementation for functions all dogs will use
}

Barkable DogRepresentable, , , DogRepresentable, Barkable.

, DogRepresentable , , , bark() :

class Dog: DogRepresentable {
   func bark() { print("Bark") }
} 

class Poodle: DogRepresentable {
   func bark() { print("Yap") }
}

 class GermanShephard: DogRepresentable {
    //Won't conform because it doesn't have bark()
}

, , .

2 :

DogRepresentable Barkable, : , Yappable, . Barkable . , . Yappable, Barkable.

, , , , .

+3

.

protocol DogSoundMaker {
    func makeSound()
}

protocol Barker: DogSoundMaker {}
protocol Yapper: DogSoundMaker {}
protocol Woofer: DogSoundMaker {}

extension Barker {
    func makeSound() { print("Bark") }
}

extension Yapper {
    func makeSound() { print("Yap yap, I am a glorified rodent") }
}

extension Woofer {
    func makeSound() { print("Woof") }
}

struct Poodle: Yapper {}
struct GermanShephard: Barker {}
struct SheepDog: Woofer {}

Poodle().makeSound()
GermanShephard().makeSound()
SheepDog().makeSound()
+2

, makeSound dogYap dogBark. , , . ( ) dogYap dogBark, , .

protocol MakeSound {
    func makeSound()
    var canBark: Bool { get }
    var canYap: Bool { get }
}

protocol dogBark: MakeSound {
    func makeSound()
}

protocol dogYap: MakeSound {
    func makeSound()
}

extension dogYap {
    func makeSound() {
        print("Yap")
    }
}

extension dogBark {
    func makeSound() {
        print("bark")
    }
}

extension MakeSound {
    func makeSound() {}
    var canBark: Bool { return self is dogBark }
    var canYap: Bool { return self is dogYap }

}

class Dog {
    var age: Int?
    var colour: UIColor?

}

class Poodle: Dog, dogYap {
}

class GermanShephard: Dog, dogBark  {
}

class SheepDog: Dog, dogBark {
}

//German shephard and Belgian bark in the same way
let germanShep = GermanShephard()
germanShep.makeSound()
germanShep.canBark
germanShep.canYap


let sheepDog = SheepDog()
sheepDog.makeSound()
sheepDog.canBark
sheepDog.canYap

let poodle = Poodle()
poodle.makeSound()
poodle.canBark
poodle.canYap
0

, :

import UIKit

protocol DogSoundMaker {}
protocol Barker: DogSoundMaker {}
protocol Yapper: DogSoundMaker {}

extension DogSoundMaker{
    var canBark: Bool { return self is Barker }
}
extension Barker {
    func makeSound() {
        print("Bark")
    }
}
extension Yapper {
    func makeSound() {
        print("Yap")
    }
}

class GermanShepherd: Barker {

}

class Poodle: Yapper{

}

class Chiwawa: Yapper {

}

var germanShep = GermanShepherd()
var poodleDog = Poodle()
poodleDog.makeSound()
poodleDog.canBark
germanShep.canBark
germanShep.makeSound()
0

Source: https://habr.com/ru/post/1663825/


All Articles