Cross Cutting Concerns Point the way to Protocol Based Implementations

updated over 1 year ago

This proposal has been withdrawn...

Cross Cutting Concerns Point the way to Protocol Based Implementations

Cross cutting concerns are aspects of an application that directly affect other concerns, examples are

  • logging,
  • monitoring,
  • permission checking,
  • styling,
  • analytics,
  • crash reporting
  • instrumentation
  • transactions

to mention just a few.

Cross cutting concerns span both the internal portions of an application as well as those portions of an application that directly interface with the user such as View Controllers and Views. Cross cutting concerns are often difficult to implement cleanly since they can resist a traditional Object Oriented implementation technique. A bad implementation of a cross cutting concern can often result in code that is scattered around an application and that has tendrils into parts of the system that it should be independent of. The resulting architecture can be tightly coupled and make maintenance and extension of the cross cutting concern code difficult, fragile, and expensive. Traditionally clean implementations of cross cutting concerns made use of Aspect Oriented Programming techniques.

Apple introduced Protocol Oriented Programming some years back, while we all see the value in using such a technique it can be difficult to decide when such an approach is appropriate and will lead to a successful implementation. Identifying a requirement as a cross cutting concern suggests that using a Protocol based approach may be appropriate. Using Protocols can provide a solution to the problem of implementing cross cutting concerns in a new way. Aspect Oriented Programming and Traits Based Programming have specific programming constructs that allow the injection of orthogonal code in specific places in the application and yet isolate that code in it's own implementation unit. While Swift lacks these specific constructs the use of behaviour implemented using Protocol Extensions and attached to a main functional unit either by direct conformance or conditional conformance is a clean and simple way to implement cross cutting concerns. Using this technique the code injected into a particular class, struct or enum is isolated in it's own implementation unit which allows for simplified maintenance and extension.

In this talk I will describe some markers to look for that indicate when a Protocol based approach may be appropriate. I will discuss how to structure a protocol so that it can be used to add behaviour to a target object. I will then describe two implementations of cross cutting concerns using these techniques. The first will be a simple implementation of styling that allows a selected style to be attached to a View with little to no coding in the target view. The second implementation will demonstrate the use of a more complicated scenario where an out band message for confirmation of a restricted transaction may be requested and processed.

About the Author The author has been designing and building Mac apps since System 7 using Metrowerks CodeWarrior, but learned to love Objective C on NeXTStep and OpenStep. When Mac OS X was released they switched their development efforts to that platform using Objective C and Cocoa. With the advent of version 1 of the iPhone SDK, iOS was a natural progression. They were thrilled when Swift was released and have subsequently developed large Swift applications for macOS, iOS and Linux on IBM Cloud.