Everyday Data Structures by William Smith

Everyday Data Structures by William Smith

Author:William Smith [Smith, William]
Language: eng
Format: epub, azw3
Publisher: Packt Publishing
Published: 2017-03-13T22:00:00+00:00


We would need a contract

If you look carefully at each of the three solutions to the logged in user business problem, you will probably notice they all share common public methods. In our array implementation, list implementation, and set implementation, we have two public methods named UserAuthenticated() and UserLoggedOut(), or some variation of these names depending on the language. This would not be an issue if we were to just choose one implementation that best suits our needs and move on. However, what if there were justifiable reasons to keep each one of these classes in our solution to efficiently work within specific environmental conditions?

In actuality, it is very common to see multiple classes that share the same public-facing methods but have uniquely implemented functionality under the hood. If we were to simply create three (or more) separate implementations that are completely independent of one another, our application would have a resulting code smell. That's because, whenever we want to use a specific implementation, we will need to call for it by name, which requires some advance knowledge of which classes and implementations are available. Plus, although our code might work just fine, it will be fragile, inextensible, and more difficult to maintain in the long term.

A better solution would involve defining a contract that each class implements. In C# or Java, we would define an interface, while in Objective-C and Swift, we would define a protocol. The difference between these two patterns is mostly semantics, as they will both provide our caller with the names of the methods, what the methods expect, and what the methods will return. What is important is that by doing this, we greatly simplify and harden our implementation of both the functionality and the calling class structures.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.