views:

282

answers:

7

I want to design a class, which contains a procedure to achieve a goal. And it must follow some order to make sure the last method, let's say "ExecuteIt", to behave correctly. in such a case, what design patter will you use ? which can make sure that the user must call the public method according some ordering.

If you really don't know what I am saying, then can you share me some concept of choosing a design patter, or what will you consider while design a class?

+6  A: 

I believe you are looking for the Template Method pattern.

Oded
Wow, you are cool, how can you remember all the patterns ?
MemoryLeak
@duffymo: Template Method is a GoF pattern as well, and fits the description better than strategy
Michael Borgwardt
I'll have to re-read it then. Thanks for the correction.
duffymo
Can any of the down votes please explain why they are down voting? What is wrong with this answer?
Oded
+3  A: 

Its basically not a pattern, but: If you want to make sure, the code/methods are executes in a specific order, make the class having only one public method, which then calls the non-public methods in the right sequence.

ogni42
A: 

As general concept you should choose a pattern as a standard solution to a standard problem so, I agree with Oded, the "Template Method" seems to fit your needs (but what you explained is too few maybe). Don´t use pattern as "fetish", what you have to keep in mind is:

  1. How can I figure my problem in a standard way?
  2. There is a pattern for this?
  3. Is this the simplest way?
DrFalk3n
+2  A: 

I think you have not to really execute nothing, just prepare the statements, resources and whatever you want. This way whatever would be the order the user invokes the methods the actual execution would be assured to be ordered; simply because you have the total control over the real execution, just before execute it.

IMHO Template Method as very little to do with your goal.

EDIT: to be more clear. Make your class to have one public method Execute, and a number of other public methods to tell your class what to do (when to do it is a responsibility of you and not of the user); then make a number of private methods doing the real job, they will be invoked in the right order by your Execute, once the user has finished settings things.

Give the user the ability of setting, keep execution for your self. He tells what, you decide how.

AlberT
and a number ofother public methods to tell your class what to do (when to do it is a your resonsibility, not of the user); Can you explain this in more detail ?
MemoryLeak
A: 

Template Method is rational, if you have a class hierarchy and base class defines protected operation steps in its public template method. Could you elaborate your question?

baris_a
+3  A: 

The simple and pragmatic approach to enforcing a particular sequence of steps in any API is to define a collection of classes (instead of just one class) in such way that every next valid step takes as a parameter an object derived from the previous step, i.e.:

   Fuel   coal   = CoalMine.getCoal();
   Cooker stove  = new Cooker (gas); 

   Filling apple = new AppleFilling();
   Pie applePie = new Pie(apple);

   applePie.bake(stove);

That is to say that to bake a pie you need to supply a Cooker object that in turn requires some sort of a suitable fuel to be instantiated first. Similarly, before you can get an instanse of a Pie you'd need to get some Filling ready.

In this instance the semantics of the API use are explicitly enforced by its syntax. Keep it simple.

Totophil
Is there a name for this design pattern?
Frank
Frank, it doesn't have a name I would be aware of. But "Type System Design Pattern" could do.
Totophil
I think it is decorator pattern, but since in my class I only do one thing, I think it is not that good to use multiple class
MemoryLeak
+3  A: 

Template Method is what you want. It is one of the oldest, simply a formalization of a way of composing your classes.

http://en.wikipedia.org/wiki/Template_method_pattern

or as in this code sample:

abstract class AbstractParent // this is the template class
{
    // this is the template method that enforces an order of method execution
   final void executeIt()
   {
     doBefore(); // << to be implemented by subclasses
     doInTheMiddle() // also to be implemented by subclasses
     doLast(); // << the one you want to make sure gets executed last
   }

   abstract void doBefore();
   abstract void doInTheMiddle();
   final void doLast(){ .... }
}

class SubA extends AbstractParent
{
   void doBefore(){ ... does something ...}
   void doInTheMiddle(){ ... does something ...}
}

class SubB extends SubA
{
   void doBefore(){ ... does something different ...}
}

But it seems you are fishing for an opportunity to use a pattern as opposed to use a pattern to solve a specific type of problem. That will only lead you to bad software development habits.

Don't think about patterns. Think about how you would go around solving that specific problem without having patterns.

Imagine there were no codified patterns (which is how it was before). How would you accomplish what you want to do here (which is what people did to solve this type of problems.) When you can do that, then you will be in a much better position to understand patterns.

Don't use them as cookie cutters. That is the last thing you want to do.

luis.espinal
I need to clarify this - I don't mean that patterns are useless. They are one of the best things Computer Science has come up with when it comes to re-usability and composition. However, it requires you to know composition, inheritance and polymorphism by themselves (and to a lesser degree, concepts such as cohesion and coupling.)A person who knows how to use patterns effectively is one who knows how to come up with solutions that are strictly based on the concepts mentioned above. Patterns give you a documented structure, but you need to have a solid foundation before you can use them.
luis.espinal