类:: getInstance的原产地/说明? [英] Origin/Explanation of Class::getInstance?

查看:104
本文介绍了类:: getInstance的原产地/说明?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对于经典继承相当新,因为我主要处理ECMAScript和Python,虽然我做一个公平的(颤抖)PHP。我知道它受到Java和其他基于古典继承的语言的深深影响。

I'm fairly new to classical inheritance as I mainly deal with ECMAScript and Python, though I do a fair bit of ( shudder ) PHP. I know it's heavily influenced by Java and other classical inheritance based languages.

问题:

在一个框架中看几个类,并注意到'new'关键字没有被调用(至少直接)创建一个实例,但是使用public getInstance方法来创建初始对象。

I was taking a peek at a few classes in a framework and noticed that the 'new' keyword wasn't called ( directly at least ) to create an instance, but the public getInstance method was used to create the initial object.

有人可以解释背后的策略吗?

Can someone explain the strategy behind this? And when should I use it for my own classes?

相关代码:

class FrontController {
    public static $_instance;

    public static function getInstance() {
        if ( !(self::$_instance instanceof self) ) {
            self::$_instance = new self();
        }
        return self::$_instance;
    }
}

$front = FrontController::getInstance();
$front->route();
echo $front->getBody();

完整代码:



class FrontController
{
    protected $_controller, $_action, $_params, $_body, $_url;

    public static $_instance;

    public static function getInstance()
    {
    if ( ! ( self::$_instance instanceof self) ) {
        self::$_instance = new self();
    }
    return self::$_instance;
    }

    private function __construct() {
    $this->uri = uri::getInstance();

    if ( $this->uri->fragment(0) ) {
        $this->_controller = $this->uri->fragment(0).'Controller';
    }
    else {
        $config = config::getInstance();
        $default = $config->config_values['application']['default_controller'].'Controller';
        $this->_controller = $default;
    }

    if ( $this->uri->fragment(1) ) {
        $this->_action = $this->_uri->fragment(1);
    } else {
        $this->_action = 'index';
    }
    }

    public function route() {
    $con = $this->getController();
    $rc = new ReflectionClass( $con );

    if ( $rc->implementsInterface( 'IController' ) ) {
        $controller = $rc->newInstance();

        if ( $rc->hasMethod( $this->getAction() ) ) {
        $method = $rc->getMethod( $this->getAction() );
        } else {
        $config = config::getInstance();
        $default = $config->config_values['application']['default_action'];
        $method = $rc->getMethod( $default );
        }
        $method->invoke( $controller );
    } else {
        throw new Exception('Interface iController must be implemented');
    }
    }

    public function getController() {
    if ( class_exists( $this->_controller ) ) {
        return $this->_controller;
    }
    else {
        $config = config::getInstance();
        $default = $config->config_values['application']['error_controller'].'Controller';
        return $default;
    }
    }

    public function getAction() {
    return $this->_action;
    }

    public function getBody() {
    return $this->_body;
    }

    public function setBody( $body ) {
    $this->_body = $body;
    }
}


推荐答案

非常简单。看起来,很多像Zend Framework的想法?

无论如何,这实现了Singleton设计模式。它禁止程序员创建多个控制器实例。所以,你是从代码中的任何地方访问你的控制器,而不是创建一个新的FrontController或类似的,你只是要求给予一个以前创建的一个实例。



如果没有实例存在,它只创建一个其他的,只返回你之前创建的前控制器。

It's quite simple. Looks, a lot like Zend Framework thought?
Anyway, this implements the Singleton design pattern. It's forbidding the programmer to create more than one instance of the controller. So, is you access your controller from anywhere in your code, instead of creating a new FrontController or something like that you just ask to be given the One instance that as been previously created.

If no instance exist, it's creating one else it only return you the front controller wich as been previously created.



Hope这有助于你!


Hope this helps you!

这篇关于类:: getInstance的原产地/说明?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆