Chapter 8.  Extending and Building Object-Oriented Code

In this chapter, we will put together many pieces of the object-oriented puzzle. We will take advantage of extensions to add features to classes, protocols, and types that we can't modify through source code editing. We will interact with a simple object-oriented data repository through Picker View and consider how object-oriented code is everywhere in an iOS app.

Putting together all the pieces of the object-oriented puzzle

In Chapter 1, Objects from the Real-World to the Playground, you learned how to recognize objects from real-life situations. We understood that working with objects makes it easier to write code that is easier to understand and reuse. You learned how to recognize real-world elements and translate them into the different components of the object-oriented paradigm supported in Swift: classes, protocols, properties, methods, and instances.

We discussed that classes represent blueprints or templates to generate the objects, which are also known as instances. We designed a few classes with properties and methods that represent blueprints for real-life objects. Then, we improved the initial design by taking advantage of the power of abstraction and specialized different classes. In Chapter 2, Structures, Classes, and Instances, you learned about an object's life cycle. We worked with many examples to understand how object initializers and deinitializers work. We declared our first class to generate a blueprint for objects. We customized object initializers and deinitializers and tested their personalized behavior in action with live examples in Swift's Playground. We considered how they work in combination with automatic reference counting.

In Chapter 3, Encapsulation of Data with Properties, you learned the different members of a class and how they are reflected in members of the instances generated from a class. We worked with instance properties, type properties, instance methods, and type methods. We worked with stored properties, getters, setters, and property observers, and we took advantage of access modifiers to hide data. We also worked with mutable and immutable versions of a 3D vector. We discussed the difference between mutable and immutable classes. Immutable classes are extremely useful when we work with concurrent code.

In Chapter 4, Inheritance, Abstraction, and Specialization, you learned how to take advantage of simple inheritance to specialize a base class. We designed many classes from top to bottom using chained initializers, type properties, computed properties, stored properties, and methods. Then, we coded most of these classes in the interactive Playground, taking advantage of different mechanisms provided by Swift. We took advantage of operator functions to overload operators that we could use with the instances of our classes. We overrode and overloaded initializers, type properties, and methods. We also took advantage of one of the most exciting object-oriented features: polymorphism.

In Chapter 5, Contract Programming with Protocols, you learned that Swift works with protocols in combination with classes. The only way to have multiple inheritance in Swift is through the usage of protocols. You learned about the declaration and combination of multiple blueprints to generate a single instance. We declared protocols with different types of requirements. Then, we created many classes that conform to these protocols. We worked with type casting to take a look at how protocols work as types. Finally, we combined protocols with classes to take advantage of multiple inheritance in Swift. We combined inheritance for protocols and inheritance for classes. In Chapter 6, Maximization of Code Reuse with Generic Code, you learned how to maximize code reuse by writing code capable of working with objects of different types; that is, instances of classes that conform to specific protocols or whose class hierarchy includes specific superclasses. We worked with protocols and generics. We also created classes capable of working with one or two constrained generic types. We combined inheritance, protocols, and extensions to maximize the reusability of code. We also made classes work with many different types. Generics are very important to maximizing code reuse in Swift. In Chapter 7, Object-Oriented Programming and Functional Programming, you learned how to refactor the existing code to take full advantage of object-oriented code. We prepared the code for future requirements, reduced maintenance cost, and maximized code reuse. We worked with many functional programming features included in Swift, and we combined them with everything we have discussed so far about object-oriented programming. We analyzed the differences between imperative and functional programming approaches for many algorithms.

Now, you will learn how to extend the existing classes to achieve our goals.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.201.55