Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/297.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 面向对象的内聚/耦合混淆_Php_Oop - Fatal编程技术网

Php 面向对象的内聚/耦合混淆

Php 面向对象的内聚/耦合混淆,php,oop,Php,Oop,我有一个关于分割对象的正确方法的问题 假设我有两个对象,比如工具和技术。一项技术可以有多个与之关联的工具对象,但是一项技术直接了解一个工具是否被认为是低内聚/紧密耦合?让技术对象包含一组工具对象是一种不好的做法,还是应该有某种第三类将它们连接在一起 Tool private id; private title; Technology private id; private title; private tools; 您能否简单概述一下如何最好地将对象与其他对象关联起来?乍一看,对我来说似乎还

我有一个关于分割对象的正确方法的问题

假设我有两个对象,比如工具和技术。一项技术可以有多个与之关联的工具对象,但是一项技术直接了解一个工具是否被认为是低内聚/紧密耦合?让技术对象包含一组工具对象是一种不好的做法,还是应该有某种第三类将它们连接在一起

Tool
private id;
private title;

Technology
private id;
private title;
private tools;

您能否简单概述一下如何最好地将对象与其他对象关联起来?乍一看,对我来说似乎还可以,但对于一个技术对象来说,使用getter/setter方法来添加工具真的合适吗?

如果
技术
需要
工具
对象,那么保留它们没有什么错

如果
工具
技术
对象状态相关,则应将它们包含在工具中

相关的可能方法:

public addTool(Tool $tool);
public removeTool(Tool $tool);
public removeToolByIndex($index);
public getToolFromIndex($index);

如果
技术
需要
工具
对象,保留它们没有什么错

如果
工具
技术
对象状态相关,则应将它们包含在工具中

相关的可能方法:

public addTool(Tool $tool);
public removeTool(Tool $tool);
public removeToolByIndex($index);
public getToolFromIndex($index);

我绝对不是OOP方面的专家,但我想说这取决于你的领域。如果你的领域处理的是技术,而技术又处理工具,那么我认为在技术中有一个工具阵列是可以的。我要做的是让每个工具对象都符合相同的接口,这样任何技术都可以有任何工具

另外,从我所学到的,我认为当您跨越层(域、持久性、服务层…)时,解耦是很重要的。因此,一个层不应该依赖于另一个层,但域对象可以相互依赖

如果您正在对一个实体依赖于另一个实体的域进行建模,那么对象也将依赖于另一个实体(如果一项技术没有工具是无用的,那么它依赖于它们)。以普通的汽车/车轮/驾驶员为例,汽车在现实世界中既依赖于车轮又依赖于驾驶员,因此对象也将依赖于车轮,因为如果缺少这些对象,它们将完全无用


我希望这是有道理的,如果我弄错了,那么我也会从别人那里学到一些东西。

我绝对不是OOP方面的专家,但我想说这取决于你的领域。如果你的领域处理的是技术,而技术又处理工具,那么我认为在技术中有一个工具阵列是可以的。我要做的是让每个工具对象都符合相同的接口,这样任何技术都可以有任何工具

另外,从我所学到的,我认为当您跨越层(域、持久性、服务层…)时,解耦是很重要的。因此,一个层不应该依赖于另一个层,但域对象可以相互依赖

如果您正在对一个实体依赖于另一个实体的域进行建模,那么对象也将依赖于另一个实体(如果一项技术没有工具是无用的,那么它依赖于它们)。以普通的汽车/车轮/驾驶员为例,汽车在现实世界中既依赖于车轮又依赖于驾驶员,因此对象也将依赖于车轮,因为如果缺少这些对象,它们将完全无用


我希望这是有意义的,如果我弄错了,那么我也会从其他人那里学到一些东西。

您给出的示例是多对多关联,所以最好使用3d对象来表示关联。如果它在路上(一对多),那么你提到的简单的构图就可以了

下面是示例代码,代码中可以做的一些改进 1.为工具和技术实现getter setter,并使变量私有化。 2.对于工具和技术,直接使用接口而不是类。 3.在pair类中使用了两个不同的索引(数组)来提高get函数的性能,若性能不重要,那个么u可以只使用一个数组

<?php
class Technology{
    public  $id;
    public  $title;

    public function __construct($id, $title){
        $this->id = $id;
        $this->title = $title;
    }
}

class Tool{
    public $id;
    public $title;

    public function __construct($id, $title){
        $this->id = $id;
        $this->title = $title;
    }
}

class TechnologyToolPair{
    private $techIndex = array();
    private $toolIndex = array();

    //Index by id, you can replace title if u maily search by title
    public function addPair($tech, $tool){
        $this->techIndex[$tech->id]['technology'] = $tech;
        $this->techIndex[$tech->id]['tool'][] = $tool;

        $this->toolIndex[$tool->id]['tool'] = $tool;
        $this->toolIndex[$tool->id]['technology'][] = $tech;
    }

    public function getByTechnologyId($id){
        return $this->techIndex[$id];
    }

    public function getByToolId($id){
        return $this->toolIndex[$id];
    }

    public function getByTechnologyName($name){
        foreach($this->techIndex as $index => $value){
            if(!strcmp($name, $value['technology']->title)){
                return $value;
            }
        }
    }
}


$tech1 = new Technology(1, 'php');
$tech2 = new Technology(2, 'java');

$tool1 = new Tool(1, 'eclipse');
$tool2 = new Tool(2, 'apache');
$tool3 = new Tool(3, 'tomcat');

$pair = new TechnologyToolPair();
$pair->addPair($tech1, $tool1);
$pair->addPair($tech1, $tool2);
$pair->addPair($tech2, $tool1);
$pair->addPair($tech2, $tool3);

var_dump($pair->getByToolId(1));
var_dump($pair->getByTechnologyId(2));
var_dump($pair->getByTechnologyName('java'));

您给出的示例是多对多关联,所以最好使用3d对象来表示关联。如果它在路上(一对多),那么你提到的简单的构图就可以了

下面是示例代码,代码中可以做的一些改进 1.为工具和技术实现getter setter,并使变量私有化。 2.对于工具和技术,直接使用接口而不是类。 3.在pair类中使用了两个不同的索引(数组)来提高get函数的性能,若性能不重要,那个么u可以只使用一个数组

<?php
class Technology{
    public  $id;
    public  $title;

    public function __construct($id, $title){
        $this->id = $id;
        $this->title = $title;
    }
}

class Tool{
    public $id;
    public $title;

    public function __construct($id, $title){
        $this->id = $id;
        $this->title = $title;
    }
}

class TechnologyToolPair{
    private $techIndex = array();
    private $toolIndex = array();

    //Index by id, you can replace title if u maily search by title
    public function addPair($tech, $tool){
        $this->techIndex[$tech->id]['technology'] = $tech;
        $this->techIndex[$tech->id]['tool'][] = $tool;

        $this->toolIndex[$tool->id]['tool'] = $tool;
        $this->toolIndex[$tool->id]['technology'][] = $tech;
    }

    public function getByTechnologyId($id){
        return $this->techIndex[$id];
    }

    public function getByToolId($id){
        return $this->toolIndex[$id];
    }

    public function getByTechnologyName($name){
        foreach($this->techIndex as $index => $value){
            if(!strcmp($name, $value['technology']->title)){
                return $value;
            }
        }
    }
}


$tech1 = new Technology(1, 'php');
$tech2 = new Technology(2, 'java');

$tool1 = new Tool(1, 'eclipse');
$tool2 = new Tool(2, 'apache');
$tool3 = new Tool(3, 'tomcat');

$pair = new TechnologyToolPair();
$pair->addPair($tech1, $tool1);
$pair->addPair($tech1, $tool2);
$pair->addPair($tech2, $tool1);
$pair->addPair($tech2, $tool3);

var_dump($pair->getByToolId(1));
var_dump($pair->getByTechnologyId(2));
var_dump($pair->getByTechnologyName('java'));

您应该查看工具和技术之间的行为和交互,而不仅仅是数据。这决定了如何在OOP中建模。您目前正在进行数据建模,面向对象应该提供更好的模块化。您可能希望通过测试来驱动设计,展示如何使用它们。

您应该查看工具和技术之间的行为和交互,而不仅仅是数据。这决定了如何在OOP中建模。您目前正在进行数据建模,面向对象应该提供更好的模块化。您可能希望通过测试来驱动设计,展示它们的使用方式。

工具肯定是相关的,但不是对象始终需要的。有时一个操作可能要求技术拥有工具,而其他时候则不希望将它们关联起来,因为我可能真的很想使用该技术,而不想将工具加载到技术对象中。工具肯定是相关的,但对象始终不需要。有时,一项操作可能要求技术具有工具,而其他时候则不希望将它们关联起来,因为我可能只是真的想使用该技术,而不想将工具加载到技术对象中。请参阅下面我的评论。。。我不知道这是否有区别。@IOInterrupt:如果