Which design template do you choose?

I want to create a class that contains a procedure to achieve the goal. And it must follow a certain order to make sure that the last method, say, "ExecuteIt", must behave correctly. In that case, what design are you using? which can guarantee that the user must call the public method in a certain order.

If you really don't know what I'm saying, can you share me with some concept of design choice, or what will you consider when developing a class?

+5
source share
7 answers

The template method is what you want. This is one of the oldest, just a formalization of the way you compose your classes.

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

or as in this code example:

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 looks like you are catching the opportunity to use a pattern, rather than using a pattern to solve a specific type of problem. This will only lead to poor software development.

Do not think about templates. Think about how you will solve the problem without having templates.

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

. , .

+4
+6

, : , /​​ , , .

+3

API - ( ) , , , :

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

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

   applePie.bake(stove);

, , , , , , . , , .

API . .

+3

, , , , . , , , ; , , , .

IMHO Template Method .

EDIT: . Execute , , ( , ); , , Execute, .

, . , , .

+2

, Oded, " , -, ( , , ). "", :

  • ?
  • ?
  • ?
0

, , . ?

0

All Articles