mardi 5 novembre 2019

Design patterns - Separate classes difference in constructor only

Question to the design patterns junkies out there, because we've been having a team argument over this for far longer than we should.

So we use a framework, Yii2, that creates default models for the database tables. In one example, table t_receipt_settings has the model TReceiptSettings. Because we have some default settings, one approach to carry those default settings was to create another class DefaultReceiptSettings that extends TReceiptSettings with only a different constructor. Example:

class DefaultReceiptSettings extends TReceiptSettings
{
    public function __construct($config = array()) {
        $this->print_website_address = 1;
        $this->print_email_address = 1;
        $this->print_phone_no = 1;
        $this->print_cash_reg_name = 1;
        parent::__construct($config);
    }

}

Now the reviews that this method collected were either:

  1. This is WRONG. You should create a static method in TReceiptSettings that sets the defaults for you. OR
  2. This is a CORRECT approach. Creating a new class with initial defaults is in fact favorable. Having in your code elsewhere new DefaultReceiptSettings(); is much better than the alternative.

Is this a good solution? an acceptable solution? or a wrong solution? And why?

Aucun commentaire:

Enregistrer un commentaire