今天的一个面试题,总结一下

题目:

输入是 日期,时间, 姓名,结果

但是数据源不清楚,可能是文件,可能是数据库

处理也不清,可能计算一段时期的人数,也可能统计别的

输出也不清楚,可能echo出来,也可以写入到文件或者数据库

要求写出程序能够满足这些变化的需求。

 

实现:

     用到的设计模式:

           工厂模式,策略模式

     文件结构: 

 

 

 

看到题目中得那么多的不确定因素,就想到了用一种良好的设计模式来实现他~

首先,程序应该分成输入,处理,和输出三部分~~我们不妨给这个程序初始化接口

 

<?php
interface initialCalculate
{
    //输入
    public function input($inputConf);
    //处理
    public function handle($inputArray);
    //输出
    public function output();
}

 

然后我们就该对这个接口来实现了~~

下面这个类具体实现的是从文件里读取数据,统计结果数,之后在屏幕上输出来

当然如果需要别的实现的话, 也可以再编写一个这样的类


<?php
class calculateNumberFromLogFile implements initialCalculate
{
    public function input($inputConf)
    {
        $calculate = new calculate;
        $inputor = $calculate->creatInputor('logFile','/document/');//输入
        return $inputor->getCalculateInfo($inputConf);//获取固定格式输入数组
    }

    public function handle($inputArray)
    {
        $calculate = new calculate;
        $handler = $calculate->creatHandler('Passed','./');//统计处理
        return $handler->handleCalculateInfo($inputArray);//统计
    }

    public function output()
    {
        $calculate = new calculate;
        $outputor = $calculate->creatOutputor('printScreen','Passed');//输出
        return $outputor->outputCalculateInfo();//输出
    }
}

 

 在上面的类中,不管你想用哪个数据源,哪个处理方式,哪个输出方式 都可以通过 calculate类来统一调用

 

<?php
/**
* calculate base class
*/
class calculate
{

    private $inputDir  = 'method/inputor';
    private $handleDir = 'method/handler';
    private $outputDir = 'method/outputor';

    public function __construct(){}

    public function creatInputor($method,$path = '')
    {
        include $this->inputDir.$path.$method.'.php';
        return new $method;
    }

    public function creatHandler($method,$path = '')
    {
        include $this->handleDir.$path.$method.'.php';
        return new $method;
    }

    public function creatOutputor($method,$handler)
    {
        include $this->outputDir.'/'.$handler.'/'.$method.'.php';
        return new $method;
    }
}

 

输入,处理,输出类:

<?php
/**
*  Input file for the log file
*/
class logFile
{
    public function __construct(){}
    
    public function getCalculateInfo($logFileConf)
    {
        if(empty($logFileConf))
        {
            exit('error');
        }
        $items = array();
        $results = array();
        $logFileArray = file($logFileConf['filePath']);
        foreach($logFileArray as $temp)
        {
            $items = explode('|', $temp);
            $line['date'] = $items[0];
            $line['interviewer'] = $items[1];
            $line['interviewee'] = $items[2];
            $line['time'] = $items[3];
            $line['result'] = trim($items[4]);
            $results[] = $line;
        }
        return $results;
    }
}

 

<?php
/**
*  handler file
*/
class Passed
{
    public static $count;
    public static $pass;
    public function __construct(){}
    
    public function handleCalculateInfo($inputArray)
    {
        if(empty($inputArray))
        {
            exit('error');
        }
        foreach($inputArray as $line)
        {
            if($line['interviewer'] == '张三' && $line['date'] == date('Ymd', time()))
            {
                self::$count += 1;
                if($line['result'] == 'pass')
                {
                    self::$pass += 1;
                }
            }
        }
        return 0;
    }
}

 

<?php
/**
*  output file
*/
class printScreen
{
    public function __construct(){}
    
    public function outputCalculateInfo()
    {
        echo '今天的人数为:'.Passed::$count.'人<br />';
        echo '通过的人数为:'.Passed::$pass.'人<br />';
        return 0;
    }
}

 

 

如果要添加一种输入的来源,在lib\method\inputor\... 下编写输入类
添加统计方式,在lib\method\handler 下编写统计类
添加输出方式,在lib\method\outputor\... 下编写输出类

 

然后根据接口lib\initialCalculate.php  在lib\initial 下编写类

 

今天面试写的程序~~ 应该还有很多不足之处

 

 

 

posted on 2011-10-26 22:40  codingpp  阅读(485)  评论(1编辑  收藏  举报

导航