Making Changes to the Original Protection Proxy
FCNY meets in the basement room at Think Coffee that reminded me of a (smoke free) anarchist’s safe house. (I was expecting to see Sacco and Vanzetti there.) They helped me hook up my PowerBook to a projector, and off we went. Having found myself in a perfect storm of doing the author edits of a Flash Catalyst book and starting on another new book, I was pressed for time; so I took the protection proxy from this blog and changed the proxy participant to include an algorithm that would filter out different “levels” of access. The level of sophistication of the algorithm and the added classes for different levels of access were quite simple. However, the point is to focus on the design pattern and not my acumen at writing algorithms. (If you don’t like the algorithms, you can always re-write them and send them in.) You can download the package by clicking the download button—the .zip file includes the PPT/KeyNote pages as well.

What was most exciting for me was the fact that the Protection Proxy was so easy to change. Here was a program with several different classes, and I had to add new functionality in a hurry that did several different things that would not crash my program. Essentially, I was adding three new concrete RealSubject classes that would work with the current program and make changes in the Proxy (SubProxy) to filter through three different “level of access” options. I knew that if I followed the interface rules, no matter what algorithm I used, it’d work. Figure 1 shows the class diagram of the revised Protection Proxy (from the FCNY presentation.)
The three RealSubject participants (International, National, Local) are pretty much the same except that each loads a different text file. However, as long as each maintains the interface, no matter how different the algorithms are, everything happily hums along.
Let the Proxy Do the Work
A Proxy design pattern has many different functions, but one of the most important is to handle all of the filtering work from the requested object. The hypothetical situation in the example is one where different people have different levels of access to data. In order to determine the level of access, the Proxy (SubProxy) object does all of the deciding work instead of passing it to the individual objects that contain the actual operations requested by the Client. In this way, you keep out duplicate code (filtering work) and you protect the RealSubject participants from unwarranted access. As you can see in the following Proxy class, it both protects and filters in a single operation:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | package { import fl.controls.TextArea; public class SubProxy implements ISubject { //Declare as interface data type private var realSub:ISubject; private var txtArea:TextArea; private var un:String; private var pw:String; public function pxRequest(tx:TextArea,un:String,pw:String):void { this.un=un; this.pw=pw; txtArea=tx; //Access to international data if (this.un=="Lisa" && this.pw=="fms4") { realSub=new International(); realSub.pxRequest(txtArea,null,null); } //Access to national data else if (this.un=="Tyler" && this.pw=="dp") { realSub=new National(); realSub.pxRequest(txtArea,null,null); } //Access to local data else if (this.un=="Will" && this.pw=="flex") { realSub=new Local(); realSub.pxRequest(txtArea,null,null); } else { txtArea.text="Adios..."; } } } } |
Also notice that the realSub object is typed as the ISubject interface. As a result, the conditional options can use the same object name to instantiate the requested object while simultaneously filtering access. Also, the exact same request is forwarded using the identical call because all of the RealSubject participants implement the same interface (as does the Proxy).
Real World Protection Proxy
When I started preparing materials for the FCNY presentation, I was planning on setting one up with different access to a MySQL database using PHP. However, I got jammed up with editorial and writing tasks that I could not put off. Nevertheless, if you have the time (and inclination) you can make some modifications to this example and change each of the RealSubject classes to different database access such as, read data, add data, change data and even delete data. Anyone working on a dating site isn’t going to let all of its members have unlimited access; instead you have access depending on your role. Likewise, the Protection Proxy can protect and filter access to objects of virtually any kind. So the next time you have to deal with this kind of issue, consider using the Proxy.
I’d be remiss if I didn’t thank Will Saunders (my long lost son) for setting up the visit to NYC, and I do. It was fun meeting all of the FCNY members, and I encourage any of you Flash developers who live in the NYC area to pay them a visit. If you enjoy their meeting half as much as I did, you’ll have a great time.

The ActionScript 3.0 Protection Proxy Design Pattern 2: The FCNY Meeting by William B. Sanders, unless otherwise expressly stated, is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.



Bill Sanders
Hi there guys!
This is a very nice article for every developer but specially for those who care about design patterns!
http://www.theserverside.com/tip/Evil-Design-Patterns-When-Design-Patterns-Become-the-Problem
Just to make my self clear: I just want to create a concise and civilized debate.
Cheers!
Hi Bill,
I read the article, and there’s nothing in it—I mean the “Evil Patterns” article. It’s mercifully short (1 Sentence and 4 Paragraphs), but it has nothing of substance.
His thesis statement—-a one-liner in the middle of his little post—-is that the simplest thing that could possibly work is the way to program. Oh, boy! Gimme those old sequential programs. They work; they’re simple—and to quote Wheaton, puh-leeze don’t let us get suckered in by some putz using that new-fangled procedural programming.
Where Wheaton jumps the rails is his shallow understanding (to go with his shallow post) of the purpose of DPs. He doesn’t seem to understand that the purpose of DPs is to make updates easy. As I tried to illustrate with the Protected Proxy, they’re very easy to update—in the middle of a multiple class application.
So my question to Wheaton is–Who’s the putz now?
Cheers,
Bill
PS: By the way Bill, thanks for bringing this to our blog. There’s nothing wrong with controversy. Bring more if you like.
When you write a 1,000 liner code, for yourself… procedural programming is easy and sometimes the right way to go.
No one will have to update it, no one will have to understand what you did there, and everybody’s happy.
But when you write 1,000,000 lines or even more… with 4-5 developers, you MUST use DP, or you’ll be lost!
I’m a developer for ~15 years or so, and I wrote small to huge projects, updating in small projects is fast with procedural programming, and slow with DP… but huge projects are almost impossible to update with procedural, and very easy in DP.
I say, pick you choice
Gil.
Hi Gil,
When I started programming I ended up focusing on drilling down. Actually, they started us off with binary and octal math, and once we got that, they showed us how to program in Fortran, which I thought was a miracle after running after 1s and 0s in binary code. After Fortran, I took up Basic and loved it. However, I discovered poke and peek statements would allow me to dive into machine coding which was much faster. Somewhere in there I discovered FORTH and the engineers at General Dynamics were über brilliant and could whip up a FORTH program as quickly as I could one in Basic. However, they were working with a language centered on the stack, and I later learned that FORTH was really an assembly level language; but I hung out with them for about 10 years and got the idea that the way to program was right down to the metal. They thought that C (and later C++) was a fraud because it claimed to be a program-once language that would run on different platforms. To this they pointed out that C never took advantage of the different processors and programmers were too reliant on higher level structures and every C program had to be tweaked for the set of chips it ran on. (They loved to point out a case where a program on the Space Shuttle in C failed and they were able to set things right using FORTH.) I also took up assembly language programming on 6502 and 6510 processors and eventually programed on a 65816 that was half 8-bit and half-16-bit.
During this time, the FORTH guys were claiming that they were creating objects and re-usable objects just like the OOP structures in C; and besides, C had to be re-tweaked anyway; so FORTH was a big time saver. (We also had some LISP guys who were doing AI who came to the FORTH meetings who liked the idea of getting up close and personal with the machine.) Programming in FORTH involved postfix notations like this:
8 7 + .
That’s the same as 6 + 7 but it’s working on the stack in the same way your computer processes information. As a result, it’s very fast. To create objects (not unlike methods and properties in ActionScript 3.0) we’d use a colon and then a label followed by an operation. For example,
: TAKE-FIVE 5 -;
is an operation that subtracts 5 from a value on the stack. So the operation,
11 TAKE-FIVE .
returns 6. Each “.” (dot) removes the value from the top of the stack. (By the way PostScript, the language that runs PostScript printers uses the same kind of postfix notation.)
If you spend enough time with FORTH, or assembly language, you can get pretty good at creating programs that are fast, efficient and will run on any platform. The shorter the program and the easier the language, the better. FORTH and assembly language require more steps than higher level languages like C++, Java and ActionScript 3.0.
OOP goes in the other direction. Instead of programming to the metal, the programming is to larger structures defined in classes. The classes are the objects, and if you’re going to program with objects (with OOP), you have to think about how the objects relate to one another. This quote sums it up:
As far as possible, I stay away from procedural programming. It’s not that anything is wrong with it, but rather I want to think in OOP terms just so that I can do it. That is, if I start doing a lot of procedural programming for little projects, I quickly loose my OOP edge. This is not to say that I’m successful in doing so; but rather I am aware of what programming habits I want to develop. By sticking with OOP I can whip up a good OOP program as fast as a procedural program. However, the trick is to stick with it, even for little bitty programs.
By the same token, I probably overuse design patterns. So what? If I can do it without wasting a lot of time, even for a little project, I’d rather do it with design patterns than not. Ironically, if I do enough design patterns, I’ll just keep reusing them until I once again lose an OOP edge! (You just can’t win…)
As always Gil, we love to hear from you.
Kindest regards,
Bill
Hi Bill,
I think that you can guess my answer…
I agree that OOP is good, no doubt!
But for short projects with short life-time, you don’t have to think in OOP mode…
When someone is doing procedural programming, he’s thinking less about the future, it’s like he’s saying “We’ll deal with it once we’ll get to the bridge”
When you do OOP you have to think about many possible changes, and think about the future. Get ready for any kind of a bridge. Make it generic.
But sometimes… you are ON the bridge. and OOP is not really useful, and not as fast as procedural (by fast I mean fast programming, not performance)
Don’t get me wrong, OOP and DP are good way of thinking, but they are useful only when:
1. you know how to do it.
2. you don’t know the future, and want to get ready for anything.
3. you have time. (It is faster to write 2-3 procedures, instead of 10 classes because I’m doing MVC…)
If one it not true, go procedural. (As much as I don’t like it)
Gil
Hi Gil,
I understand perfectly—and believe me, you’re not alone. All I’m trying to do is to get to the point where I can do OOP as quickly as I can do procedural. The only way I know how is to do it all the time—just like those FORTH engineers who programmed to the stack when everyone else took a different path—namely, C++.
If I were smarter, I wouldn’t have to practice OOP and DPs so much; but I’m not. So I just keep on practicing my ‘Hello World’ OOP and design patterns.
Cheers,
Bill
Hi Bill
Purchased your book a couple years back and reference it on a regular basis. Have you by any chance read the Addison Wesley Cocoa Design Patterns book? Finding it immensely well written as well.
Hi Jim,
Thank you for bringing Cocoa Design Patterns to my attention. Both Chandima and I use Macs and iPhones; so we’ll both want to look at this book. However, it’s written for Objective C, which is on my to-do list, but a bit far down the list. Right now, I’m up to my ears in an HTML5 project, which involves some PHP and JavaScript, and I’m working in ActionScript 3.0 into this project but only tangentially. Next, I want to start writing some Android apps in AS3 using DPs and probably flooding this blog with them. I’m still a little sore at Apple for dropping Flash/Flex from iPhone development—what would be über cool would be Flash Media Server apps for mobile devices, and that leaves out AS3 for iPhone….sooo, off to Android land.
From reading the Amazon reviews of Cocoa Design Patterns, it’s definitely one that I’ll have to read. If you’d like to write a review of it and possible relationships to AS3, we’d be glad to post it on this blog.
Kindest regards,
Bill