Finally got around to doing another design pattern, the facade. This one was pretty relevant because we’re using it in our current project at work. I don’t think the NasaAPOD app I’ve been working on is a great example of using the facade because the facade is suppose to hide the complexity of a system behind a simple API. The app is already pretty simple, so the facade isn’t complex at all. If I was to fully utilize the facade pattern though, the ApiFacade.swift
file would also store and fetch from CoreData instead of fetching from the API every time. This is some pseudo-code of the difference in the fetch function.
/// Current Implementation
func fetchPhotoInfo(completion: @escaping([NasaPhotoInfo]) -> Void {
// fetch photos from api
}/// Implementation to fully utilize the facade
func fetchPhotoInfo(completion: @escaping([NasaPhotoInfo]) -> Void {
// check if photos are in core data
// return photos if in core data
// fetch photos from api
// store photos in core data
// return photos
}
Also since going through this facade architecture, I’ve done more with Codable
and realized I could clean up my code if I used it instead of doing the mess below.
To wrap up, this was a pretty easy pattern to implement. Like I said earlier, the current features of the app don’t really show off the facade pattern well since it doesn’t have a complex API. I might expand on the app to store to CoreData to just show it off more, but I’d do it in a separate branch because I don’t want to have to setup CoreData in the rest of the existing apps.
- a simpler interface to a complex subsystem.
- Hide complexity of a system, class or logic and provide all functionalities behind a simple interface.