设计模式 -1-SRP单一职责原则

//: Playground - noun: a place where people can play


import UIKit


var str = "Hello, playground"

/*

 单一职责原则(SRP)Single Responsibility Principle

 

 应该有且仅有一个原因引起类的变更

 

 好处:

    1.类的复杂性降低,实现什么指责都有清晰明确的定义

    2.复杂性降低,可读性提高

    3.可维护性提高

    4.变更引起的风险降低

 

 

 */

//单一职责

//打电话分了UserBoUserBiz两个接口,一个接口负责

protocol UserBo {                   //协议管理

    ///拨电话

    func dial(phonenum:String)

    ///挂电话

    func hangup()

}


protocol UserBiz {                  //数据传送

    func chat(o: ChatTool)

}




protocol ChatTool {

    func chatBy() -> String

}

class Dinaxin: ChatTool {

    func chatBy() -> String{

        return "电信"

    }

}

class Yidong: ChatTool {

    func chatBy() -> String{

        return "移动"

    }

}

class UserPhone: UserBo,UserBiz {

    

    func dial(phonenum: String) {

        debugPrint("拨电话" + phonenum)

    }

    func chat(o: ChatTool) {

        

        debugPrint("通过" + o.chatBy() + "通话")

    }

    func hangup() {

        debugPrint("挂电话")

    }

}

class Test{

    var userPhone:UserPhone!


    func test(){

        userPhone = UserPhone()

        userPhone.dial("1391496862")

        userPhone.chat(Yidong()) //只修改这里就能改通话方式

        userPhone.hangup()

    

    

    }

}


Test().test()









  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值