关于框架设计的建议 [英] Advice on framework design

查看:136
本文介绍了关于框架设计的建议的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为一个项目构建一些迷你框架,并提出了这个解决方案。我试过很多,但这似乎对我来说非常方便(代码简化简化):

 #基本上只是一个注册表模式
class Repository {
private static $ objects = array();

public function loadObject($ alias,$ object){
self :: $ objects [$ alias] = $ object;
返回true;
}

public function __get($ name){
if($ this-> objectExists($ name)){
return self :: $ objects [ $名称];
} else {
return false;
}
}
}

类数据库扩展存储库{
/ *数据库类* /
}

类会话扩展Repository {
public function some_func($ key,$ value){
/ *我可以访问数据库对象使用$ this扩展Repository * /
$这个 - >数据库 - > exec(/ * sql * /);
}
}

/ * =================== * /

#加载核心对象
$ R = new Repository :: getInstance();
$ R - > loadObject('config',new Config());
$ R - > loadObject('database',new Database());
$ R - > loadObject('session',new Session());

/ * =================== * /

您可以看到这种方法有任何问题或缺点吗?对于我来说,我可能会看到更多的内存消耗,因为每个下一个类都拥有来自Repository的越来越多的对象。
在我有一个设计之前,每个类是独立的,但无论如何,所有这些都需要数据库,会话,配置等,否则我不得不在任何类中声明它们。
只是想注意,我正在为核心对象规划此设计,而不是针对特定类。

解决方案

因为每个下一个类都存有来自存储库的越来越多的对象 - 我不完全明白你的意思,我认为对象是静态的,只有一个副本。



  class Repository $ b我想你可以使用一些不同的方法来避免缺点。 $ b {
private static $ instance;
private $ objects = array();
private static getInstance()
{
if(!Repository :: $ instance)
!Repository :: $ instance = new Repository();
return!Repository :: $ instance();
}

public static function loadObject($ alias,$ object)
{
Repository :: getInstance() - > objects [$ alias] = $ object ;
返回true;
}

public static function get($ name)
{
$ repository = Repository :: getInstance();
if(isset($ repository-> objects [$ name]
return $ repository-> objects [$ name];
else
return false;

然后,您将在您的小孩课程中使用:

  Repository :: get('config'); 

和引导

 存储库:: loadObject('config',新的Config()); 
存储库:: loadObject('database',new Database());

等。 b $ b

i'm currently constructing some kind of mini-framework for a project, and come up with this solution. I have tried many of them, but this seems to me very convinient (code is shortened for simplicity):

# Basically it's just a Registry pattern    
    class Repository {
        private static $objects = array();

        public function loadObject($alias, $object) {
            self :: $objects[$alias] = $object;
            return true;
        }

        public function __get($name) {
            if ($this->objectExists($name)) {
                return self::$objects[$name];
            } else {
                return false;
            }
        }
    }

    class Database extends Repository {
        /* database class */
    }

    class Session extends Repository {
        public function some_func($key, $value) {
            /* i can access database object using $this in any class that extends Repository */
            $this -> database -> exec (/* sql */);
        }
    }

    /* =================== */

    # Load core objects
    $R = new Repository :: getInstance();
    $R -> loadObject ('config', new Config());
    $R -> loadObject ('database', new Database());
    $R -> loadObject ('session', new Session());

    /* =================== */

Can you see any problems or drawbacks with this approach? For me i see maybe i little more memory consumption, because each next class holds more and more objects from Repository. Before i had a design where each class was independent, but anyway all of them require database, session, config etc, no i had to declare them in any class. Just wanted to note that i'm planning this design only for core objects, not for specific classes.

解决方案

"because each next class holds more and more objects from Repository" - I don't exactly understand what you meant by that, I think as the objects are static there's only one copy.

I think you can use a little bit different approach to avoid drawback, by combining singleton pattern.

class Repository
{
  private static $instance;
  private $objects = array();
  private static getInstance()
  {
    if (!Repository::$instance)
      !Repository::$instance = new Repository();
    return !Repository::$instance();
  }

  public static function loadObject($alias, $object)
  {
    Repository::getInstance()->objects[$alias] = $object;
    return true;
  }

  public static function get($name)
  {
    $repository = Repository::getInstance();
    if (isset($repository->objects[$name]
      return $repository->objects[$name];
    else
      return false;
}

You will then use this in your child classes:

Repository::get('config');

and in bootstrap

Repository::loadObject('config', new Config());
Repository::loadObject('database', new Database());

etc.

这篇关于关于框架设计的建议的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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