Last Friday, at 8 pm, we are having a discussion about Strategy Pattern at RPL laboratory. I won’t explain you about Strategy Pattern in a serious way. You’ll find more information in those articles:

http://en.wikipedia.org/wiki/Strategy_pattern
http://www.dofactory.com/Patterns/PatternStrategy.aspx
http://www.exciton.cs.rice.edu/JavaResources/DesignPatterns/StrategyPattern.htm

This article is more like a review, what I’ve got from the discussion. Let’s think globally🙂

Mirza, the presenter of the discussion, demonstrated in a very good way. He pictured a class that called BlackBox. The function of the BlackBox was to loop and generate “prima”-sorry, how to say it in English? :p-numbers in random way. He wanted to show the result by making another class called Viewer. Then, there were 2 classes in the main program, they were Black Box and Viewer. To make it work, he had to make a link between them, notify the Viewer so BlackBox could recognize it.

By writing this statement in BlackBox (in java): Viewer.NotifyMe(x);

Aha, no problemo.🙂

THE PROBLEM IS…

The problem appeared when he wanted to make another way to show the result. If Viewer showed the result in console, then Monitor would show it in form. After he made the class of Monitor, he should reinvestigate the BlackBox code. Remember: the link, he had made before, was to recognize Viewer, not Monitor! He should change the code: Viewer -> Monitor for 3 or more times. Wah repot…
Maybe you’ve ever face the same problem while you are in the middle of coding. Find, next, replace, find, next, replace… while there is a way to solve the problem. Okay, how to make the BlackBox dynamic and reuse able?

PROGRAM TO INTERFACE- NOT TO IMPLEMENTATION

Make an interface. This interface has inheritances-for the previous case they were Viewer and Monitor. The BlackBox would call the interface, not the Viewer either the Monitor. You can switch the behavior in the Interface, not in the BlackBox.

DESIGN BY CONTRACT

If Viewer and Monitor-or other classes-want to be the inheritance of the Interface. They have to ‘sign’ a contract first.

For example (in java): … Monitor implement displayable{…

If Interface has a method called “display”, then the Viewer and Monitor have to name their method by “display”.

This called STRATEGY PATTERN. This way allows you to switch your algorithm in a simple way. If you want to have several different behaviors that you want an object to perform, then just encapsulate each of the behavior in different class, not buried in the body.

Good discussion, no one asked any question though… :p Actually, this is the… 4th discussion we had in RPL Laboratory since May 2008. We have this program every Friday, at 17.00-most of the time. We already had:
– Web Engineering by Aruna
– Web 2.0 by Yoyo
– Web 3.0 by Amel
– Game Development by Leo
– Strategy Pattern by Mirza

The next discussion would be:
– Observer Pattern
– Garbage Collector
– Software Product Line
– Microsoft Technology
– E-Learning

Any request? The discussion is not only for the RPL member, everyone welcome.
Sorry if there’s anything incorrect in my explanation about Strategy Pattern. I’m just the beginner. Hehehe…