Mean, sick, and poor is no way to go through life, son.

Design pattern question

class 1
*******
f1()
f2()
f3()
f4()
f5()

class 2 uses class 1
*********************
M1() calls f1() f2()
M2() calls f3() f4() f5()

what is the design pattern?
Permalink Ron 
April 15th, 2017 7:24pm
Pulls up a chair.
Permalink Student programmer 
April 15th, 2017 8:45pm
Abstract Factory
Permalink Georgeous George 
April 15th, 2017 9:02pm
<This class calls a bunch of shit in that class>-ton.

Is this homework?
Permalink Bored Bystander 
April 15th, 2017 9:46pm
BB is close but the correct answer is "the high frequency trading system pattern, obviously".
Permalink Io 
April 16th, 2017 12:57am
Lol.
Permalink Bored Bystander 
April 16th, 2017 12:58am
Procedure Factory
Permalink Trog 
April 16th, 2017 3:53am
You're outlining a symptom.

State the goal instead.

Design patterns are *solutions* to problems that are repetend.

So, ask yourself this -- what's the problem we're trying to solve? And not -- who calls whom. The latter is an observation on the structure of your components. That's totally irrelevant.

Without any context about your goal (and I infer that you're just trying to learn design patterns and don't really have a larger business goal you're trying to accomplish), here is your answer.

1. If class 2 just calls class 1 methods, and that's all the information that's available, then that's no design pattern.

2. But if class 2 methods call into class 1 methods because class 1 is an internal / package level class that's not accessible to "the outside world" outside the package / module / assembly, then class 2 is acting like a *Facade* or a *Proxy* for class 1.

3. If both class 1 and class 2 are accessible outside but class 3, which needs to use class 1 expects a certain interface (a certain signatures of methods to call) but class 1 does not have those conforming methods with conforming signatures, and class 2 now provides those conforming methods so that class 3 can call into class 1 through this indirection of class 2, then class 2 is acting like an *Adapter* between classes 1 and 3.

Keep studying but always ask about the goal. And observe the problem if you are given one.

Most programmers study design patterns the wrong way and/or with the wrong intention -- to win a pissing contest in their company.

Just keep at it but remember to not obsess over structure. Ask yourself the goal and if you're still not getting it (it will take many years), don't try to hold it too tight. Let the hold lose and in a few years' time, they will *happen* to you. They will come to you.

Focus on solving problems.

But keep studying.
Permalink Woo Woo 
April 16th, 2017 7:18am
Design patterns are code that most experienced programmers would end up writing when presented with a problem, whether or not they studied design patterns the way the world studies them.

Most of them are intuitive solutions to an easy problem.
Permalink Woo Woo 
April 16th, 2017 7:24am
Woo Woo, I didn't present it well so I'll clarify...

class A
*******
Step1()
Step2()
Step3()
Step4()
Step5()

Init() // step 1 + 2
Play() // step 3,4,5

The client code must first call Init and then he can use Play() with no limit.... The client code can also do the Init() at any time.....

So now I understand the this fit's the Template Pattern....

PS - Woo Woo I very much appreciate your help... :)
Permalink Ron 
April 16th, 2017 10:12am
Homework.
Permalink Bored Bystander 
April 16th, 2017 10:21am
I give him credit for getting help on his homework.
Permalink Shylock 
April 16th, 2017 12:48pm
Yes, that's the template.

But the template has a larger motivation than delegating function calls. It serves to create a broad template, i.e. a framework that defines the sequence of activities that constitute a larger activity without defining the finer details of the sub-activities.

In your own example, the Init() method provides a framework, i.e. a sequence. It says: when someone calls Init, step 1 must happen, then only step 2 must happen and then nothing else must happen.

However, it does not say anything about what must happen inside Step1(). It leaves that to the specific class or component that will provide the implementation of Step1(). In your case, it is the same class, but it could well have been another class like so:

abstract class Game
{
  public void Init()
  {
    try
    {
      // create game background
      Step1();
    }
    catch(Exception ex)
    {
      if (ex.InnerException != null)
      {
        Step100(ex.InnerException);
        return;
      }
     
      Step101(ex);
      throw ex;
    }
  }

  // Creates background image of the game
  public abstract void Step1();

  // Choose theme
  public virtual void Step2() { /* select default theme */ }
 
  public abstract void Step100(Exception ex);

  public virtual void Step101(Exception ex) {
    // log the exception
  }
}


class Mario : Game
{
  override void Step1()
  {
    // create blue color background with green trees
    // blue sky, a nice river and a hut
  }

  // We won't override the default theme. We'll
  // let the default theme apply. So we won't override
  // the Step2 method.

  override void Step100(Exception ex)
  {
    // do something with the inner exception 
  }
}

class MarioX : Mario
{
  // We want a city theme with high-rise
  // buildings, so we'll override Step1 method.
  override void Step2()
  {
   
  }

  // We also want less saturated colors like
  // the movie Matrix, so we'll have our own
  // color pallete and our own theme
  override void Step2()
  {
  }
}

See what the base class did? It provided a framework in the form of a rigid sequence of steps that *must* happen without defining *how* they will happen, in deference to specific implementers.

That's the goal of the Template pattern. And that's probably what you wanted.
Permalink Woo Woo 
April 16th, 2017 12:56pm
The try block inside the base class Game should have read:

try
{
  // create game background
  Step1();

  // create a default theme
  Step2();
}

I left out the Step2 the last time.
Permalink Woo Woo 
April 16th, 2017 12:58pm
And the actual step functions such as Step1, Step2, etc. are usually protected with the 'protected' access specifier, thus securing against the possibility of any client code defining its own flow. The clients then have to abide by the template defined by the template method.
Permalink Woo Woo 
April 16th, 2017 1:00pm
Did you just do his homework?
Permalink Terrorist Watch 
April 16th, 2017 1:07pm
No, just helping out. Only 10 minutes or less spent. No harm in that. :-)
Permalink Woo Woo 
April 16th, 2017 1:57pm
Gahhh. Too much insight, thought and effort to expend on a random CoT knucklehead.
Permalink Bored Bystander 
April 16th, 2017 2:12pm
@BB: Seeing his two recent questions and the condescension in his tone, I agree with your last remark. He's not worth spending time with.

I was foolish to indulge him. I thought he was in earnest.

He's one of those parasitic freeloaders. He has no idea about his own questions. And they're not even questions he has in his incapable mind. He's just copying and pasting questions he got for his homework or something. What an asshole!
Permalink Woo Woo 
April 18th, 2017 1:16am

This topic is archived. No further replies will be accepted.

Other topics: April, 2017 Other topics: April, 2017 Recent topics Recent topics