This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Entity One: Weapon | |
| protocol Weapon{ | |
| func fire(); // Weapon can be fire | |
| } | |
| // The Context: Player, who can fire a weapon | |
| class Player{ | |
| var currentWeapon:Weapon?; | |
| // Fire: call the `fire` method of the weapon |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // all implementation of Explosve that also implements Weapon, call `detonate` in `fire` method | |
| extension Explosive where Self:Weapon{ | |
| func fire(){ | |
| self.detonate() | |
| } | |
| } | |
| // Finally, extend Grenade to Weapon | |
| extension Grenade:Weapon{} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| struct WeaponAdapterForExplosive:Weapon{ | |
| var explosive:Explosive | |
| func fire(){ | |
| self.explosive.detonate() | |
| } | |
| } | |
| player.currentWeapon = WeaponAdapterForExplosive(explosive:Grenade()) | |
| player.fire() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| protocol Explosive{ | |
| func detonate() | |
| } | |
| struct Grenade:Explosive{ | |
| func detonate(){ | |
| print("A grenade exploded!") | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| let player = Player() | |
| player.currentWeapon = AK47() | |
| player.currentWeapon?.fire() // prints "Firing with AK47" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| struct AK47:Weapon{ | |
| func fire(){ | |
| print("Firing with AK47") | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Entity One: Weapon | |
| protocol Weapon{ | |
| func fire(); // Weapon can be fire | |
| } | |
| // The Context: Player, who can fire a weapon | |
| class Player{ | |
| var currentWeapon:Weapon?; | |
| // Fire: call the `fire` method of the weapon |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Player{ | |
| var currentWeapon:Weapon?; | |
| var currentBlade:Blade?; | |
| var currentExplosive:Explosive?; | |
| func select(weapon:Weapon){ | |
| self.currentWeapon = weapon; | |
| } | |
| func select(blade:Blade){ | |
| self.currentBlade = blade; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // This is the player definition | |
| class Player{ | |
| // Current weapon i.e. the current strategy/behavior | |
| var currentWeapon:Weapon?; | |
| // Dynamically sets the current weapon | |
| func select(weapon:Weapon){ | |
| self.currentWeapon = weapon; | |
| } | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // This is the protocol, that defines family of weapon | |
| protocol Weapon{ | |
| func fire(); // Weapon can be fire | |
| } | |
| // This is the player definition | |
| class Player{ | |
| // Current weapon i.e. the current strategy/algorithm | |
| var currentWeapon:Weapon?; | |
NewerOlder