赞
踩
PersonBr.swift
- //
- // PersonBr.swift
- // 23设计模式
- //
- // Created by 阳君 on 14/12/2.
- // Copyright (c) 2014年 六月. All rights reserved.
- //
-
- import Foundation
-
- /// PersonBr定义抽象类的接口。
- class PersonBr {
-
- /// 服装
- var clothing:Clothing?
- /// 标示
- var type:String = ""
-
- init() {
-
- }
-
- /** 桥接的动作*/
- func dress() {
-
- }
-
- }
a) Lady.swift
- //
- // Lady.swift
- // 23设计模式
- //
- // Created by 阳君 on 14/12/2.
- // Copyright (c) 2014年 六月. All rights reserved.
- //
-
- import Foundation
-
- /// LadyBr扩充由PersonBr定义的接口
- class LadyBr: PersonBr {
-
- override init() {
- super.init()
- super.type = "女人"
- }
-
- override func dress() {
- super.clothing?.personDressCloth(self)
- }
-
- }
b)ManBr.swift
- //
- // Man.swift
- // 23设计模式
- //
- // Created by 阳君 on 14/12/2.
- // Copyright (c) 2014年 六月. All rights reserved.
- //
-
- import Foundation
-
- /// ManBr扩充由PersonBr定义的接口
- class ManBr: PersonBr {
-
- override init() {
- super.init()
- super.type = "男人"
- }
-
- override func dress() {
- super.clothing?.personDressCloth(self)
- }
-
- }
Clothing.swift
- //
- // Clothing.swift
- // 23设计模式
- //
- // Created by 阳君 on 14/12/2.
- // Copyright (c) 2014年 六月. All rights reserved.
- //
-
- import Foundation
-
- /// Clothing定义实现类的接口,该接口不一定要与PersonBr的接口完全一致
- protocol Clothing {
-
- func personDressCloth(personBr:PersonBr)
-
- }
a) Jacket.swift
- //
- // Jacket.swift
- // 23设计模式
- //
- // Created by 阳君 on 14/12/2.
- // Copyright (c) 2014年 六月. All rights reserved.
- //
-
- import Foundation
-
- /// Clothes实现Clothing接口并定义它的具体实现
- class Clothes: Clothing {
-
- func personDressCloth(personBr: PersonBr) {
- print("\(personBr.type)穿衣服");
- }
-
- }
b) Trouser.swift
- //
- // Trouser.swift
- // 23设计模式
- //
- // Created by 阳君 on 14/12/2.
- // Copyright (c) 2014年 六月. All rights reserved.
- //
-
- import Foundation
-
- /// Trouser实现Clothing接口并定义它的具体实现。
- class Trouser: Clothing {
-
- func personDressCloth(personBr: PersonBr) {
- print("\(personBr.type)穿裤子");
- }
-
- }
- // 桥接模式
- let man:PersonBr = ManBr()
- let lady:PersonBr = LadyBr()
- let clothes:Clothing = Clothes()
- let trouser:Clothing = Trouser()
- clothes.personDressCloth(man)
- trouser.personDressCloth(man)
- clothes.personDressCloth(lady)
- trouser.personDressCloth(lady)
男人穿衣服
男人穿裤子
女人穿衣服
女人穿裤子
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。