Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/290.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 选择要使用的正确模式;“司机”;并将其结合起来_Php_Design Patterns - Fatal编程技术网

Php 选择要使用的正确模式;“司机”;并将其结合起来

Php 选择要使用的正确模式;“司机”;并将其结合起来,php,design-patterns,Php,Design Patterns,我不熟悉设计模式。我已经在我使用过的框架上使用了它,但是现在我需要为我想要做的事情选择正确的框架。我的问题是下一个: 我有两个数据库需要提取统计数据。我将称之为lorem和ipsum。在某些情况下,我只需要一个、另一个或两个组合的数据。实际上,我有如下函数: 盖特尔 getAllFromLorem getAllFromLipsum 我想我可以使用工厂模式来做这样的事情: <?php $lorem = Stats::factory('lorem'); $lorem->getAll(

我不熟悉设计模式。我已经在我使用过的框架上使用了它,但是现在我需要为我想要做的事情选择正确的框架。我的问题是下一个:

我有两个数据库需要提取统计数据。我将称之为loremipsum。在某些情况下,我只需要一个、另一个或两个组合的数据。实际上,我有如下函数:

  • 盖特尔
  • getAllFromLorem
  • getAllFromLipsum
我想我可以使用工厂模式来做这样的事情:

<?php
$lorem = Stats::factory('lorem');
$lorem->getAll();

$lipsum = Stats::factory('lipsum');
$lipsum->getAll();
当我制造一个或其他驱动程序时,它将使用另一个文件的getAll

我认为这是正确的。但我也有一个问题。我希望有一些函数可以在内部组合它,比如:

/stats/lorem.php
/stats/lipsum.php
/stats.php
<?php
$all = Stats::factory();
$all->getAll(); // this is lorem and ipsum combined (by me, not auto of course...)
interface StatsSource {
    function getAll ();
}

这个想法是让您的
Stats::factory()
方法返回特定接口的对象。在这种情况下,接口类似于:

/stats/lorem.php
/stats/lipsum.php
/stats.php
<?php
$all = Stats::factory();
$all->getAll(); // this is lorem and ipsum combined (by me, not auto of course...)
interface StatsSource {
    function getAll ();
}
一个实现是“单一数据库”版本,另一个实现是组合结果的组合源版本。下面是一个简单的实现,它将两个源的结果合并在一起

class SingleDBStatsSource implements StatsSource {

    function __construct ($database) {
        // $database would be the name of the db to use
        // or better yet, a connection to the specific db.
    }

    function getAll ()
    {
        // use databse connection to retrieve all records.
    }        

}


class CombinedStatsSource implements StatsSource {

    function __construct ($statsSourceList) {
        $this->statsSourceList = $statsSourceList;
    }

    function getAll ()
    {
        $results = array();
        foreach ($this->statsSourceList as $statsSource) {
            $results = array_merge($results, $statsSource->getAll());
        }
        return $results;
    }        

}

其思想是让您的
Stats::factory()
方法返回特定接口的对象。在这种情况下,接口类似于:

/stats/lorem.php
/stats/lipsum.php
/stats.php
<?php
$all = Stats::factory();
$all->getAll(); // this is lorem and ipsum combined (by me, not auto of course...)
interface StatsSource {
    function getAll ();
}
一个实现是“单一数据库”版本,另一个实现是组合结果的组合源版本。下面是一个简单的实现,它将两个源的结果合并在一起

class SingleDBStatsSource implements StatsSource {

    function __construct ($database) {
        // $database would be the name of the db to use
        // or better yet, a connection to the specific db.
    }

    function getAll ()
    {
        // use databse connection to retrieve all records.
    }        

}


class CombinedStatsSource implements StatsSource {

    function __construct ($statsSourceList) {
        $this->statsSourceList = $statsSourceList;
    }

    function getAll ()
    {
        $results = array();
        foreach ($this->statsSourceList as $statsSource) {
            $results = array_merge($results, $statsSource->getAll());
        }
        return $results;
    }        

}