The chain of responsibility pattern is useful when there are multiple objects that could take responsibility for a request but you don’t want to expose details of those objects to the calling component.
责任链模式:把两种/多种解决方案封装到到一个链里,提供一个封闭的接口一一尝试。
let messages = [
Message(from:"bob@example.com",to:"joe@example.com",
subject:"Free for lunch?"),
Message(from:"joe@example.com",to:"alice@acme.com",
subject:"New Contracts"),19)"> Message(from:"pete@example.com",to:"all@example.com",
subject: "Priority: All-Hands Meeting"),
];
iflet chain =Transmitter.createChain(true) {
for msgin messages {
let handled = chain.sendMessage(msg);
println("Message sent:\(handled)");
}
}
//1
struct Message {
let from:String;
let to:String;
let subject:String;
}
class Transmitter {
var nextLink:Transmitter?;
required init() {}
func sendMessage(message:Message,handled:Bool =false) ->Bool {
if (nextLink !=nil) {
returnnextLink!.sendMessage(message,handled: handled);
}elseif (!handled) {
println("End of chain reached. Message not sent");
}
return handled;
}
classfunc createChain(localOnly:Bool) ->Transmitter? {
let transmitterClasses:[Transmitter.Type]
= localOnly ? [PriorityTransmitter.self,LocalTransmitter.self]
: [PriorityTransmitter.self,LocalTransmitter.self,RemoteTransmitter.self];
var link:Transmitter?;
for tClassin transmitterClasses.reverse() {
let existingLink = link;
link = tClass();
link?.nextLink = existingLink;
}
return link;
}
privateclassfunc matchEmailSuffix(message:Message) ->Bool {
iflet index =find(message.from,"@") {
return message.to.hasSuffix(message.from[Range<String.Index>(start:
index,end: message.from.endIndex)]);
}
return false;
}
}
class LocalTransmitter :Transmitter {
overridefunc sendMessage(message:Message,var handled:Bool) ->Bool {
if (!handled &&Transmitter.matchEmailSuffix(message)) {
println("Message to\(message.to) sent locally");
handled =true;
}
returnsuper.sendMessage(message,handled: handled);
}
}
class RemoteTransmitter :Transmitter {
overridefunc sendMessage(message:Message,var handled:Bool) ->Bool {
if (!handled && !Transmitter.matchEmailSuffix(message)) {
println("Message to\(message.to) sent remotely");
handled =true;
}
returnsuper.sendMessage(message,handled: handled);
}
}
class PriorityTransmitter :Transmitter {
var totalMessages =0;
var handledMessages =0;
overridefunc sendMessage(message:Message,var handled:Bool) ->Bool {
totalMessages++;
if (!handled && message.subject.hasPrefix("Priority")) {
handledMessages++;
println("Message to\(message.to) sent as priority");
println("Stats: Handled\(handledMessages) of\(totalMessages)");
handled =true;
}
returnsuper.sendMessage(message,handled: handled);
}
}