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

WhiteCap 6.7.1 - Visual plug-in for iTun...
WhiteCap is a sleek and sophisticated music visualizer and screensaver that features futuristic, wireframe mesh visuals with dynamic backgrounds and colors. WhiteCap contains thousands of visual... Read more
DiskMaker X 6.0 rc5 - Make a bootable OS...
DiskMaker X (was Lion DiskMaker) helps you to build a bootable drive from the official OS X installer app (the one you download from the Mac App Store). It detects the OS X Install program with... Read more
Lyn 1.9 - Lightweight image browser and...
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
MacFamilyTree 8.2.1 - Create and explore...
MacFamilyTree gives genealogy a facelift: modern, interactive, convenient and fast. Explore your family tree and your family history in a way generations of chroniclers before you would have loved.... Read more
LaunchBar 6.9 - 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
LaunchBar 6.9 - 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
MacFamilyTree 8.2.1 - Create and explore...
MacFamilyTree gives genealogy a facelift: modern, interactive, convenient and fast. Explore your family tree and your family history in a way generations of chroniclers before you would have loved.... Read more
Spotify - Stream music, crea...
Spotify is a streaming music service that gives you on-demand access to millions of songs. Whether you like driving rock, silky R&B, or grandiose classical music, Spotify's massive catalogue puts... Read more
VOX 2.8.26 - Music player that supports...
VOX just sounds better! 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 audio formats you should ever need.... Read more
beaTunes 5.0.1 - Organize your music col...
beaTunes is a full-featured music player and organizational tool for music collections. How well organized is your music library? Are your artists always spelled the same way? Any R.E.M. vs REM?... Read more

Latest Forum Discussions

See All

This War of Mine gets a new ending and m...
This War of Mine just got a big new update, featuring free DLC that adds a new ending to the game, among other exciting changes. The update is celebrating the game's two-year release anniversary. Apart from the new ending, which will be quite... | Read more »
Summon eight new heroes in Fire Emblem H...
Nintendo keeps coming at us with Fire Emblem Heroes updates, and it doesn't look like that trend is stopping anytime soon. The folks behind the game have just announced the new War of the Clerics Voting Gauntlet, expected to start next Tuesday. [... | Read more »
The best deals on the App Store this wee...
iOS publishers are pulling out all the stops this week -- there's a huge number of seriously great games at discounted prices this week. Let's not waste any time and get right down to business. [Read more] | Read more »
The House of da Vinci (Games)
The House of da Vinci 1.0.0 Device: iOS Universal Category: Games Price: $4.99, Version: 1.0.0 (iTunes) Description: Enter The House of Da Vinci, a new must-try 3D puzzle adventure game. Solve mechanical puzzles, discover hidden... | Read more »
Solve the disappearance of history’s gre...
Blue Brain Games invites you to indulge in an immersive hands-on 3D puzzle adventure in similar vein to The Room series, with its debut release The House of Da Vinci. Set during the historic period of the Italian Renaissance (when Leonardo himself... | Read more »
Age of Rivals (Games)
Age of Rivals 3.3 Device: iOS Universal Category: Games Price: $.99, Version: 3.3 (iTunes) Description: Deep civilization-building strategy in a fast-paced card game! | Read more »
Panthera Frontier (Games)
Panthera Frontier 1.0 Device: iOS Universal Category: Games Price: $4.99, Version: 1.0 (iTunes) Description: | Read more »
Angry Birds Evolution beginner's gu...
Angry Birds changes things up a fair bit in its latest iteration, Angry Birds Evolution. The familiar sling-shot physics mechanics are still there, but the game now features team-based gameplay, RPG elements, and a new top-down view. With all of... | Read more »
Sega Forever is for the retro game fans
Sega is launching a new retro games service titled Sega Forever, in a move that's sure to delight games enthusiasts with a bit of nostalgia. Sega's releasing five classic games for free. The titles include Sonic the Hedgehog, Phantasy Star II,... | Read more »
The Little Acre (Games)
The Little Acre 1.0 Device: iOS Universal Category: Games Price: $2.99, Version: 1.0 (iTunes) Description: | Read more »

Price Scanner via

ABBYY TextGrabber 6 for iOS Implements Instan...
ABBYY has announced the release of TextGrabber 6.0.0, an important feature update to the company’s productivity app developed for iOS and Android devices. TextGrabber 6.0 now offers Real-Time... Read more
vPhone, First Smartphone That Can’t Be Lost,...
Austin, Texas based Hypori has introduced the vPhone, a virtual smartphone that affords every business user the benefits of separate work and personal phones, conveniently delivered on a single... Read more
Save this weekend with 2016 refurbished MacBo...
Apple has dropped prices on Certified Refurbished 2016 15″ and 13″ MacBook Pros by as much as $590 off original MSRP. An Apple one-year warranty is included with each model, and shipping is free: -... Read more
New 27-inch 3.4GHz iMac on sale for $1699, sa...
MacMall has the new 2017 27″ 3.4GHz iMac (MNE92LL/A) in stock and on sale for $1699 including free shipping. Their price is $100 off MSRP. Read more
Clearance 2016 MacBook Pros available for up...
B&H Photo has clearance 2016 13″ and 15″ MacBook Pros in stock today and on sale for up to $400 off original MSRP. Shipping is free, and B&H charges NY & NJ sales tax only: - 15″ 2.7GHz... Read more
Apple Ranks 9th In comScore Top 50 U.S. Digit...
comScore, Inc. has released its monthly ranking of U.S. online activity at the top digital media properties for May 2017 based on data from comScore Media Metrix Multi-Platform. * Entity has... Read more
10.5-inch iPad Pros available for up to $20 o...
B&H Photo has the new 2017 10.5″ iPad Pros available for up to $20 off MSRP including free shipping plus NY & NJ sales tax only: - 64GB iPad Pro WiFi: $649 - 256GB iPad Pro WiFi: $749 - 512GB... Read more
Three Off-The-Beaten-Track iOS Apps That Dese...
One of the great things about using iPads and iPhones is the vast selection of apps available for most anything you want or need to do. The three outlined in this article have been in my core app... Read more
Apple No. 1 Spot In Gartner Top 100 Vendors i...
Gartner, Inc. has unveiled the top global 100 vendors in IT in 2016 based on their revenue across IT (excluding communication services) and component market segments. In the Gartner Global Top 100:... Read more
Clearance iMacs available for up to $300 off...
B&H Photo has clearance 21″ and 27″ Apple iMacs available starting at $949, each including free shipping plus NY & NJ sales tax only: - 27″ 3.3GHz iMac 5K: $1999 $300 off original MSRP - 27″... Read more

Jobs Board

*Apple* News Product Marketing Mgr., Publish...
…organizational consensus on strategy and vision for publisher tools, authoring, and Apple News Format.Carries this strategy and vision across the organization to 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
Security Data Analyst - *Apple* Information...
…data sources need to be collected to allow Information Security to better protect Apple employees and customers from a wide range of threats.Act as the subject matter Read more
Lead *Apple* Solutions Consultant - Apple I...
…integrity, and trust.Success Metrics/Key Performance Indicators:Quantitative* Year over Year growth in Apple Product and Beyond the Box sales in the assigned Point of Read more
*Apple* Solutions Consultant till v%u00E5r...
…ethics, integrity, and trust.Success Metrics/Key Performance Indicators:QuantitativeYear over Year growth in Apple Product and Beyond the Box sales in the assigned Point Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.