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.5.11 - Lightweight image browser a...
Lyn is a lightweight and fast image browser and viewer designed for photographers, graphic artists and Web designers. Featuring an extremely versatile and aesthetically pleasing interface, it... Read more
NeoOffice 2014.11 - Mac-tailored, OpenOf...
NeoOffice is a complete office suite for OS X. With NeoOffice, users can view, edit, and save OpenOffice documents, PDF files, and most Microsoft Word, Excel, and PowerPoint documents. NeoOffice 3.x... Read more
LaunchBar 6.4 - Powerful file/URL/email...
LaunchBar is an award-winning productivity utility that offers an amazingly intuitive and efficient way to search and access any kind of information stored on your computer or on the Web. It provides... Read more
Remotix 3.1.4 - Access all your computer...
Remotix is a fast and powerful application to easily access multiple Macs (and PCs) from your own Mac. Features Complete Apple Screen Sharing support - including Mac OS X login, clipboard... Read more
DesktopLyrics 2.6.6 - Displays current i...
DesktopLyrics is an application that displays the lyrics of the song currently playing in "iTunes" right on your desktop. The lyrics for the song have to be set in iTunes; DesktopLyrics does nothing... Read more
VOX 2.5.1 - Music player that supports m...
VOX is a beautiful music player that supports many filetypes. The beauty is in its simplicity, yet behind the minimal exterior lies a powerful music player with a ton of features and support for all... Read more
NetNewsWire 4.0.0 - RSS and Atom news re...
NetNewsWire is the best way to keep up with the sites and authors you read most regularly. Let NetNewsWire pull down the latest articles, and read them in a distraction-free and Mac-like way. Native... Read more
MacUpdate Desktop 6.0.6 - Search and ins...
MacUpdate Desktop 6 brings seamless 1-click installs and version updates to your Mac. With a free MacUpdate account and MacUpdate Desktop 6, Mac users can now install almost any Mac app on macupdate.... Read more
ForkLift 2.6.5 - Powerful file manager:...
ForkLift is a powerful file manager and ferociously fast FTP client clothed in a clean and versatile UI that offers the combination of absolute simplicity and raw power expected from a well-executed... Read more
Drive Genius 4.1.0 - Powerful system uti...
Drive Genius 4 gives you faster performance from your Mac while also protecting it. The award-winning and improved DrivePulse feature alerts you to hard drive issues before they become major problems... Read more

Biz Builder Delux (Games)
Biz Builder Delux 1.0.0 Device: iOS Universal Category: Games Price: $4.99, Version: 1.0.0 (iTunes) Description: Ah, there's nothing like the rhythmic bustle of a burgeoning business burg... especially when you're the one building it... | Read more »
Auroch Digital is Bringing Back Games Wo...
| Read more »
Carbo - Handwriting in the Digital Age...
Carbo - Handwriting in the Digital Age 1.0 Device: iOS Universal Category: Productivity Price: $3.99, Version: 1.0 (iTunes) Description: | Read more »
Draggy Dead (Games)
Draggy Dead 1.1 Device: iOS Universal Category: Games Price: $.99, Version: 1.1 (iTunes) Description: Ditch your dead end job and take up a rewarding career in Grave Robbing today!Guide the recently deceased to a fun filled life of... | Read more »
Bad Dinos (Games)
Bad Dinos 1.0.0 Device: iOS Universal Category: Games Price: $2.99, Version: 1.0.0 (iTunes) Description: | Read more »
The Apple Watch isn't Great as a Fi...
| Read more »
Show the World What You See With
Live broadcasting is getting popular on mobile devices, which is why you can now get, by Infinite Takes. [Read more] | Read more »
PhotoTime's 2.1 Update Adds Apple W...
The latest PhotoTime update is adding even more functionality to the handy photo organizing app. Yep, including Apple Watch support. [Read more] | Read more »
Oh My Glob! Adventure Time Puzzle Quest...
Finn and Jake are taking over D3 Go!'s popular puzzle game series in the upcoming Adventure Time Puzzle Quest. [Read more] | Read more »
Earthcore: Shattered Elements - Tips, Tr...
At first glance, Earthcore: Shattered Elements seems like a rather simple card-battling game. Once you’re introduced to skills that will change quite a bit. Even more so once you start to acquire hero cards. But it’s not so complicated that we... | Read more »

Price Scanner via

OtterBox Maximizes Portability, Productivity...
From the kitchen recipe book to the boarsroom presentation, the OtterBox Agility Tablet System turns tablets into one of the most versatile pieces of handheld technology available. Available now, the... Read more
Launch of New Car App Gallery and Open Develo...
Automatic, a company on a mission to bring the power of the Internet into every car, has announced the launch of the Automatic App Gallery, an app store for nearly every car or truck on the road... Read more
Memorial Day Weekend Sale: 13-inch 1.6GHz Mac...
Best Buy has the new 13″ 1.6GHz/128GB MacBook Air on sale for $849 on their online store this weekend. Choose free shipping or free local store pickup (if available). Sale price for online orders... Read more
Memorial Day Weekend Sale: 27-inch 3.5GHz 5K...
Best Buy has the 27″ 3.5GHz 5K iMac on sale for $2099.99 this weekend. Choose free shipping or free local store pickup (if available). Sale price for online orders only, in-store prices may vary.... Read more
Sale! 16GB iPad mini 3 for $349, save $50
B&H Photo has the 16GB iPad mini 3 WiFi on sale for $349 including free shipping plus NY sales tax only. Their price is $50 off MSRP, and it’s the lowest price available for this model. Read more
Price drop on 2014 15-inch Retina MacBook Pro...
B&H Photo has dropped prices on 2014 15″ Retina MacBook Pros by $200. Shipping is free, and B&H charges NY sales tax only: - 15″ 2.2GHz Retina MacBook Pro: $1799.99 save $200 - 15″ 2.5GHz... Read more
With a Mission to Make Mobile Free, Scratch W...
Scratch Wireless, claiming to be the world’s first truly free mobile service, has announced the availability of a new Scratch-enabled Android smartphone, the Coolpad Arise. The smartphone is equipped... Read more
First-Ever Titanium Alloy Curved iPhone 6 Scr...
One of the most common problems with mobile phones is damage to the screens. The slightest drop can cause a dreaded spider web of gashes and cracks in the glass panel surface that can cost $hundreds... Read more
Preorder new 12-inch MacBook, $10 off, save o...
Adorama has new 12″ Retina MacBooks available for preorder for $10 off MSRP including free shipping plus NY & NJ sales tax only. For a limited time, Adorama will include a free Apple USB-C to USB... Read more
Will iOS 9 Finally Bring Productivity Friendl...
Ah, the irony. From its original announcement in 2010, Apple has doggedly insisted that the iPad remain “simple,” thus arbitrarily limiting its considerable potential as a content creation and... Read more

Jobs Board

*Apple* Solutions Consultant - Retail Sales...
**Job Summary** As an Apple Solutions Consultant (ASC) you are the link between our customers and our products. Your role is to drive the Apple business in a retail Read more
Business Development Manager - *Apple* Pay...
**Job Summary** Apple Pay is seeking an experienced Business Development professional to join the Apple Pay team to develop partnerships and strategic alliances with Read more
Project Manager, *Apple* Retail New Store O...
**Job Summary** An Apple Retail New Store Openings & Remodels Project Manager is responsible for successfully managing the openings, remodels, and small works of Read more
SW QA Engineer - *Apple* TV - Apple (United...
**Job Summary** The Apple TV team is looking for experienced Quality Assurance Engineers with a passion for delivering first in class home entertainment solutions. **Key Read more
Partner Marketing Manager - *Apple* Pay - A...
**Job Summary** The Apple Pay partner marketing team is looking for a marketing manager to develop and drive US marketing programs with our financial institution Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.