Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Guilherme Kunzler
1,433 PointsProtocols on Swift iOS
In one of the examples give by Pasan from Apple's Library protocols are doing a bit more than making sure the classes conform to it.
They are allowing objects from one class to access methods within other classes.
This line of code "let generator: RandomNumberGenerator" on the Dice class is allowing the "var d6" that is of type "Dice" to access a function "func random() -> Double" that is outside of "Dice" scope and inside "LinearCongruentialGenerator" scope and is using the "RandomNumberGenerator" to make this bridge.
Also allowing to do the following call "d6.generator.random()" when again ".ramdom()" is not on "Dices" scope.
I would like to know more why that happens and were/what to research to learn more about it as the example is very loose in the class and it is quite fundamental to understand the next class about delegates. As it is also poorly explained in the delegates class.
protocol RandomNumberGenerator { func random() -> Double }
class LinearCongruentialGenerator: RandomNumberGenerator { var lastRandom = 42.0 let m = 139968.0 let a = 3877.0 let c = 29573.0
func random() -> 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() -> Int {
return Int(generator.random() * Double(sides)) + 1
}
}
var d6 = Dice(sides: 6, generator: LinearCongruentialGenerator())
1 Answer
Enrique Munguía
14,311 PointsThis is a concept called Composition, from Wikipedia:
Object composition (not to be confused with function composition) is a way to combine simple objects or data types into more complex ones.
Composition allows us create data types that are built upon simpler types, for example aun automobile has or is composed from objects including steering wheel, seat, gearbox and engine. This relation might be fixed in a program as a composition relation.
In the example the d6 object from class Dice has a generator of random numbers, it does not matter that the method random is not in scope of Dice, because it has a generator that can do that job. If it were not for composition the Dice class should have its own random method, possibly duplicating code. The use of protocols is not necessary to compose objects, you can do it with simple classes or structs, but in the example the protocol gives extra flexibility because you can plug in different types of generators to Dice.
Guilherme Kunzler
1,433 PointsGuilherme Kunzler
1,433 PointsThanks for the answer Enrique. Let me see if I got it.
I've attempted to exemplify compositions in the code bellow and if it represents composition I imagine it can be achieved without protocols or delegates.
If that is the truth I am not so sure I would say a class just about composition would be really helpful before jumping into protocols and delegates that makes use of composition.
class A {
}
class B {
}
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()