TweetFollow Us on Twitter

March 94 - Protocols

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)
delete(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

Latest Forum Discussions

See All

Fallout Shelter pulls in ten times its u...
When the Fallout TV series was announced I, like I assume many others, assumed it was going to be an utter pile of garbage. Well, as we now know that couldn't be further from the truth. It was a smash hit, and this success has of course given the... | Read more »
Recruit two powerful-sounding students t...
I am a fan of anime, and I hear about a lot that comes through, but one that escaped my attention until now is A Certain Scientific Railgun T, and that name is very enticing. If it's new to you too, then players of Blue Archive can get a hands-on... | Read more »
Top Hat Studios unveils a new gameplay t...
There are a lot of big games coming that you might be excited about, but one of those I am most interested in is Athenian Rhapsody because it looks delightfully silly. The developers behind this project, the rather fancy-sounding Top Hat Studios,... | Read more »
Bound through time on the hunt for sneak...
Have you ever sat down and wondered what would happen if Dr Who and Sherlock Holmes went on an adventure? Well, besides probably being the best mash-up of English fiction, you'd get the Hidden Through Time series, and now Rogueside has announced... | Read more »
The secrets of Penacony might soon come...
Version 2.2 of Honkai: Star Rail is on the horizon and brings the culmination of the Penacony adventure after quite the escalation in the latest story quests. To help you through this new expansion is the introduction of two powerful new... | Read more »
The Legend of Heroes: Trails of Cold Ste...
I adore game series that have connecting lore and stories, which of course means the Legend of Heroes is very dear to me, Trails lore has been building for two decades. Excitedly, the next stage is upon us as Userjoy has announced the upcoming... | Read more »
Go from lowly lizard to wicked Wyvern in...
Do you like questing, and do you like dragons? If not then boy is this not the announcement for you, as Loongcheer Game has unveiled Quest Dragon: Idle Mobile Game. Yes, it is amazing Square Enix hasn’t sued them for copyright infringement, but... | Read more »
Aether Gazer unveils Chapter 16 of its m...
After a bit of maintenance, Aether Gazer has released Chapter 16 of its main storyline, titled Night Parade of the Beasts. This big update brings a new character, a special outfit, some special limited-time events, and, of course, an engaging... | Read more »
Challenge those pesky wyverns to a dance...
After recently having you do battle against your foes by wildly flailing Hello Kitty and friends at them, GungHo Online has whipped out another surprising collaboration for Puzzle & Dragons. It is now time to beat your opponents by cha-cha... | Read more »
Pack a magnifying glass and practice you...
Somehow it has already been a year since Torchlight: Infinite launched, and XD Games is celebrating by blending in what sounds like a truly fantastic new update. Fans of Cthulhu rejoice, as Whispering Mist brings some horror elements, and tests... | Read more »

Price Scanner via MacPrices.net

Apple’s 24-inch M3 iMacs are on sale for $150...
Amazon is offering a $150 discount on Apple’s new M3-powered 24″ iMacs. Prices start at $1149 for models with 8GB of RAM and 256GB of storage: – 24″ M3 iMac/8-core GPU/8GB/256GB: $1149.99, $150 off... Read more
Verizon has Apple AirPods on sale this weeken...
Verizon has Apple AirPods on sale for up to 31% off MSRP on their online store this weekend. Their prices are the lowest price available for AirPods from any Apple retailer. Verizon service is not... Read more
Apple has 15-inch M2 MacBook Airs available s...
Apple has clearance, Certified Refurbished, 15″ M2 MacBook Airs available starting at $1019 and ranging up to $300 off original MSRP. These are the cheapest 15″ MacBook Airs for sale today at Apple.... Read more
May 2024 Apple Education discounts on MacBook...
If you’re a student, teacher, or staff member at any educational institution, you can use your .edu email address when ordering at Apple Education to take up to $300 off the purchase of a new MacBook... Read more
Clearance 16-inch M2 Pro MacBook Pros in stoc...
Apple has clearance 16″ M2 Pro MacBook Pros available in their Certified Refurbished store starting at $2049 and ranging up to $450 off original MSRP. Each model features a new outer case, shipping... Read more
Save $300 at Apple on 14-inch M3 MacBook Pros...
Apple has 14″ M3 MacBook Pros with 16GB of RAM, Certified Refurbished, available for $270-$300 off MSRP. Each model features a new outer case, shipping is free, and an Apple 1-year warranty is... Read more
Apple continues to offer 14-inch M3 MacBook P...
Apple has 14″ M3 MacBook Pros, Certified Refurbished, available starting at only $1359 and ranging up to $270 off MSRP. Each model features a new outer case, shipping is free, and an Apple 1-year... Read more
Apple AirPods Pro with USB-C return to all-ti...
Amazon has Apple’s AirPods Pro with USB-C in stock and on sale for $179.99 including free shipping. Their price is $70 (28%) off MSRP, and it’s currently the lowest price available for new AirPods... Read more
Apple Magic Keyboards for iPads are on sale f...
Amazon has Apple Magic Keyboards for iPads on sale today for up to $70 off MSRP, shipping included: – Magic Keyboard for 10th-generation Apple iPad: $199, save $50 – Magic Keyboard for 11″ iPad Pro/... Read more
Apple’s 13-inch M2 MacBook Airs return to rec...
Apple retailers have 13″ MacBook Airs with M2 CPUs in stock and on sale this weekend starting at only $849 in Space Gray, Silver, Starlight, and Midnight colors. These are the lowest prices currently... Read more

Jobs Board

Liquor Stock Clerk - S. *Apple* St. - Idaho...
Liquor Stock Clerk - S. Apple St. Boise Posting Begin Date: 2023/10/10 Posting End Date: 2024/10/14 Category: Retail Sub Category: Customer Service Work Type: Part Read more
*Apple* App Developer - Datrose (United Stat...
…year experiencein programming and have computer knowledge with SWIFT. Job Responsibilites: Apple App Developer is expected to support essential tasks for the RxASL Read more
Omnichannel Associate - *Apple* Blossom Mal...
Omnichannel Associate - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Read more
Operations Associate - *Apple* Blossom Mall...
Operations Associate - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Read more
Cashier - *Apple* Blossom Mall - JCPenney (...
Cashier - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Blossom Mall Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.