doufei2194 2014-07-16 22:49
浏览 29
已采纳

设计模式,单例和传递参数[关闭]

There is one topic I've struggled with for quite some time and it lies in the realm of static methods, singletons and passing parameters around for an application. I've been trying to find a way to use classes in order to pass variables around. Here's an example:

class EmployeeSettings { 

    private static $statuses = array();
    private static $isInit = false;

    private static function init() {
        if(self::$isInit) return;

        self::$statuses = array(
            'active' => array(
                'value' => 0,
                'name' =>'Active'
            ),
            'terminated' =>array(
                'value' => 1,
                'name' =>'Termninated'
            ),
            'suspended' => array(
                'value' => 2,
                'name' =>'Suspended'
            ),  

        );  

        self::$isInit = true;
    }

    public static function getStatuses() {
        self::init();
        return self::$statuses;
    }   

    public static function getStatusName($status) {
        self::init();
        foreach(self::getStatuses() as $ref => $info) {
            if($info['value']==$status) {
                return self::$statuses[$ref]['name'];   
                break;
            }
        }   
    }
}

This would allow me to access any parameter from EmployeeSettings across the app. A similar pattern can be constructed where all the application's configuration would be stored:

class CoreConfig
{
    static $confArray;

    public static function read($name)
    {
        return self::$confArray[$name];
    }

    public static function write($name, $value)
    {
        self::$confArray[$name] = $value;
    }

}

Which is very "convenient" as I can get any configuration setting I need on the fly. Some would say this type of design is to be avoided for many reasons, none of which seem to tell me why they're bad and why they should be avoided. Often times it makes sense conceptually and architecturally to allow access only one instance of a particular class and I'm not sure on how to proceed any further.

  • 写回答

1条回答 默认 最新

  • douxu4610 2014-07-16 23:03
    关注

    There was a time when Singletons were all the rage.

    Then it was revealed that they are really globals in disguise! Oh, the horror!!

    Now the rage is Dependency Injection. Someday a "new thang" will come along and DI will be despised as well.

    Convenience is fleeting. Sooner or later it will fail you. You might try MVC frameworks instead of trying to re-invent wheels. A few of them are using the newest paradigms and are quite solid. I prefer Symfony2. Laravel is really popular at the moment. There are many others.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题