From the course: Practical Design Patterns in Swift
Unlock the full course today
Join today to access over 22,600 courses taught by industry experts or purchase this course individually.
Request processor - Swift Tutorial
From the course: Practical Design Patterns in Swift
Request processor
- [Instructor] We're going to implement the chain of handlers that can process specific request types. I'll start with the request handling protocol. All request handlers will need to adopt it. Protocol RequestHandling. The protocol declares an initializer that takes an argument of type RequestHandling. The next parameter represents the next responder in the chain. It's optional, so callers can also pass in nil that would mark the end of the chain. Next we'll declare the method responsible for processing a specific request. It can accept any type of request. If a concrete responder doesn't deal with a particular request, it must forward it to the next responder. We completed the protocol. Now let's implement the type that adopts the RequestHandling protocol. Instead of creating multiple concrete types, I'm going to define the generic handler class with a type parameter called T. I make it conform to the RequestHandling protocol and also define it as a final class to prevent the…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.