Skip to Content.
Sympa Menu

gang-of-4-patterns - RE: [gang-of-4-patterns] strategy, state , template ????

gang-of-4-patterns AT lists.cs.illinois.edu

Subject: Design Patterns discussion

List archive

RE: [gang-of-4-patterns] strategy, state , template ????


Chronological Thread 
  • From: Siva Sankara Varma Datla <sdatla AT quark.co.in>
  • To: Bharathi Thiyagarajan <bharathi.thiyagarajan AT adcc.alcatel.be>, Prabodh Saha <prabodhs AT ivycomptech.com>
  • Cc: gag-of-4-patterns <gang-of-4-patterns AT cs.uiuc.edu>
  • Subject: RE: [gang-of-4-patterns] strategy, state , template ????
  • Date: Sat, 20 Dec 2003 14:35:45 +0530
  • List-archive: <http://mail.cs.uiuc.edu/pipermail/gang-of-4-patterns/>
  • List-id: Design Patterns discussion <gang-of-4-patterns.cs.uiuc.edu>

Hi Bharathi,

After seeing your example, I am sure it is not fall under Template pattern.
According to Template Pattern, the base class would be deciding which set of
functions to call and in which order. In your example it is not the base
class which takes the decision of calling functions.

The differences between State and Strategy are...
1) In Strategy, the user generally chooses which of several strategies to
apply and that only one strategy at a time is likely to be instantiated and
active within the Context class.
By contrast, in State pattern it is likely that all of the different States
will be active at once and switching may occur frequently between them.

2) Strategy encapsulates several algorithms that do more or less the same
thing.
State encapsulates related classes that each does something somewhat
different.

3) The concept of transition between different states is completely missing
in the Strategy pattern.

Now we will come to your example...
1) According to the first difference your example falls under Strategy as
any object of AParserType, BParserType, CParserType will be created
according to the context(TraceType). And clearly it is not State Pattern as
the objects of all these types exists at any particular time.
2) All your algorithms do the same thing i.e. parsing of a file. So this is
again property of Strategy Pattern according to the second difference stated
above.
3) Again 3rd difference also tells that it is Strategy pattern.


Hope this will help you.

Regards
Siva
-----Original Message-----
From: Bharathi Thiyagarajan
[mailto:bharathi.thiyagarajan AT adcc.alcatel.be]

Sent: Friday, December 19, 2003 2:37 PM
To: Prabodh Saha
Cc: gag-of-4-patterns
Subject: Re: [gang-of-4-patterns] strategy, state , template ????

Thanks for you suggestion

It would be really nice if we could discuss for this scenario, the design
pattern
cannot be
state for the following points and it cannot be a template for the following
points.
so that we can
come to a conclusion. we can also discuss for this scenario, in such a
situation it
can be designed
as a template method or state. Please give your valuable suggestions.


The class diagram that I drew was not aligned properly

Here is the code skeleton:

class TraceParser
{ public:
virtual void parserTrace();
}

class AParserType : public TraceParser
{ public:
parserTrace() { cout << "A Parser Type"; }
}

class BParserType : public TraceParser
{ public:
parserTrace() { cout << "B Parser Type"; }
}

class CParserType : public TraceParser
{ public:
parserTrace() { cout << "C Parser Type"; }
}

class Trace
{ public:
Trace(TraceType)
{ if TraceType == A /* TraceType is an enum. */
TraceParserObject = new AParserType()
else if TraceType == B
TraceParser Object = new BParserType()
else if TraceType == C
TraceParserObject = new CParserType()
else
...return error
TraceParserObject->parseTrace();
}

private :
TraceParser *TraceParserObject;
}

In my Main Object:
Trace *TraceFile = new Trace(checkTraceType());


It would be really nice if we could discuss for this scenario, the design
pattern
cannot be
state for the following points and it cannot be a template for the following
points.
so that we can
come to a conclusion. we can also discuss for this scenario, in such a
situation it
can be designed
as a template method or state. Please give your valuable suggestions.

Thanks and Regards
Bharathi




Prabodh Saha wrote:

> Bharathi,
> I think u should use the strategy pattern here,
> 'cause the requirement specs of your problem warrants so.
>
> Define an abstract class Trace
> (with all the functions common to A,B,C parserType) and just
> the declaration of functions that will have different
> implementations for A,B and C.
>
> Now you have a ParserObject (that holds a reference to Trace)
> and has a public method parserObject.setParser(Trace trace).
> Make this ParserObject a memeber of your Main object.
>
> Your Main object will decide the file type to be parsed.
> Say it decides it is of type A.
> Then call: parserObject.setParser(new AparserType);
> Then call parserObject.method1()
> .method2()
> etc.
> from your main object.
> [NOTE: these method1(), method2() etc. of parserObject will
internally call
> the public methods you declared in your abstract class "Trace"
and defined
> (some of them) differently in different types of Parsers.]
>
> Once done with the 1st file, your Main object can pickup another
file.
> Decide its type (say TypeB now) and configure its parserObject
with
> "new BparserType()" and the rest is same.
>
> Regards,
> Prabodh
>
> -----Original Message-----
> From:
> gang-of-4-patterns-admin AT cs.uiuc.edu
> [mailto:gang-of-4-patterns-admin AT cs.uiuc.edu]On
> Behalf Of Bharathi
> Thiyagarajan
> Sent: Friday, December 19, 2003 10:38 AM
> To: gag-of-4-patterns
> Subject: [gang-of-4-patterns] strategy, state , template ????
>
> Hello *
>
> I think I will give a glimpse of scenario of my problem so that
> we can discuss and conclude which will be the best design pattern
>
> Here is the scenario:
> I have 3 types of trace files (A , B, C) each has
> its own format and requires a different way of parsing the file to pick
> data from the file.
> so Afile has a Aparser, Bfile has Bparser, and
> Cfile has Cparser. apart from these Aparser, Bparser, Cparser there are
> common methods
> that can be reused for parsing the file.
>
> Now the design for this:
>
> Trace <>--------> TraceParser
>
> / | \ (sorry the lines
> here indicate inheritance/generalization)
>
> AparserType BparserType CparserType
>
> Points for discussion:
> 1. If TraceParse has a method ParserTrace which
> AparseType , BparseType , CparserType has different implementation for
> it --- It is Strategy
> 2. If TraceParser has many methods some of them are
> common for the three parser types and some of then have been overridden
> by the three parser Types -
> is it a template method ????
> 3. The input file at a point can be one of TraceType,
> Trace will decided by checking the file which TraceType is it and based
> on that object AparserType/
> BparserType/CparserType is created
>
> Please enlighten me , correct me so that I will learn from my
> mistakes.
>
> Thanks & Regards
> Bharathi
>
> _______________________________________________
> gang-of-4-patterns mailing list
> gang-of-4-patterns AT cs.uiuc.edu
> http://mail.cs.uiuc.edu/mailman/listinfo/gang-of-4-patterns

_______________________________________________
gang-of-4-patterns mailing list
gang-of-4-patterns AT cs.uiuc.edu
http://mail.cs.uiuc.edu/mailman/listinfo/gang-of-4-patterns





Archive powered by MHonArc 2.6.16.

Top of Page