每个类的职责以及它们如何在UML中彼此交互 [英] responsibility of each class and how they interact each other in UML

查看:97
本文介绍了每个类的职责以及它们如何在UML中彼此交互的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为我的项目管理软件绘制一个类图,描述以下情形. 它包含

I'm trying to draw an class diagram for my project management software describing the following scenario. It contains

  • 项目
  • 经理
  • 员工

经理可以创建项目
经理可以更改项目截止日期
经理可以更改项目名称
经理可以将一名员工分配给项目(单个项目只有一名分配的员工)
员工可以提交项目

Manager can create project
manager can change project deadline
manager can change project name
manager can assign a employee To Project (single project have only one assigned employee)
employee can submit project

为上述要求创建了此类图

For the above requirements created this Class diagram

并如下所示使用php在代码中实现

and implement it in code using php as shown below

Class Manager {
private $Id;
private $name;
private $selectedProject;

public function __construct($name){
    $this->$name = $name;        
}

//manager create a project
    public function createProject(){            
        $project       = new Project('Web Dev','incomplete','2018/6/18');
        $this->selectedProject = $project;
    }

//manager can change project deadline
    public function updateProjectDeadline($projectDeadline){
        $this->selectedProject->SetProjectDeadline($projectDeadline);
    }

//manager can change project name
    public function updateProjectName($projectName){
        $this->selectedProject->SetProjectDeadline($projectName);
    }

//manager can assign a employee To Project
    public function assignEmployeeToProject(Employee $employee){
        $this->employee = $employee;
        $this->selectedProject->SetProjectEmployee($this->employee);
    }

}


Class Project {
    private $Id;
    private $projectName
;
    private $projectStatus
;
    private $deadline
;
    private $assignedEmployee;

    public function __construct($projectName
,$projectStatus
,$deadline
){
        $this->$projectName
 = $projectName
;
        $this->$projectStatus
 = $projectStatus
;
        $this->$deadline
 = $deadline
;
    }

    public function SetProjectDeadline($deadline
)
{
        $this->$deadline
 = $deadline
;    
    }
    public function SetProjectEmployee(Employee $employee)
{
        $this->$assignedEmployee = $employee;
    }
    public function setProjectStatus($projectStatus
)
{
        $this->$projectStatus
 = $projectStatus
;    
    }
    public function setProjectName($projectName
)
{
        $this->$projectName
 = $projectName
;    
    }
}


Class Employee {
    private $empName;
    private $assignedProject;

    public function __construct($empName){
        $this->$empName = $empName;
    }

    //employee can submit project
    public function submitProject(Project $project){
        $this->assignedProject = $project;
        $this->assignedProject->setProjectStatus('submit');
    }
}

我想知道
我的班级图正确吗?
我的实现正确吗?

I want to know
Is my class diagram correct ?
Is my implementation correct ?

以下方法的特殊实现涵盖了良好的OO设计?

specially implementation of below methods cover good OO Design ?

经理更改项目截止日期-updateProjectDeadline()
经理更改项目名称-updateProjectName()

manager changing the project deadline - updateProjectDeadline()
manager changing project name - updateProjectName()

我感到代码有异味,因为在下面仅完成的工作就调用了另一个类的设置器....

i feel code smell because in above below only thing done there is calling a setter of another class ....

P.S-另一个不好的做法是,类访问另一个setters,getters(从另一个类中调用一个setter,getter方法)?

P.S - another one is it bad practice class access another class setters,getters (call setter ,getter methods of one from another class) ??

推荐答案

在类图中,跳出来的两件事是您已经填充了箭头(在UML类图中没有意义-它们要么需要打开的箭头或未填充的闭合箭头(取决于含义)-在下图中,我添加了 dependency (虚线箭头)和 basic association (实体箭头),这可能就是您的意思.另外,中间的菱形-这不是UML类图元素.是纸条吗?

In your class diagram the two things that jump out are that you have filled arrow heads (these have no meaning in UML class diagrams -- they either need to be open arrows or a closed arrow that is not filled depending on meaning -- in the diagram below I've added a dependency (dash line arrow) and a basic association (solid line arrow) which could be what you mean). In addition, the diamond in the middle -- this is not a UML class diagram element. Is it a note??

就对象定向而言,我想知道的一般原理是封装,抽象,多态等.通常,访问器(getter)和变异器(setter)方法是OO原理被破坏的标志,因为它们倾向于表示另一个类正在操纵(或被鼓励操纵)该类的内部(即破坏封装). SetEmployee可能更多地是关于要求Project分配一个-诸如Project.Allocate(Employee)或Employee.AllocateTo(Project)之类的东西可能更有意义,并且不太可能鼓励破坏封装.

In terms of Object Orientation -- the general principles I assume you know -- encapsulation, abstraction, polymorphism etc. Typically accessor (getter) and mutator (setter) methods are a sign of OO principles being broken, as they tend to mean that another class is manipulating (or is being encouraged to manipulate) the internals of the class in question (i.e. breaks encapsulation). SetEmployee is probably more about asking the Project to allocate one -- so something like Project.Allocate (Employee) or Employee.AllocateTo (Project) might be more meaningful and less likely to encourage encapsulation to be broken.

这篇关于每个类的职责以及它们如何在UML中彼此交互的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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