原型模式的本质,就是方便对一个对象继承的同时多态化处理
/*:
🃏 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)
*/
网友评论