title: Protocols and composition on Swift
body: <p>I am trying to learn more about protocols and got stuck without understanding what is going on on the following piece of code. Mostly seeking some light and direction for good articles and pieces of explanation basically.</p>
  
  <p>In one of the examples from Apple's Library protocols are doing a bit more than making sure the classes conform to it. </p>
  
  <p>They are allowing objects from one class to access methods within other classes without using the traditional class inheritance definition.  </p>
  
  <p>This line of code  <code>let generator: RandomNumberGenerator</code> on the Dice class is allowing the <code>var d6</code> that is of type <code>Dice</code> to access a function <code>func random() -&gt; Double</code> that is outside of <code>Dice</code> scope and inside <code>LinearCongruentialGenerator</code> scope and is using the <code>RandomNumberGenerator</code> to make this bridge.</p>
  
  <p>Also allowing to do the following call <code>d6.generator.random()</code> when again <code>.ramdom()</code> is not on <code>Dices</code> scope.</p>
  
  <pre><code>protocol RandomNumberGenerator {
      func random() -&gt; Double
  }
  
  class LinearCongruentialGenerator: RandomNumberGenerator {
      var lastRandom = 42.0
      let m = 139968.0
      let a = 3877.0
      let c = 29573.0
  
      func random() -&gt; Double {
          lastRandom = ((lastRandom * a + c) % m)
          return lastRandom/m
      }
  }
  
  class Dice {
      let sides: Int
      let generator: RandomNumberGenerator
  
      init(sides: Int, generator: RandomNumberGenerator) {
  
          println(generator.random())
  
          self.sides = sides
          self.generator = generator
      }
  
      func roll() -&gt; Int {
          return Int(generator.random() * Double(sides)) + 1
      }
  }
  
  var d6 = Dice(sides: 6, generator: LinearCongruentialGenerator())
  </code></pre>
  
  <p><strong><em>Update to question</em></strong></p>
  
  <p>Thanks for the answers! By doing some research I think I just touched composition. So I wrote the code bellow to exemplify composition a bit better, without using protocols or delegates. Just pure composition. Please let me know if I got it right as it may help other people trying to understand composition.  </p>
  
  <pre><code>class A {
  
      var a1: String
  
      init (valueToA: String){
          self.a1 = valueToA
      }
  
      func aFunc1() -&gt; A {
          return self
      }
  }
  
  
  class B {
  
      var b1: A
  
      init (valueToB: A ) {
          self.b1 = valueToB
      }
  
      func bFunc1(){
          println("I am bFunc and I am calling aFunc \(b1.aFunc1())")
      }
  
  }
  
  var myA = A(valueToA: "Initiated myA with this string")
  //myA.aFunc1()
  
  var myB = B(valueToB: myA)
  myB.b1 = A(valueToA: "a value to B")
  myB.b1.aFunc1()
  </code></pre>
  
  <blockquote>
    <p>The same code but now with protocols</p>
  </blockquote>
  
  <pre><code>protocol myProtocol {
      func protocolFunc(value: String) -&gt; String
  }
  
  class A: myProtocol {
  
      var a1: String
  
      init (valueToA: String){
          self.a1 = valueToA
      }
  
      func aFunc1() -&gt; A {
          return self
      }
  
      func protocolFunc(value: String) -&gt; String {
          return value
      }
  
  }
  
  class B {
  
      var b1: A
      var b2: myProtocol
  
      init (valueToB1: A, valueToB2: myProtocol ) {
          self.b1 = valueToB1
          self.b2 = valueToB2
      }
  
      func bFunc1(){
          println("I am bFunc and I am calling aFunc \(b1.aFunc1())")
      }
  
      func callProtocolFuncOnA (value: String) {
          b1.protocolFunc(value)
      }
  
  }
  
  var myA1 = A(valueToA: "my A 1 created")
  var myA2 = A(valueToA: "my A 2 created")
  
  
  var myB = B(valueToB1: myA1, valueToB2: A(valueToA: "my A 3 created"))
  
  myB.callProtocolFuncOnA("calling other function")
  </code></pre>