TweetFollow Us on Twitter

March 94 - Protocols


Mikel Evins

What are protocols? A protocol is a specification for the interface of an object. Each protocol consists of an abstract description of operations that can be performed on a set of data objects. Less technically, a protocol describes the things that a given object can do. It describes the operations that work on an object; it doesn't say anything about how those operations are to be done, just what they are and what their interface is.

Let's consider an example from the Dylan programming language: the iteration protocol. As described in the book Dylan: an object-oriented dynamic language [Apple Computer, 1992], the Dylan programming language includes a set of collection classes that store groups of objects. Examples of collections include arrays, strings, tables, lists, and so on. Each of these classes has a different characteristic way of storing its components, and a correspondingly different performance profile. On the other hand, all of the collection classes have certain things in common. They all store sets of more than one object, for example, and a programmer might reasonably expect to iterate over any collection's contents. Dylan specifies the iteration protocol in order to address this expectation: the interface specified by the iteration protocol can be used to visit each object in a collection for the purpose of performing an operation on it, and the protocol is valid for any collection object.

Dylan's iteration protocol specifies the following interface:

initial-state collection => state
next-state collection state => state
current-element collection state => element
copy-state collection state => state

Names in boldface type are the names of Dylan functions; names in plain type are the names of parameter types; the symbol => denotes a return value.

This protocol is independent of the specific types of the parameters. We don't know, and we don't need to know, the specific type or representation of a collection or its associated state. So long as the protocol's functions are defined to operate consistently and give correct results, the protocol works and the objects in question can be considered collections. In fact, any object whatsoever that participates in the iteration protocol can be thought of as a collection, regardless of whether it shares any superclasses with any other collection objects. This is the insight that motivates our examination of protocols.

In fact, we could define an iteration protocol for any programming language, even assembly language. By defining a protocol we define an abstract data type, even if the programming language that we use doesn't provide any special support for us. Once we have defined a protocol such as the iteration protocol, any data on which we can use it correctly can be considered an instance of the abstract type defined by the protocol.

If we define our abstract types in terms of protocols rather than in terms of their representation then our method of data abstraction is portable across programming languages and representations. We can decide how to organize our data independently of how that organization will be represented, and even use more than one representation within a given implementation. As an example, programmers commonly have reason to represent collections of data values associated with names. Hash tables provide a good solution to the problem of searching for named values in a large collection, but when the collections are small the hashing overhead can be more time-consuming than a simple linear search through the entries in the collection. If a system needed to execute many such searches then the optimal solution might be to store small collections in association lists and large ones in hash tables.

This strategy of maintaining multiple representations might seem inelegant if the programmers using it had to be aware of which representation they were using at any given time, but if both the association lists and the hash tables were treated as instances of a single abstract type accessible through a common protocol then we might implement the optimal solution without imposing any extra burden on programmers who use our data type. Some runtime systems use exactly this strategy to solve similar problems, and the approach works just as well with C, Pascal, and assembly language as with object-oriented languages such as Smalltalk and Dylan.

languages and protocols

The concept of a protocol has different relationships to different programming languages. In Smalltalk protocols are in a sense canonized; the word protocol is used to refer to the set of methods that a class's instances respond to. Unfortunately, this meaning of the word protocol is a little different from what we intend in this article; as we shall see, our intended meaning is a little more general and flexible, and, more importantly, is not defined in terms of the particular classes that implement a data type.

In the Common Lisp Object System the word is used with a meaning closer to what we intend. The CLOS facility for defining new object systems is called the meta-object protocol, and this use of the word protocol is almost exactly what we mean: a set of operations that define the behavior of an abstract type. In the case of the meta-object protocol the abstract type is the meta-class, a class used to define the behavior of other classes. The meta-object protocol is the set of operations used with meta-objects to define how classes will behave. This meaning is still a little different from what we intend here, though, in that it makes reference to a class to which the protocol belongs. As we shall see, our intended meaning of the word protocol does not depend upon the existence of classes.

Protocols have no special standing in most other programming languages, although the network programmer's concept of a protocol is not so very different from ours. For us a protocol is a set of operations that defines an program entity with which other program entities can interact; for a network implementor a protocol is a set of operations that are valid when applied to entities that might be connected to a network. The important commonality between the two concepts is that in each case a collection of specified operations defines a set of objects or entities. It doesn't matter what those objects are composed of, only how they behave.

If we understand this idea of an object whose identity is defined by its interface then we can begin to think of organizing our programs in terms of such entities. The great advantage of this approach is that we don't have to know anything about how we will implement the objects in order to describe a program's architecture and abstract design. We don't need to know what programming language we will use to implement them. If we take a protocol-oriented approach to design then we will reap many of the benefits of object-oriented programming whether or not the programming language we use is an object-oriented one.

As users of object-oriented languages we are often tempted to organize our projects in the terms made easiest by the language we choose. By adopting the protocol-oriented approach we can invert this process and select a language (or languages) that suits our design. We can choose our tools in terms of our requirements rather than adjusting our requirements to suit the tools.

classes and taxons

With our programs organized in terms of protocols we will often want to talk about sets of objects that conform to a particular protocol, and to do so we'll probably want to use a simple term that designates such collections of objects. For many experienced object-oriented programmers the term that leaps to mind is class, but this intuition is mistaken; classes are not abstract data types.

A class is the representation used to describe an abstract type in a given programming language. Although the distinction between an abstract type and the programming construct used to represent it is subtle, it is a real distinction. The data objects used to represent numbers in a C program, for example, are not mathematical numbers, and it's not unusual to find numeric bugs in C programs because a programmer has momentarily forgotten that a C number doesn't behave quite like the mathematical number it represents. (Arithmetic in C, for example, can overflow).

For another thing, classes commonly incorporate implementation details that have nothing to do with the definition of an abstract type. We might, for example, define a class Square to represent certain geometric objects, and we might choose to inherit many of the properties of Square from a previously defined class Rectangle. The fact that the class Square inherits from the class Rectangle is purely an implementation detail that has nothing to do with the definition of the abstract type Square, and users of the two classes don't need to know anything about that detail if the protocols for the two types are suitably designed. If we confuse the class Square with the abstract type Square then we might be tempted to think that we must use inheritance to define Square objects that have the proper relationship to Rectangle objects, but we would be mistaken. The abstract type Square is actually included in the abstract type Rectangle, so that any object that is a Square is also a Rectangle, but this relationship can be accurately modeled whether or not our programming language supports inheritance, so long as we define a protocol that correctly describes the necessary abstractions.

In the example of the collections that map names to data values our association lists and hash tables might be implemented by data structures that have no inheritance relationship to each other, even if we implement them with a class-based language like Smalltalk. So long as they conform to the proper protocol they belong to a common abstract data type defined by that protocol.

Let's use the term taxon (from the science of taxonomy, which classifies living things according to their evolutionary relationships) to refer to a set of objects that all conform to a particular protocol. A protocol definition then also defines a taxon, and objects that conform to the protocol are instances of the taxon. A taxon is like a class in that it refers to a set of objects that are specific instances of a general abstract category. They differ from classes in that they are solely defined by their behavior: the definition of a taxon implies nothing about inheritance relationships, representation, or language facilities. A particular taxon might be populated by instances of a class that corresponds to and implements the taxon, or by instances of several such classes, whether or not they share any superclasses. It might sometimes also be empty, containing no instances, or it might, in a non-class-based system, be populated by data objects with no inheritance relationships to any other data objects. In the example of Squares and Rectangles we have defined two taxons, one called Rectangle and one called Square, where instances of Square are special cases of the taxon Rectangle. The taxon Square includes all the objects that conform to the Square protocol, and the taxon Rectangle includes all the objects that conform to the Rectangle protocol along with all those that conform to the Square protocol.

Again, recall our hash table taxon and our association list taxon; we might implement them in C with no special support for classes or inheritance, and so without any inheritance relationship at all. We can still think of our hash tables and our association lists as instances of a common taxon, however, defined by their common protocol. A taxon can be named and documented and used to organize programs implemented in any programming language, or even in a mix of languages in programs running on assorted machines underassorted runtime systems.

Taxons are the data component of a protocol-based design; they bear the same relation to classes that protocols do to methods or functions. We can diagram the relationships among taxons much in the way that we diagram the relationships among classes, and in general analyze them in analogous ways. Taxons can actually be a little less prone to misunderstanding in that there is less danger of confusing runtime relationships (such as messaging relationships) with compile-time structural relationships (such as inheritance).

advantages of protocol-based designs

So far we have discussed what a protocol-based design is and how taxons differ from classes, but these concepts are only curiosities unless there is some advantage to using them in real projects. The chief advantage of using protocols to organize programs is that we can take advantage of object-oriented concepts of design without depending upon any particular language support in order to implement our designs. We can for example, analyze a programming problem in terms of the abstract types of data that we will process and the abstract operations that we will use to process them without making any reference to how we will represent the data or implement the operations. As a result, we can independently use whatever tools are best suited to the problem on each platform, and we can measure various representation and implementation strategies to find the best performance tradeoffs without affecting the design.

Suppose we want to implement a persistent object database for storing various different elements of documents. We might want to store text, line drawings, digitized photographs, video clips, and soon. We would like to optimize access to the various different types of data, and there are clever tricks for speeding access to certain types of objects. For example, we can speed text searches by choosing a hash function that sets certain bits of a tag on each text object that has certain combinations of letters in it. Unfortunately, this trick doesn't help us much in searching through groups of line drawings, where we might like to use a statistical comparison of curve shapes instead. We would like, therefore, to store different types of objects each in their own pools and optimize methods of search separately for each pool. At the same time, we would like the users view of documents stored in the database to be as seamless as possible, so forcing a user to choose from among several type-specific databases is not an acceptable design choice.

A workable solution might be to design a protocol for database access, specifying generic operations for adding and deleting objects, and for treating objects in a database as candidates for matching a search criterion. We can specify these operations generally enough that they can work with any type of object that we might want to store and treat any specific optimizations for a given data type as implementation details rather than as part of the protocol. We do this by specifying the interface that the operations must present to the programmer, but not specifying how they will be implemented. Similarly, we specify that we must be able to apply the protocols operations to each data type we store, but we don't specify how the data objects will be stored nor specifically how the operations will work on them; we specify only what the operations and their results will be.

Consider the following operations:

assert(database, object)
member?(database,object,criterion) => Boolean

We can specify this interface without establishing any policy about how to represent database, criterion, or Boolean objects. We can choose any representation we like so long as we implement the operations specified by the protocol so that they work with our representations. Object-oriented languages offer us the convenience of polymorphism, with which we can implement the operations with a separate definition for each specific representation. If we use a conventional procedural language then we must explicitly manage the various versions of the operations using conditional execution or dispatch tables that we construct, but we can still organize our program using the protocol concept ,and our implementation will benefit from the modularity that the approach offers us.

Once the database protocol is specified we can then choose an implementation strategy that fits the tradeoffs we want to make, and we can make our decisions without affecting the design of the database protocol. We might implement the various types of objects and the database stores for them very differently; perhaps even to the extent of writing the various implementations in different programming languages. As long as the various implementations conform properly to the protocol that we have defined, the objects that they implement will satisfy our design.

Substitution and re-engineering

Often a programmer will discover part of the way through implementation of a program that the representations or algorithms that he or she has chosen are not optimal for the program. Sometimes the desire to rework a program to improve the algorithms or data representation will go unfulfilled because of the constraints of production schedules. Programmers would more often be able to act on their impulses to improve a program by incorporating what they have learned in the course of implementation if they could replace parts of the program without having a large impact on the overall structure of the program. Protocol-based designs provide one way to make it easier to change subsystems without changing a whole program because they help us to insulate implementation details of a subsystem from the other subsystems that rely on it. Remember that each subsystem can remain independent of the implementation of the others so long as it refers only to their published protocols.

If a program's design is specified solely in terms of the protocols by which each subsystem can be exercised, then substituting a new implementation is straightforward, and the only constraint the programmer must satisfy is to ensure that the new subsystem correctly conforms to the appropriate protocols. In this context protocols can be thought of as contracts that specify what a subsystem will provide. In some types of systems it is even appropriate to provide runtime support that the system can use to query modules to ensure that they conform to the proper protocol before using their facilities.

Using protocols

Protocols can be used in several ways to benefit programmers and, indirectly, users of their software.

A protocol can define a set of operations that clients of a library are expected to implement. Users of application frameworks will find this idea to be a familiar one. Frameworks such as MacApp consist not only of data structures and operations that are provided for client programs, but also specifications for operations that clients are required to implement. One of the problems that programmers have in learning frameworks like MacApp is that they typically lack a way of distinguishing the operations that are required from the operations that are provided. Separating operations into different protocols can provide such a distinction.

Implementors can publish protocols while keeping the details of classes (or other organizational structures) private. Most libraries are composed partly of operations and data structures that the implementors intend their customers to use and partly of other data structures and operations that exist solely to support the public elements. Using protocols, a provider can expose sets of operations without necessarily exposing the data structures associated with them. Taking our name-to-value mappings as an example, a library that provides them does not need to expose the names of the classes that implement them, nor the instance variables of the objects that represent them. It only needs to implement the operations used to create and manipulate them.

Designers can use protocols to organize operations independently of data representations. Most working programmers are probably familiar with the organizational convenience of distinguishing the definition of an interface from the implementation that corresponds to it by using separate header and implementation files. Using protocols extends this convenience by collecting related operations and separating them from unrelated ones. Each set of operations that make up a given protocol can be defined in a separate file, thus arranging together operations that are related and separating them from unrelated ones.

Besides gathering related code into convenient repositories of related code, protocol-oriented organization provides a convenient means of subdividing complex implementations. In some programs class definitions can become quite extensive and its helpful to have a means of breaking them into manageable pieces. Protocols can provide a structure for breaking them up in an understandable way.

Protocols in practice

The use of protocols is a set of conventions rather than a language feature or system facility. Because protocols are merely convenient ways to think of organizing programs, we can use them in any sort of program independent of the features of our development system. On the other hand, some development systems provide specific support for protocols. As a specific example, ParcPlace Systems of Palo Alto, California provides development-environment assistance for protocol-based designs in its ObjectWorks Smalltalk product. Similarly, NeXT Computer of Redwood City, California has extended its version of the Objective-C programming language to provide language-level support for defining protocols and organizing code around them. Such conveniences are attractive features and help to earn Smalltalk and NEXTSTEP their well-deserved reputations for quality and ease-of-use in application development. We can hope that other vendors of development systems will see the advantages of such features and support them in future products.

Regardless of the support that we can expect from compiler vendors, however, protocols and taxons remain a useful organizational tool with which we can improve the quality and maintainability of our applications.


Community Search:
MacTech Search:

Software Updates via MacUpdate

Lyn 1.8.5 - Lightweight image browser an...
Lyn is a fast, lightweight image browser and viewer designed for photographers, graphic artists, and Web designers. Featuring an extremely versatile and aesthetically pleasing interface, it delivers... Read more
Apple iOS 10.2.1 - The latest version of...
iOS 10 is the biggest release of iOS ever. A massive update to Messages brings the power of the App Store to your conversations and makes messaging more personal than ever. Find your route with... Read more
Apple Security Update 2016-003 Supplemen...
Apple Security Update is recommended for all users and improves the security of OS X. For detailed information about the security content of this update, please visit: Read more
Apple macOS Sierra 10.12.3 - The latest...
With Apple macOS Sierra, Siri makes its debut on Mac, with new features designed just for the desktop. Your Mac works with iCloud and your Apple devices in smart new ways, and intelligent... Read more
BetterTouchTool 1.992 - Customize Multi-...
BetterTouchTool adds many new, fully customizable gestures to the Magic Mouse, Multi-Touch MacBook trackpad, and Magic Trackpad. These gestures are customizable: Magic Mouse: Pinch in / out (zoom... Read more
Viber 6.5.5 - Send messages and make cal...
Viber lets you send free messages and make free calls to other Viber users, on any device and network, in any country! Viber syncs your contacts, messages and call history with your mobile device, so... Read more
Opera 42.0.2393.137 - High-performance W...
Opera is a fast and secure browser trusted by millions of users. With the intuitive interface, Speed Dial and visual bookmarks for organizing favorite sites, news feature with fresh, relevant content... Read more
iClock Pro 3.4.7 - Customize your menuba...
iClock Pro is a menu bar replacement clock for Apple's default clock. iClock Pro is an update, total rewrite and improvement to the popular iClock. Have the day, date and time in different fonts and... Read more
PhotoDesk 4.1.5 - Instagram client for p...
PhotoDesk lets you view, like, comment, and download Instagram pictures/videos. (NO Uploads! / Image Posting! Instagram forbids that! AND you need an existing Instagram account). But you can do so... Read more
Capo 3.5.1 - Slow down and learn to play...
Capo lets you slow down your favorite songs so you can hear the notes and learn how they are played. With Capo, you can quickly tab out your songs atop a highly-detailed OpenCL-powered spectrogram... Read more

Clash Royale gets some serious balance u...
| Read more »
Ironhide Game Studio prepares for a busy...
Kingdom Rush breathed fresh life into the tired tower defense genre way back in 2012. The game was a robust challenge that somehow managed to lift you up, rather than leaving you feeling crushed and hopeless. The rich array of unit types and... | Read more »
Collect pets and sling arrows in Arcane...
Mobile gaming is a crowded market, but regular updates are a good way to keep us attention-short players keen. The brand new content in Arcane Online is a prime example. Published by Japanese developer Gala, Arcane Online is a fantasy MMO that... | Read more »
Super Mario Run dashes onto Android in M...
Super Mario Run was one of the biggest mobile launches in 2016 before it was met with a lukewarm response by many. While the game itself plays a treat, it's pretty hard to swallow the steep price for the full game. With that said, Android users... | Read more »
WarFriends Beginner's Guide: How to...
Chillingo's new game, WarFriends, is finally available world wide, and so far it's a refreshing change from common mobile game trends. The game's a mix of tower defense, third person shooter, and collectible card game. There's a lot to unpack here... | Read more »
Super Gridland (Entertainment)
Super Gridland 1.0 Device: iOS Universal Category: Entertainment Price: $1.99, Version: 1.0 (iTunes) Description: Match. Build. Survive. "exquisitely tuned" - Rock Paper Shotgun No in-app purches, and no ads! | Read more »
Red's Kingdom (Games)
Red's Kingdom 1.0 Device: iOS Universal Category: Games Price: $4.99, Version: 1.0 (iTunes) Description: Mad King Mac has kidnapped your father and stolen your golden nut! Solve puzzles and battle goons as you explore and battle your... | Read more »
Turbo League Guide: How to tame the cont...
| Read more »
Fire Emblem: Heroes coming to Google Pla...
Nintendo gave us our first look at Fire Emblem: Heroes, the upcoming mobile Fire Emblem game the company hinted at last year. Revealed at the Fire Emblem Direct event held today, the game will condense the series' tactical RPG combat into bite-... | Read more »
ReSlice (Music)
ReSlice 1.0 Device: iOS Universal Category: Music Price: $9.99, Version: 1.0 (iTunes) Description: Audio Slice Machine Slice your audio samples with ReSlice and create flexible musical atoms which can be triggered by MIDI notes or... | Read more »

Price Scanner via

Deal alert! 13-inch 2.0GHz MacBook Pros for $...
B&H Photo has the new 2016 13″ 2.0GHz non-Touch Bar MacBook Pros in stock today and on sale for $225 off MSRP. Shipping is free, and B&H charges NY sales tax only: - 13″ 2.0GHz MacBook Pro... Read more
Free LibreOffice Portable 5.2.4 Complete Offi... and The Document Foundation have announce the release of LibreOffice Portable 5.2.4. LibreOffice Portable is an Open Source full-featured office suite — including a word processor,... Read more
Apple Planning Three New Tablets For 2017 – D...
Digitimes’ Rebecca Kuo and Joseph Tsai say that unnamed insider sources report Apple having three new tablets in the pipeline for 2017 release: a 9.7-inch model with a friendly price range, a new mid... Read more
Roundup of 15-inch Touch Bar MacBook Pro sale...
B&H Photo has the new 2016 15″ Apple Touch Bar MacBook Pros in stock today and on sale for up to $150 off MSRP. Shipping is free, and B&H charges NY sales tax only: - 15″ 2.7GHz Touch Bar... Read more
Apple refurbished iPad Pros available for up...
Apple has Certified Refurbished 9″ and 12″ Apple iPad Pros available for up to $160 off the cost of new iPads. An Apple one-year warranty is included with each model, and shipping is free: - 32GB 9″... Read more
16GB iPad Air 2, Apple refurbished, available...
Apple has Certified Refurbished 16GB iPad Air 2s available for $319 including free shipping. A standard Apple one-year is included. Their price is $60 off original MSRP for this model. Read more
Apple iMacs on sale for up to $120 off MSRP
B&H Photo has 21″ and 27″ Apple iMacs on sale for up to $120 off MSRP, each including free shipping plus NY sales tax only: - 27″ 3.3GHz iMac 5K: $2199 $100 off MSRP - 27″ 3.2GHz/1TB Fusion iMac... Read more
Apple refurbished Apple TVs available for up...
Apple has Certified Refurbished 32GB and 64GB Apple TVs available for up to $30 off the cost of new models. Apple’s standard one-year warranty is included with each model, and shipping is free: -... Read more
Save up to $350 with Apple Certified Refurbis...
Apple has Certified Refurbished 2015 21″ & 27″ iMacs available for up to $350 off MSRP. Apple’s one-year warranty is standard, and shipping is free. The following models are available: - 21″ 3.... Read more
2015 12-inch Retina MacBooks, Apple refurbish...
Apple has Certified Refurbished 2015 12″ Retina MacBooks available for up to $410 off original MSRP. Apple will include a standard one-year warranty with each MacBook, and shipping is free. The... Read more

Jobs Board

*Apple* & PC Desktop Support Technician...
Apple & PC Desktop Support Technician job in Manhattan, NY Introduction: We have immediate job openings for several Desktop Support Technicians with one of our most Read more
*Apple* & PC Desktop Support Technician...
Apple & PC Desktop Support Technician job in Stamford, CT We have immediate job openings for several Desktop Support Technicians with one of our most well-known Read more
*Apple* Retail - Multiple Positions - Apple,...
Job Description: Sales Specialist - Retail Customer Service and Sales Transform Apple Store visitors into loyal Apple customers. When customers enter the store, Read more
*Apple* Site Security Manager - Apple (Unite...
# Apple Site Security Manager Job Number: 54692472 Culver City, California, United States Posted: Jan. 19, 2017 Weekly Hours: 40.00 **Job Summary** The Apple Read more
*Apple* macOS Systems Integration Administra...
…most exceptional support available in the industry. SCI is seeking an Junior Apple macOS systems integration administrator that will be responsible for providing Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.