سؤال

Whats is best? Work with low-level methods receiving some arguments and dealing with it. Or have a high-level interface in the objects that does exactly whats its name says?

eg.:

Low-level:

<?php

class Html {
    public function renderSelect($name, $options) {
        //
    }
}

class Repository {
    public function lists($repositoryName, $keyColumn, $valueColumn) {
        //
    }
}

# usage

$html->renderSelect('campaign_id', $repository->lists('campaigns', 'id', 'name'));
$html->renderSelect('account_id', $repository->lists('accounts', 'id', 'company'));

High-level:

<?php

class Html {
    public function renderSelect($name, $options) {
        //
    }

    public function renderCampaignsSelect() {
        return $this->renderSelect('campaign_id', $repository->listsCampaigns());
    }

    public function renderAccountsSelect() {
        return $this->renderSelect('account_id', $repository->listsAccounts());
    }
}

class Repository {
    public function lists($repositoryName, $keyColumn, $valueColumn) {
        //
    }

    public function listsCampaigns() {
        return $this->lists('campaigns', 'id', 'name');
    }

    public function listsAccounts() {
        return $this->lists('accounts', 'id', 'company');
    }
}

# usage

$html->renderCampaignsSelect();
$html->renderAccountsSelect();

Notably the high-level option will grow as applications scales, if more entities comes up, more methods will be needed, like: added Sponsor will have renderSponsorsSelect and listsSponsors. But its usage makes the code very smooth to read and we can do different implementations for each method.

What do you think?

Thanks.

هل كانت مفيدة؟

المحلول

Low-level (fine-grained) methods are more reusable. High-level(coarser granularity) methods are easier to use. I think the closer to the user interface layer the more higher level methods are preferred, it can hide implementation details and is easier to read as you metioned.

نصائح أخرى

Use what you call "low-level" in generic API, frameworks. A kind of libraries designed to be used in many other projects in different domains. Examples: Symfony, Zend Framework.

Use what you call "high-level" in domain models. Projects targeted to solve a specific problem. But instead of calling it high-level, say domain-specific or using ubiquitous language. Examples: phpBB3, Wordpress, your site generaing funny pictures of cats with customised text.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top