Design Patterns in ActionScript–Adapter

penguin-256x256 Our company is building a new project recently. This project is about simulated flight. The total solution will include the software module and hardware module. Unfortunately, our company just a software company, we don’t have the ability to design the hardware. So, our company wants to buy the hardware solution from other company, we only need to work out the software.

The software part looks like just a piece of cake to us. So, we quickly build a team to handle this task.

After many and many overtime, the team has finished the software module. And all the operation with hardware is reserved as an interface.

At the same time, our boss has met some hardware suppliers and decided to make a deal with one of them.

Sooner after the deal, we receive the hardware driver library. Things not always go that easy. This time, we found out that the hardware driver library is not fit for the interfaces reserved by our software.

This is a big problem. If we can’t fix it, we’ll only have two ways to go, one is rewriting our code to fit for the hardware driver library; the other is buying a new hardware solution. Eh, maybe we have the third choice, that’s we quit our jobs.

We were very depressed by this situation. But life still goes on. We check the driver library carefully, and we found that many functions can provide the function we need, but it has a different name. We can’t change the driver library. Does it mean that we need to change our code?

Obviously, we don’t want to do that, it’s not an easy job, further more, we’re all lazy guys. So, we need a tricky solution.

Genius programmers, what will you do if you’re in this situation?

Finally, a genius guy in our team gives a suggestion. This solution is much like building a bridge from the interfaces reserved in the software module and the functions provided by the driver library.

The solution is that, we build a new class to wrap driver library, and the new class will derived from the interfaces reserved by the software.

The original code is showing below.

public class Rocket{

private var rocketLib:RocketLib;

public function Rocket(){

rocketLib = new RocketLib();

}

public function run():void{
rocketLib.run();
}
}

public class RocketDriver{

public function run():void{

trace("The Rocket Runs!");
}
}

As you see, the Rocket class is in our software module, and the RocketDriver class is in the library they provided. And the solution is as follows.

public class RocketLib{

private var rocketDriver:RocketDriver;

public function RocketLib(){
rocketDriver = new RocketDriver();
}

public function run():void{
rocketDriver.run();
}

}

Now, the software module doesn’t need to be changed. And everything goes well.

The UML diagram is as below.

clip_image002

Do you like this solution? Of course, this is a pattern named adapter pattern. The intent of this pattern is as follows.

Convert the interface of a class into another interface clients expect. Adapter lets classes work together that couldn’t otherwise because of incompatible interfaces.

–By GOF BOOK

Hope you have a sensitive feeling about this pattern now.

Search-256x256 Demo | DownloadDownload Full Project

Enjoy!

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • DZone
  • Reddit
  • Technorati
  • StumbleUpon
  • Twitter
RSS Enjoy this Post? Subscribe to Ntt.cc

RSS Feed   RSS Feed     Email Feed  Email Feed Follow us Follow us
You can leave a response, or trackback from your own site.

2 Responses to “Design Patterns in ActionScript–Adapter”

  1. Mike Britton says:

    I’ve been in the same position. If forewarned of a move to purchase existing solutions, I try to build things that demonstrate how in-house solutions can meet requirements. If an integration task is assigned to me and I know it’s the best option for the short term, I do the integration in a way that makes sense. It may not be Flash. I approach it as a challenge, and learn from it. The worst case scenario has you outside your comfort zone, but it’s still likely you will learn something along the way. I hope all goes well for you on this one!

  2. koo says:

    Hi, project source not found!

    but thanks for all, it really helps me to understand DesignPattern in very simple form :)

    you ROCKS!!!!

Leave a Reply