The “||” operator and a chain of responsibility
Juten Tach,
lately i came across the || operator again and i read through the language specification. Little quiz: What is the content of the variable result in this line of code?
var result:* = null || "one" || "two";
The answer is: “one”. As for me, i had forgotten, that the || operator actually returns the value, that evaluates to true. If both values evaluate to true it returns the one on the left first.
So i started to think about, how to make use of this and the Chain of Responsibility Pattern came to my mind. You remember this pattern, right? You have a list of classes, that have different capabilities and you have a task. Instead of finding out by yourself, which class to use, you simply pass it to a chain, where these classes are connected. Each class determines for itself, if it is responsible and the first one, that answers with yes, is returned and gets the job.
So here i present a little exemplary implementation of a Chain of Responsibility, that in its core makes use of the || operator. By the way, i am not really conforming to the original pattern, because i actually don’t like it. In the original pattern, the potentially responsible classes link to each other directly, which in my opinion causes some trouble. First, they have to be aware, that they are part of a chain, which i think, they shouldn’t and second, they define the order in the chain themselves, which i think they shouldn’t and third, the requesting class has to know, which one is the first class in the chain, which i think, it shouldn’t.
[Caution]: This post now gets a bit lengthy
To build this Chain Of Responsibility thing, we need four ingredients: a chain, some potentially responsible classes, a request that should be processed and finally a using class, which wants the request to be processed.
We start with the request. For this example, it is really simple:
package responsibility { public class Request { public var stuffToBeProcessed:*; } }
As you can see, there is just a variable, which holds some kind of value, with which we want to work with. I did not give it a type to make it more interesting at runtime. You will see later on.
OK, next we need some classes, which can handle this type of request. Therefore we define an interface, which describes this general ability.
package responsibility { public interface Responsible { /** * Checks, if this class can handle the request. * @return An instance of Responsible or null. */ function checkResponsibility(request:Request):Responsible; /** * Processes the request. */ function doIt():void; } }
Pretty easy, right? The checkResponsibility(request:Request) method will either return itself, if it feels responsible for the request or null. The doIt() method then will actually do, whatever is to be done for fulfilling the request.
Next, we have two concrete classes, which implement this interface, ResponsibleOfStrings and ResponsibleOfNumbers:
package responsibility { public class ResponsibleOfStrings implements Responsible { public function checkResponsibility(request:Request):Responsible { if(request.stuffToBeProcessed is String) { return this; }else return null; } public function doIt():void { trace("ResponsibleOfStrings is doing it!"); } } } package responsibility { public class ResponsibleOfNumbers implements Responsible { public function checkResponsibility(request:Request):Responsible { if(request.stuffToBeProcessed is Number) { return this; }else return null; } public function doIt():void { trace("ResponsibleOfNumbers is doing it!"); } } }
So, you can see, these classes simply check if the request holds a string or a number respectively. If they find what they expect, they return themselves, otherwise they return null.
OK, next we need a chain. Very well, here it is:
package responsibility { public class ChainOfResponsibility { private var listOfResponsibleObjects:Vector. = new Vector.; public function addResponsibleObject(responsibleObject:Responsible):void { listOfResponsibleObjects.push(responsibleObject); } public function determineResponsibleObject(request:Request):Responsible { var result:Responsible = null; for(var i:int = 0; i < listOfResponsibleObjects.length; i++) { result ||= listOfResponsibleObjects[i].checkResponsibility(request); } return result; } } }
So finally here comes the part with the || operator. We run a list of potentially responsible classes. And in the function determineResponsibleObject(request:Request), we now go through this list. And here we make use of the fact, that when we chain the return values of each checkResponsibility(request) call by using the || operator, we get the first one, that returned itself instead of null. Means, we get the first class instance, which feels responsible for doing the job.
OK, lastly of course, we want to see, how to use that stuff. So here it is:
package { import flash.display.Sprite; import responsibility.*; public class Main extends Sprite { public function Main() { var chainOfResponsibility:ChainOfResponsibility = new ChainOfResponsibility; chainOfResponsibility.addResponsibleObject(new ResponsibleOfNumbers); chainOfResponsibility.addResponsibleObject(new ResponsibleOfStrings); var request:Request = new Request; request.stuffToBeProcessed = 10; var responsibleObject:Responsible = chainOfResponsibility.determineResponsibleObject(request); responsibleObject.doIt(); } } }
So, first we are setting up our Chain of Responsibility an give it our two responsible classes. Then we build a new request. In this case, we assign a number to the variable stuffToBeProcessed. Then we hand that request over to our chain and save the result in our variable responsibleObject. Finally we call the doIt() method on that object. And if we run this, we get a trace saying “ResponsibleOfNumbers is doing it!”, as expected. Of course, if we had assigned a string to the variable stuffToBeProcessed, than we would have seen “ResponsibleOfStrings is doing it!”.
Well, that’s it. If you find this useful, you can use it freely for any purpose.

I use the same principle with the && operator when using validators:
var result:Boolean = true; // expected value
for (var i:uint=0; i<validators.length; i++) {
result &&= validators[i].validate();
if (!result) throw new Error("fault");
}