美文网首页
swift设计模式-原型模式

swift设计模式-原型模式

作者: 刘书亚的天堂之路 | 来源:发表于2016-11-02 15:41 被阅读27次

原型模式的本质,就是方便对一个对象继承的同时多态化处理

/*:
🃏 Prototype
------------

The prototype pattern is used to instantiate a new object by copying all of the properties of an existing object, creating an independent clone. 
This practise is particularly useful when the construction of a new object is inefficient.

### Example
*/
class ChungasRevengeDisplay {
    var name: String?
    let font: String

    init(font: String) {
        self.font = font
    }

    func clone() -> ChungasRevengeDisplay {
        return ChungasRevengeDisplay(font:self.font)
    }
}
/*:
### Usage
*/
let Prototype = ChungasRevengeDisplay(font:"GotanProject")

let Philippe = Prototype.clone()
Philippe.name = "Philippe"

let Christoph = Prototype.clone()
Christoph.name = "Christoph"

let Eduardo = Prototype.clone()
Eduardo.name = "Eduardo"
/*:
>**Further Examples:** [Design Patterns in Swift](https://github.com/kingreza/Swift-Prototype)
*/

相关文章

网友评论

      本文标题:swift设计模式-原型模式

      本文链接:https://www.haomeiwen.com/subject/duwvuttx.html