dpsq8476 2015-09-04 23:08
浏览 40

当模型引用另一个模型时,它是否会损害Demeter定律?

I have a global container class:

final class Container
{
    /**
     * @return ForumThread
     */ 
    public static function getForumThread()
    {
        if (self::$obj1 === null)
        {
             self::$obj1 = new ForumThread();
        }
        return self::$obj1;
    }

    /**
     * @return ForumPosts
     */ 
    public static function getForumPosts()
    {
        if (self::$obj2 === null)
        {
             self::$obj2 = new ForumPosts();
        }
        return self::$obj2;
    }
}

my models:

class ForumThread
{
    /**
     * @return bool
     */
    public function findBadLanguage ($inWhat)
    {
        return (bool)rand(0,1);
    }

    /**
     * @return
     */
    public function add ($threadName)
    {
        if (!$this->findBadLanguage ($threadName))
        {
             INSERT INTO
        }
    }
}

class ForumPost
{
    /**
     * @return
     */
    public function post ($toThreadId, $comment)
    {
        // im talking about this:
        Container::getForumThread()->findBadLanguage($comment);
    }
}

I know findBadLanguage() should be in another class, but lets suppose thats okay. Lets focus on Container::get****() calls. Is it OK to turn to a global container and get objects from it? Doesnt it hury Demeter's law? (those object must be exists only once, and can be DI-ed)

EDIT: you can regard to the Container as a Factory

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 #MATLAB仿真#车辆换道路径规划
    • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
    • ¥15 数据可视化Python
    • ¥15 要给毕业设计添加扫码登录的功能!!有偿
    • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
    • ¥15 微信公众号自制会员卡没有收款渠道啊
    • ¥100 Jenkins自动化部署—悬赏100元
    • ¥15 关于#python#的问题:求帮写python代码
    • ¥20 MATLAB画图图形出现上下震荡的线条
    • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘