简体   繁体   English

在类之间移动对象 (Java)

[英]Move objects between classes (Java)

I have a problem like this:我有这样的问题:

A task board illustrates the progress that an Agile team is making in achieving their sprint goals.任务板说明了敏捷团队在实现其 sprint 目标方面取得的进展。 A task boards usually has a few simple things:任务板通常有一些简单的东西:

5 basic columns (there can be more) 5个基本列(可以有更多)

Stories故事

To Do (Planned)待办事项(计划)

In Progress (In process, WIP, etc.)进行中(进行中、WIP 等)

In Review审查中

Complete (Completed, Done, etc.)完成(完成、完成等)

A Row for each story每个故事一行

The cards that are present on the taskboard can be either a story card or a task card.任务板上的卡片可以是故事卡或任务卡。

A story card always has the 'yellow' color and has a title, short description of the story and estimation in story points.故事卡总是有“黄色”颜色,并有标题、故事的简短描述和故事点的估计。 This card can be present only on the 'Stories' column.此卡只能出现在“故事”栏中。

The task card has a title, detailed description of the task (acceptance criteria), estimation, color ('green' - normal, 'red' - blocked) and multiple tags (name of the developer working on the task, name of the developer reviewing the task, reason of the blocking state, etc.).任务卡片有标题、任务的详细描述(接受标准)、估计、颜色(“绿色”-正常、“红色”-阻止)和多个标签(从事该任务的开发人员姓名、开发人员姓名)查看任务、阻塞状态的原因等)。 This type of card can be moved between columns (other than the 'Stories' column where it cannot be present).这种类型的卡片可以在列之间移动(除了不能出现的“故事”列)。

1.Identify the objects from the text,the classes should have atributtes and methods that should be extracted from the text, and create a main class in which you instantiate all the defined classes.. 1.从文本中识别对象,类应该具有从文本中提取的属性和方法,并创建一个主类,在其中实例化所有定义的类。

I am a beginner.我是初学者。 I think that the objects are: the story and task and I need to move tasks between classes ..I tried to solve the problem like this:我认为对象是:故事和任务,我需要在类之间移动任务..我试图解决这样的问题:

I made a UserStory class:我做了一个 UserStory 类:

public class UserStory {
private String title;
private String description;
private int storyPoints;
public Task task;

public UserStory(){}

public void setTitle(String title) {
    this.title = title;
}

public void setDescription(String description) {
    this.description = description;
}

public void setStoryPoints(int storyPoints) {
    this.storyPoints = storyPoints;
}

public String getTitle() {
    return title;
}



  public String getDescription() {
        return description;
    }

    public int getStoryPoints() {
        return storyPoints;
    }
}

and another 5 classes:first class is Task and another 4 classes who inherit the Task class: Planned, InProgress, InReview and Done.还有5个类:第一个类是Task,另外4个类继承了Task类:Planned、InProgress、InReview和Done。 And for example I want to move a task from Planned to InProgress..These 2 classes looks like this:例如,我想将一个任务从 Planned 移动到 InProgress..This 2 classes 看起来像这样:

First I have the Task superclass首先我有 Task 超类

    public class Task {
    public String title;
    public String color;
    public String description;
    public String workingDev;
    public String revDev;

    public Task(String title, String color, String description, String workingDev, String revDev) {
        this.title = title;
        this.color = color;
        this.description = description;
        this.workingDev = workingDev;
        this.revDev = revDev;
    }
    public Task(){};
     //getters and setters

} }

Planned class:计划班级:

public class Planned extends Task {

public Planned(String title, String color, String description, String workingDev, String revDev) {
    super(title, color, description, workingDev, revDev);
}

public Planned() {
}

public void moveInProgress(InProgress inProgress) {
    inProgress.InProgress();
}

public void InPlanned() {
    System.out.println("Task moved in Planned");
}

} }

InProgress class: InProgress 类:

public class InProgress extends Task {

public InProgress(String title, String color, String description, String workingDev, String revDev) {
    super(title, color, description, workingDev, revDev);
}

public InProgress() {
}

public void InProgress() {
    System.out.println("Task moved InProgress");
}

public void moveInPlanned(Planned planned){
    planned.InPlanned();
}

} }

And the main class:和主类:

public class Hello {
public static void main(String[] args) {
    Planned pln = new Planned();
    InProgress prog = new InProgress();

    pln.moveInProgress(prog);
}

} }

In my opinion, that's how I understood the requirement,I'm right?.. and I don't know what is the bond between the UserStory class and the Task class.在我看来,这就是我对需求的理解,对吗?..我不知道 UserStory 类和 Task 类之间的联系是什么。 a story strcuture can be something like this: As a [type of user], I want to [perform some task] so that I can [achieve some goal].故事结构可以是这样的:作为[类型的用户],我想[执行某些任务]以便我可以[实现某个目标]。 Any advice or hints would be welcome.欢迎任何建议或提示。 If I was not clear please tell me, thank you如果我不清楚,请告诉我,谢谢

"and another 4 classes who inherit the Task class: Planned, InProgress, InReview and Done". “以及另外 4 个继承 Task 类的类:Planned、InProgress、InReview 和 Done”。

That is a classical mistake many beginners make.这是许多初学者犯的一个经典错误。 "With a hammer, everything looks like a nail." “有了锤子,一切看起来都像钉子。” Your hammer is inheritance / polymorphism and you apply it generously to every new problem.你的锤子是继承/多态,你慷慨地将它应用到每一个新问题上。 Don't worry, we all have been there.别担心,我们都去过那里。

The better approach is to prefer composition over inheritance .更好的方法是更喜欢组合而不是继承

Cards don't really need to know if they are in progress, done or planned or else.卡片并不真正需要知道它们是在进行中、已完成还是计划中或其他。 You already see that because you are struggling to create meaningful methods on them.您已经看到了这一点,因为您正在努力为它们创建有意义的方法。 They inherit from Task, but then what?它们从 Task 继承,但是呢?

Personally, I'd go with a Board class, which has Columns (as in, a member private List<Column> columns ).就个人而言,我会使用Board类,它有Columns (如成员private List<Column> columns )。

And I'd only use one Card class which can move between those columns.而且我只使用一个可以在这些列之间移动的Card类。 With the above setup, its trivial to write a method on Board in the form of public void Move (Card card, Column from, Column to) .有了上面的设置,在Board上写一个方法很简单,以public void Move (Card card, Column from, Column to) You can even skip the from and have the method only be public void move(Card card, Column to) .您甚至可以跳过from并使方法仅为public void move(Card card, Column to) In that case, you need to first find the column the card is in, which is straight-forward.在这种情况下,您需要首先找到卡片所在的列,这很简单。

Now, you still need to restrict movement of the cards, so that user stories are somewhat special.现在,您仍然需要限制卡片的移动,以便用户故事有些特殊。

You can employ inheritance here.可以在这里使用继承。 Eg by having two classes Task extends Card and Story extends Card .例如,通过有两个类Task extends CardStory extends Card You could then make the Column class generic: Column<C extends Card> .然后,您可以使Column类通用: Column<C extends Card>

With that, you can have a column either accept any card or any story.就这样,你可以有一列要么接受任何卡或任何故事。

If you really want to, you can also create an enum to track the progress of the cards, but that isn't good from an extensibility PoV.如果您真的愿意,您还可以创建一个枚举来跟踪卡片的进度,但这对于可扩展性 PoV 来说并不好。 What if you want to add more status?如果您想添加更多状态怎么办?

So, in summary:所以,总结一下:

class Board {
    // 5 basic columns (there can be more)
    List<Column<Task>> columns = new ArrayList<>(5);
    
    Column<Story> stories = new Column<>("Stories");
    

    public Board() {
        var planned = new Column<Task>("To Do (Planned)");
        columns.add(planned);
        // etc.
    }

    public void move(Task card, Column<Task> from, Column<Task> to) {
        from.remove(card);
        to.add(card);
        // if you want to track state in the card, set the new state here
    }

    public void addStory(Story story) {}

    public void removeStory(Story story) {}

    public List<Columns<Task>> getColumns() {}

    public void getColumnByName(String name) {
       // implement using stream/filter/collect/findFirst/findAny
    }

    public void addColumn(Column column) {}
}

public class Card {  
    // ... 
}

public class Task extends Card {
    //
}

public class Story extends Card {
    //
}

public class Column<T extends Card> {
    private List<T> cards = new ArrayList();

    // fields like name/title etc
    // methods add/remove etc.
}

With this setup, you get quite a nice Kanban-Board thingy going.通过这种设置,你会得到一个很好的看板。 I've omitted all method implementations, but they are stright-forward to do.我省略了所有方法的实现,但它们是直接要做的。

You can also opt to make columns accept both Columns with Task and Stories ( private List<Column ? extends Card>> ), but if you are not comfortable with using generics in this way, you do not need to.您还可以选择让columns接受带有任务和故事的列( private List<Column ? extends Card>> ),但如果您不习惯以这种方式使用泛型,则不需要这样做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM