swift - The Composite Pattern
来源:互联网 发布:淘宝开店考试答案2016 编辑:程序博客网 时间:2024/06/07 11:40
It shows you how the composite pattern can be used so that different kinds of objects in a tree data structure can be treated consistently.
复合模式:
这个设计模式为你展示了不同类型的在树中的数据结构如何一致的对待。
client:
let doorWindow =CompositePart(name: "DoorWindow", parts:
Part(name: "Window", price:100.50),
Part(name: "Window Switch", price:12));
let door =CompositePart(name: "Door", parts:
doorWindow,
Part(name: "Door Loom", price:80),
Part(name: "Door Handles", price:43.40));
let hood =Part(name: "Hood", price:320);
let order =CustomerOrder(customer: "Bob", parts: [hood, door, doorWindow]);
order.printDetails();
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
pattern:
protocol CarPart {
var name:String {get };
var price:Float {get };
func addPart(part:CarPart) ->Void;
func removePart(part:CarPart) ->Void;
}
class Part :CarPart {
let name:String;
let price:Float;
init(name:String, price:Float) {
self.name = name;self.price = price;
}
func addPart(part: CarPart) {
// do nothing
}
func removePart(part: CarPart) {
// do nothing
}
}
class CompositePart :CarPart {
let name:String;
private var parts:[CarPart];
init(name:String, parts:CarPart...) {
self.name = name;self.parts = parts;
}
var price:Float {
return reduce(parts,0, {subtotal, part in
return subtotal + part.price;
});
}
func addPart(part:CarPart) {
parts.append(part);
}
func removePart(part:CarPart) {
for index in0 ..< parts.count {
if (parts[index].name == part.name) {
parts.removeAtIndex(index);
break;
}
}
}
}
////
import Foundation
class CustomerOrder {
let customer:String;
let parts:[CarPart];
init(customer:String, parts:[CarPart]) {
self.customer = customer;
self.parts = parts;
}
var totalPrice:Float {
return reduce(parts,0, {subtotal, part in
return subtotal + part.price});
}
func printDetails() {
println("Order for\(customer): Cost:\(formatCurrencyString(totalPrice))");
}
func formatCurrencyString(number:Float) ->String {
let formatter = NSNumberFormatter();
formatter.numberStyle =NSNumberFormatterStyle.CurrencyStyle;
return formatter.stringFromNumber(number) ??"";
}
}
- swift - The Composite Pattern
- The ninth pattern --------Iterator and Composite Pattern
- Swift 10 组合模式 Composite Pattern
- swift - The Prototype Pattern
- swift - The Builder Pattern
- swift - The Adapter Pattern
- swift - The Decorator Pattern
- swift - The Facade Pattern
- swift - The Flyweight Pattern
- swift - The Proxy Pattern
- swift - The command pattern
- swift - The Factory Method Pattern
- swift - The Chain Responsibility pattern
- Iterator Pattern & Composite Pattern
- 7.5.1.1复合设计模式(THE COMPOSITE DESIGN PATTERN)
- Swift-中介者模式(The Mediator Pattern)
- Design Pattern学习笔记之迭代器模式和复合模式(the Iterator and Composite Pattern)
- Design Pattern(7)-Composite Pattern
- ViewDragHelper详解
- easy_install安装,更新模块 python
- HttpClientGet与HttpClientPost
- iPhone 6 / 6 Plus 设计·适配方案
- Cacti的安装与插件开发(一)
- swift - The Composite Pattern
- SQLite基础学习
- 转载:Ant学习笔记——自己构建Ant编译环境
- 思维导图-理清发展思路
- DCIM 用 UIScrollView 和UIPageControl
- Tire学习总结
- scala2.10.x case classes cannot have more than 22 parameters
- arm交叉编译器gnueabi、none-eabi、arm-eabi、gnueabihf、gnueabi区别
- 面试题:阿里 改造站点