Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 145885
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T08:30:10+00:00 2026-05-11T08:30:10+00:00

Here is an implementation example of the algorigthm in the base absctract class from

  • 0

Here is an implementation example of the algorigthm in the base absctract class from http://sourcemaking.com/design_patterns/template_method/php

public final function showBookTitleInfo($book_in) {     $title = $book_in->getTitle();     $author = $book_in->getAuthor();     $processedTitle = $this->processTitle($title);     $processedAuthor = $this->processAuthor($author);     if (NULL == $processedAuthor) {         $processed_info = $processedTitle;     } else {         $processed_info = $processedTitle.' by '.$processedAuthor;     }     return $processed_info; } 

I don’t like it becase I think, that ‘showBookTitleInfo’ knows too much about the methods it calls.

Here is another example abstract class template_method { var $state; public function __construct() { $this->state = 0; }

    public function processEvent( $event ) {         $this->doFirstStep( $event );         $this->doSecondStep( $event );     }     abstract public function doFirstStep( &$event );     abstract public function doSecondStep( &$event ); }  class CustomLogic extends template_method {     public function doFirstStep( &$event ) {         echo __METHOD__.': state: '.$this->state.' event: $event\n';         $this->state++;     }     public function doSecondStep( &$event ) {         echo __METHOD__.': state: '.$this->state.' event: $event\n';         $this->state++;     } } 

why we pass event as by-reference, if we don’t change its value? How should I implement ‘my steps’ logic, if they are using current state, can modify its value, and other steps can read modified value and can modify it too?

For example, I want to implement cost counting mechanism for scheduled message sending – simple and reccurent(ex: every Mon, Fri until 23.05.2009).

So, I implement the algorithm in abstract class as following:

abstract class AbstractCostCounter {     public function countNotReccurentSendingCost($messageObj) {         $totalMessages = $messageObj->getTotalMessages(); // multiple recipients are allowed         $message_cost = 1; // just to give you an idea         $this->cost = $totalMessages * $message_cost;     }     abstract public function countOptional();      // I pass $messageObject not as by-reference, because it hasn't to be modified     public function countCost( $messageObject ) {         $this->countNotReccurentSendingCost( $messageObject );         $this->countOptional( $messageObject );     }  }  class TemplateNotReccurentCostCounting {     public function countOptional($messageObj) {         // do nothing     } }  class TemplateReccurentCostCounting {     public function countOptional($messageObj) {         $notReccurentSendingCost = $this->cost;         $totalMessagesInScheduledPlan = $messageObj->getTotalMessagesInScheduledPlan();         $reccurentSendingPlanCost = $notReccurentSendingCost * $totalMessagesInScheduledPlan;         $this->cost = $reccurentSendingPlanCost;     } } 

Am I moving in the right direction? Is it where Template method design pattern should be implemented? Please let me know, if it is something wrong with this code.

P.S. cost counter is not a production code. I wrote it because I wanted to give you an idea.

Thanks, in advance

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T08:30:11+00:00Added an answer on May 11, 2026 at 8:30 am

    The template method pattern gives the parent class a lot of control, the parent class has to know a lot about the abstract methods (their signature) because it has to ‘control’ the algorithm. BTW the concrete method in the parent class has to be final.

    You have no advantage with your firstStep secondStep methods, I could implement what I want in stepOne and do nothing in stepTwo…

    The question is when would you want to use Template Method Pattern, not how to rewrite it to give more flexibility 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 108k
  • Answers 109k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer What you want is called a 'proxy' server. Unfortunately, there's… May 11, 2026 at 9:19 pm
  • Editorial Team
    Editorial Team added an answer The parameter(s) of VirtualHost are the local addresses you listen… May 11, 2026 at 9:19 pm
  • Editorial Team
    Editorial Team added an answer Context Root is defined by XML files in the WAR.… May 11, 2026 at 9:19 pm

Related Questions

I am working on a function to establish the entropy of a distribution. It
I'm developing a procedurally-generated game world in Python. The structure of the world will
I've been working on a visualization project for 2-dimensional continuous data. It's the kind
I have a path made up of a list of 2D points. I want

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.