Chapter Three Part Four
Chapter Three Part Four
Chapter Three
Design Pattern
rule engines.
Real life Example
A language translator who translates a language for us
provides a classic example for this pattern. Or, we can
also consider music notes as our grammar and musicians as
our interpreters.
Computer-World Example
• Java compiler interprets the Java source code into byte code
that is understandable by JVM.
• In C#, the source code is converted to MSIL code that is
interpreted by CLR. Upon execution, this MSIL(intermediate
code) is converted to native code (binary executable code) by
JIT compiler.
Advantage and Applicability
Advantage of Interpreter Pattern
It is easier to change and extend the grammar.
Implementing the grammar is straightforward.
It is used:
When the grammar of the language is not
complicated.
When the efficiency is not a priority.
UML for Interpreter Design Pattern
Sample Code
Step 1: Create a Pattern interface.
Step 2: Create a InfixToPostfixPattern class that will allow
what kind of pattern you want to convert.
Step 3: Create a InterpreterPatternClient class that will
use InfixToPostfix Conversion.
Strategy Patterns (Behavioral)
The Strategy Design Pattern seems to be the simplest of all
design patterns, yet it provides great flexibility to your code.
This pattern is used almost everywhere, even in conjunction
with the other design patterns.
The patterns we have discussed so far have a relation with
this pattern, either directly or indirectly.
A Strategy pattern is a group of algorithms encapsulated
inside classes that are made interchangeable so the
algorithm can vary depending on the class used.
Strategies are useful when you would like to decide at
run time when to implement each algorithm. They can
be used to vary each algorithm’s usage by its context
Real World Example
In a football match, at the last moment, in general, if Team A
is leading Team B by a score of 1-0, instead of attacking, Team
A becomes defensive. On the other hand, Team B goes for an
all-out attack to score.
Computer World Example
The above concept is applicable to a computer football game
also. Or, we can think of two dedicated memories where
upon fulfillment of one memory, we start storing the data in
the second available memory.
So, a runtime check is necessary before the storing of data,
and based on the situation, we’ll proceed.
Strategy Patterns (Behavioral)
Intent
"Define a family of algorithms, encapsulate each one,
and make them interchangeable.
Strategy lets the algorithm vary independently from
clients that use it."
Also Known as
The Strategy Pattern is also known as Policy.
Problem and Solution in Strategy
The Strategy design pattern solves problems like:
How can a class be configured with an algorithm at run-time
instead of implementing an algorithm directly?
How can an algorithm be selected and exchanged at run-time?
The Strategy pattern describes how to solve such problems:
Define a family of algorithms, encapsulate each one, - define
separate classes (Strategy1,Strategy2,…) that implement
(encapsulate) each algorithm, and make them interchangeable -
and define a common interface (Strategy)through which