标签:set uml rri alt leave enc 解耦 tle bsp
一,概念:
责任链模式(Chain of Responsibility Pattern)为请求创建了一个接收者对象的链。这种模式给予请求的类型,对请求的发送者和接收者进行解耦。这种类型的设计模式属于行为型模式。在这种模式中,通常每个接收者都包含对另一个接收者的引用。如果一个对象不能处理该请求,那么它会把相同的请求传给下一个接收者,依此类推。
主要解决职责链上的处理者负责处理请求,客户只需要将请求发送到职责链上即可,无须关心请求的处理细节和请求的传递,所以职责链将请求的发送者和请求的处理者解耦了。
二,UML图:
Handler定义一个处理请示的接口;
ConcreteHandler具体处理者类,处理它所负责的请求,可访问它的后继者,如果可以处理该请求,就处理,否则就将该请求转发给它的后继者。
Handler 里面聚合它自己,在 HandlerRequest 里判断是否合适,如果没达到条件则向下传递,向谁传递之前 set 进去。
最重要的两点,一个是你需要事先给每个具体管理者设置它的上司是哪个类,也就是设置后继者。另一点就是你需要在每个具体管理者处理请求时,做出判断,是可以处理这个请求,还是必须要“推卸责任”,转移给后继者去处理。
三,使用场景:
1、有多个对象可以处理同一个请求,具体哪个对象处理该请求由运行时刻自动确定。
2、在不明确指定接收者的情况下,向多个对象中的一个提交一个请求。
3、可动态指定一组对象处理请求。
四,代码实例:
struct Leaving { var title: String var reasone: String var from: String var position: String var leaveDaysNum: Float }
protocol Leader { var superior: Leader? {get set} var leaderPosition: String {get set} var controlLeavingDataTimeMix: Float {get set} var controlLeavingDataTimeMax: Float {get set} func handleLeaving(leaving: Leaving) -> Bool; } class CommonLeader: Leader { var superior: Leader? var leaderPosition: String var controlLeavingDataTimeMix: Float var controlLeavingDataTimeMax: Float init(superior: Leader?, position: String, mixTime: Float, maxTime: Float) { self.superior = superior leaderPosition = position controlLeavingDataTimeMax = maxTime controlLeavingDataTimeMix = mixTime } } extension Leader{ func handleLeaving(leaving: Leaving) -> Bool { if leaving.leaveDaysNum >= controlLeavingDataTimeMix, leaving.leaveDaysNum <= controlLeavingDataTimeMax { print("I‘m \(leaderPosition), I agree!") return true }else if leaving.leaveDaysNum > controlLeavingDataTimeMax{ if let superboss = superior{ print("I‘m \(leaderPosition), Your leave longer than my control,i should ask my superior!") return superboss.handleLeaving(leaving: leaving) }else{ print("I‘m \(leaderPosition), Your leave time is too long, I‘m against it") return false; } }else{ print("I‘m \(leaderPosition), you have a problem with leaving time thus can‘t agree with you.") return false } } } class TeamLeader: CommonLeader { init(superior: Leader?) { super.init(superior: superior, position: "Team Leader", mixTime: 0.5, maxTime: 1.0) } } class DepartmentHead: CommonLeader { init(superior: Leader?) { super.init(superior: superior, position: "Department Head", mixTime: 1.5, maxTime: 7.0) } } class CompanyManager: CommonLeader { init(superior: Leader?) { super.init(superior: superior, position: "Company Manager", mixTime: 7.5, maxTime: 31.0) } }
class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() let leaving = Leaving(title: "I‘d like to have paternity leave.", reasone: "My wife would have a baby in recent days.", from: "Ternence", position: "iOS engineer", leaveDaysNum: 15.0) let CM = CompanyManager(superior: nil) let DH = DepartmentHead(superior: CM) let TL = TeamLeader(superior: DH) print(TL.handleLeaving(leaving: leaving)) } }
标签:set uml rri alt leave enc 解耦 tle bsp
原文地址:https://www.cnblogs.com/yangzigege/p/8969883.html