English 中文(简体)
模块设计模式
原标题:Modular design pattern

我试图决定设计一个旨在允许大量极端化的系统。 从我能够知道的情况来看,像抽象工厂这样的模式,除了重复法典之外,还不允许凌驾于基本方法之上(如下文所示)。

我对面向方面的方案规划做了一些初步研究,似乎与我所期望的思路一致,但我花了很困难的时间把我的头部总结在细节上。

abstract class Object {

    protected $object_id;
    protected $name;

    function LoadObjectData()
    {
        $file_contents = readfile( object .$object_id. .data );
        $data = array();
        // parse file contents into $data array...
        return $data;
    }

    function Create()
    {
        $data = $this->LoadObjectData();
        $name = $data[ name ];
        return $data;
    }

}

class User extends Object {

    protected $email_address;

    function Create()
    {
        $data = parent::Create();
        $this->email_address = $data[ email_address ];
        return $data;
    }

}

//----------Module 1-MySQL Lookup-------------
/*
 * Redefine Object::LoadObjectData() as follows:
*/

function LoadObjectData()
{
    $data = array();
    $result = mysql_query("SELECT...");
    // construct array from result set
    return $data;
}

//----------Module 2-Cache Machine-------------
/*
 * Redefine Object::LoadObjectData() as follows:
 */

function LoadObjectData()
{
    if (exists_in_cache($object_id)) {
        return get_cached_object($object_id);
    }
    $data = parent::LoadObjectData();
    cache_object($object_id, $data);
    return $data;
}

(这是一个不好的事例,但希望它有助于使我的观点贯穿整个过程)

打算采用的系统将有很大比例的方法可以推广,我希望尽量减少开发商所需的额外努力和学习。

《禁止杀伤人员地雷公约》是我所期待的,还是有更好的办法处理这一问题?

Thanks!

问题回答

因此,你想要使用一种装饰模式,而没有界定腐蚀物本身。

如果是的话,它就是一种mon干,可以采用面向方面的工具。 可以通过下列延伸和框架轻易解决这一问题:

  1. PHP Runkit Extension
  2. Go! Aspect-Oriented framework for PHP
  3. PHP-AOP Extension.

你不必宣布基类为抽象类别。 你们能够使其成为正规的班级,并根据经过的建筑参数装上其他班级。 施工人员可以返回一个班级,而不仅仅是建筑商的班级。 为了避免重叠代码,你可以将静态功能与即时功能和变量混为一谈。 仅仅记住,所有情况下的固定功能或变数都是一样的。 改变一个静态变量,所有情况都发生变化。 原始结构的一个相当基本的例子。

class BaseObject {
    protected static $cache = array();

    public function __construct($load_plugin) {
        require_once($load_plugin. .class.php );
        $object  = new $load_plugin();
        return $object;
    }

    public static function cacheData($cache_key, $data) {
        self::$cache[$cache_key] = $data;
    }
}

class Plugin extends BaseObject {
    public function __construct() {
    }

    public function loadData() {
        // Check the cache first
        if ( !isset(self::$cache[$cache_key]) ) {
            // Load the data into cache
            $data =  data to cache ;
            self::cacheData($cache_key, $data);
        }
        return self::$cache[$cache_key];
     }
}




相关问题
Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

JSON with classes?

Is there a standardized way to store classes in JSON, and then converting them back into classes again from a string? For example, I might have an array of objects of type Questions. I d like to ...

Object-Oriented Perl constructor syntax and named parameters

I m a little confused about what is going on in Perl constructors. I found these two examples perldoc perlbot. package Foo; #In Perl, the constructor is just a subroutine called new. sub new { #I ...

Passing another class amongst instances

I was wondering what is the best practice re. passing (another class) amongst two instances of the same class (lets call this Primary ). So, essentially in the constructor for the first, i can ...

Where can I find object-oriented Perl tutorials? [closed]

A Google search yields a number of results - but which ones are the best? The Perl site appears to contain two - perlboot and perltoot. I m reading these now, but what else is out there? Note: I ve ...

热门标签