headfirst設計模式swift版01

來源:互聯網
上載者:User

標籤:

headfirst設計模式這本書真好,準備用一個月學完。書裡講得很清楚了。

設計原則:

1、找出應用中可能需要變化之處,把它們獨立出來,不要和那些不需要變化的代碼混在一起。

2、針對介面編程,而不是針對實現編程。

書中的代碼是用java寫得,現在正用swift,便做個翻譯,沒啥技術含量。

protocol FlyBehavior {    func fly()  ->Void}protocol QuackBehavior {    func quack() ->Void}class CannotFly:FlyBehavior {    init() {            }    func fly()  {        println("不會飛")    }}class Quack:QuackBehavior {    init() {            }    func quack()  {        println("嘎嘎")    }}class Duck {    var flyBehavior:FlyBehavior?    var quackBehavior:QuackBehavior?        init() {            }    func swim(){            }    func display(){            }        func fly() {        flyBehavior?.fly()    }    func quack()  {        quackBehavior?.quack()    }}class MiniDuckSimulator:Duck {    override init() {        super.init()    }    }let miniDuck = MiniDuckSimulator()println("................")miniDuck.fly()miniDuck.flyBehavior = CannotFly()println("................")miniDuck.fly()

 

headfirst設計模式swift版01

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.