doutang7414 2016-03-02 22:29 采纳率: 0%
浏览 22

组合与继承与控制器

EDIT 3: Attempt at re asking the question.

class Config {
    protected $a;
    protected $b;
    public doStuff();
    public getA();
}

CASE 1

class Connection {
     protected $config;
     public function __construct(){
         $this->config = new Config();
     }
     public function getConfig(){
        return $this->config;
     }
}
$c = new connection();
$c->getConfig()->doStuff();
$otherClass->setA($c->getConfig()->getA());

CASE FACTORY/CONTROLLER

 $config = new Config();
 $config->doStuff();
 $a = $config->getA();
 $otherClass->setA($a);

Whats the difference ?

OLD QUESTION FOLLOWING, NOT CLEAR

So I was reading about composition vs inheritance even though it's been 8 years Im programming because you know modelling a program doesn't have simple clean cut answers such as a plane vs a biplane and an engine and this simple interrogation came over my mind.

A biplane IS a plane and a plane HAS an engine and HAS seats and HAS "most likely" windows.

The plane class has an engine, seats and windows. Classical case of composition. In other words, the plan contains 3 sub objects.

Now, what if, we had a factory of planes. This factory technically would assemble the plane meaning it would have a plane, an engine and seats. Now think about the principle of a controller which acts as an intermediary between the model and the view. It could almost be seen as a factory of different objects needed(the models) needed to create the view.

My question is, could I simply create a controller/factory which contains the seats,plane,windows object without the plane itself having those 3 previous objects embedded in the plane object ?

So, basically "decoupling" the "HAS logic" into one big factory/controller. What do you think ?

Edit: My question doesn't make sense in this specific example because the plane sub object actually return objects and dont do stuff. Imagine if those sub objects were only doing stuff and returning void

  • 写回答

1条回答 默认 最新

  • douweihui0178 2016-03-02 22:39
    关注

    The question is a bit hazy, but basically what you are describing is a IoC container, such as Spring, Guice (in the Java world) or the Service Container in Symfony 2 (in PHP).

    In all the above cases the controllers (and other pieces of application) are plain classes that do not need to inherit any functionality, because all the required bits and pieces are injected during the object construction, which is undertaken by a centralized factory.

    评论

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效