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

Model 15 (Music)
Model 15 1.0 Device: iOS iPhone Category: Music Price: $29.99, Version: 1.0 (iTunes) Description: The Moog Model 15 App is the first Moog modular synthesizer and synthesis educational tool created exclusively for iPad, iPhone and... | Read more »
How to deal with wind in Angry Birds Act...
Angry Birds Action! is a physics-based puzzler in which you're tasked with dragging and launching birds around an obstacle-littered field to achieve a set objective. It's simple enough at first, but when wind gets introduced things can get pretty... | Read more »
How to get three stars in every level of...
Angry Birds Action! is, essentially, a pinball-style take on the pull-and-fling action of the original games. When you first boot it up, you'll likely be wondering exactly what it is you have to do to get a good score. Well, never fear as 148Apps... | Read more »
The beginner's guide to Warbits
Warbits is a turn-based strategy that's clearly inspired by Nintendo's Advance Wars series. Since turn-based strategy games can be kind of tricky to dive into, see below for a few tips to help you in the beginning. Positioning is crucial [Read... | Read more »
How to upgrade your character in Spellsp...
So you’ve mastered the basics of Spellspire. By which I mean you’ve realised it’s all about spelling things in a spire. What next? Well you’re going to need to figure out how to toughen up your character. It’s all well and good being able to spell... | Read more »
5 mash-ups we'd love to...
If there's one thing that has proved, it's that the addictive gameplay of can be transplanted onto basically anything and it will still be good fun. It wouldn't be surprising if we saw other developers jumping on the bandwagon,... | Read more »
How to navigate the terrain in Sky Charm...
Sky Charms is a whimsical match-'em up adventure that uses creative level design to really ramp up the difficulty. [Read more] | Read more »
Victorious Knight (Games)
Victorious Knight 1.3 Device: iOS Universal Category: Games Price: $1.99, Version: 1.3 (iTunes) Description: New challenges awaits you! Experience fresh RPG experience with a unique combat mechanic, packed with high quality 3D... | Read more »
Agent Gumball - Roguelike Spy Game (Gam...
Agent Gumball - Roguelike Spy Game 1.0 Device: iOS Universal Category: Games Price: $2.99, Version: 1.0 (iTunes) Description: Someone’s been spying on Gumball. What the what?! Two can play at that game! GO UNDERCOVERSneak past enemy... | Read more »
Runaway Toad (Games)
Runaway Toad 1.0 Device: iOS Universal Category: Games Price: $2.99, Version: 1.0 (iTunes) Description: It ain’t easy bein’ green! Tap, hold, and swipe to help Toad hop to safety in this gorgeous new action game from the creators of... | Read more »

Price Scanner via

13-inch 2.5GHz MacBook Pro on sale for $999,...
B&H Photo has the 13″ 2.5GHz MacBook Pro on sale for $999 including free shipping plus NY sales tax only. Their price is $100 off MSRP. Read more
Apple refurbished 2015 iMacs available for up...
Apple now has a full line of 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... Read more
Indian Smartphone Market Grows Annually by 12...
India’s smartphone market grew by 12 percent year-over-year, with 24.4 million units shipping in Q1 2016. The top five vendors stayed the same, with Samsung in the lead, followed by Micromax, Intex... Read more
Get Notifications When Your Friend’s Phone Ba...
Calgary, Canada based Stonelight Pictures has announced the release of Battery Share 1.0.1, its new utility for iOS 9 supported devices. The company notes that people are spending more time on their... Read more
11-inch 1.6GHz/128GB MacBook Air on sale for...
Amazon has the current-generation 11″ 1.6GHz/128GB MacBook Air (sku MJVM2LL/A) on sale for $749.99 for a limited time. Their price is $150 off MSRP, and it’s the lowest price available for this model... Read more
Price drops on clearance 2015 13-inch MacBook...
B&H Photo has dropped prices on clearance 2015 13″ MacBook Airs by up to $250. Shipping is free, and B&H charges NY sales tax only: - 13″ 1.6GHz/4GB/128GB MacBook Air (MJVE2LL/A): $799, $200... Read more
Mac minis on sale for up to $100 off MSRP
B&H Photo has Mac minis on sale for up to $100 off MSRP including free shipping plus NY sales tax only: - 1.4GHz Mac mini: $449 $50 off MSRP - 2.6GHz Mac mini: $649 $50 off MSRP - 2.8GHz Mac mini... Read more
13-inch Retina MacBook Pros on sale for up to...
B&H Photo has 13″ Retina MacBook Pros on sale for $130-$200 off MSRP. Shipping is free, and B&H charges NY tax only: - 13″ 2.7GHz/128GB Retina MacBook Pro: $1169 $130 off MSRP - 13″ 2.7GHz/... Read more
Apple price trackers, updated continuously
Scan our Apple Price Trackers for the latest information on sales, bundles, and availability on systems from Apple’s authorized internet/catalog resellers. We update the trackers continuously: - 15″... Read more
SanDisk Half-Terabyte SSD Optimized for Every...
SanDisk Corporation has announced the SanDisk Z410 SSD, a cost-competitive, half-terabyte solid state drive (SSD) that enables manufacturers to design for a broad range of desktop PCs and laptops.... Read more

Jobs Board

Restaurant Manager (Neighborhood Captain) - A...
…in every aspect of daily operation WHY YOU LL LIKE IT You ll be the Big Apple You ll solve problems You ll get to show your ability to handle the stress and Read more
*Apple* Retail - Multiple Positions (US) - A...
Job Description:SalesSpecialist - Retail Customer Service and SalesTransform Apple Store visitors into loyal Apple customers. When customers enter the store, Read more
Restaurant Manager (Neighborhood Captain) - A...
…in every aspect of daily operation. WHY YOU'LL LIKE IT: You'll be the Big Apple . You'll solve problems. You'll get to show your ability to handle the stress and Read more
*Apple* Subject Matter Expert - NTT Data, In...
…in Owings Mills, MD has a 6+ month contract position available for an Apple Subject Matter Expert. TITLE: Apple Subject Matter Expert LOCATION: Owings Mills, 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
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.