![]() Please follow and read the hyperlinks I’ve included throughout the article. The model looks more like a candidate for dividing up features “horizontally” using protocols and composition as opposed to “vertically” as is the case with classes and inheritance. In fact, my file model doesn’t look like a good candidate for a lot of inheritance. ![]() You should also notice that I’ve avoided the pitfalls a starting with a base (abstract) “file” class, probably being tempted to place most file-related functionality in the base class, and then trying to specialize the base with inheritance. Apple claims that this prevention of unintended mutation makes it “easy to reason about your code.” See this link for more on value semantics.īy separating and organizing file system management features into protocols, you can see how my code schema can be easily reused, maintained, and extended. If I make a copy of an instance of AppFile, changes to the copy are not reflected in the original variable. Every variable of my AppFile type has an independent value. Protocol extensions allow me to define base/default functionality that can be easily overridden in structs that adopt from my existing protocols or in new protocols that inherit from my existing protocols. I can compose together as much or as little file-related functionality by adopting as many or as few protocols as I need. I can turn to composition as opposed to inheritance, while still having the opportunity to inherit from protocols if I like. Without deep knowledge of UML, you should be able to see how I’ve logically separated dissimilar functionality and, conversely, grouped similar operations together. Instead of going into a long-winded discussion of UML, I challenge you to learn a little bit about this design tool at these links here, here, here, here, and here. Before writing the code for this article, I created a design using a UML (Unified Modeling Language) diagram: You’ll see functionality that is logically broken into protocols, only one instance of protocol inheritance, and finally, to get the core functionality, the composition of four protocols into one struct that defines an iOS local file. ![]() You’ll see no classes or class inheritance trees in this tutorial. ![]() If you’re not familiar with these concepts, please read my articles on protocols, an introduction to POP, using advanced POP, and value semantics. I’ve written extensively about POP and value semantics in previous posts. In this tutorial, I’m going to show you how to develop support for manipulating files inside your app’s sandboxed directory structure - but, my code has been designed and written in Swift by leveraging two relatively new methodologies: protocol-oriented programming (POP) and value semantics. PROTOCOL-ORIENTED PROGRAMMING AND VALUE SEMANTICS ![]()
0 Comments
Leave a Reply. |