简体   繁体   English

How to do train composition - 类的组合

[英]How to do train composition - compositon of classes

I am currently working on a train simulation project for uni.我目前正在为大学做一个火车模拟项目。

This is my class hierarchy:这是我的 class 层次结构:

RollingStock
    Coach
        FreightCoach
        PassengerCoach
        SpecialCoach
    Engine
        DieselEngine
        ElectricEngine
        SteamEngine
    Trainset

My questions:我的问题:

  • Every coach has a unique ID.每个教练都有一个唯一的 ID。 However, Engines and Trainsets share their ID-Space ("series-name").但是,Engines 和 Trainsets 共享它们的 ID 空间(“系列名称”)。 "Name" is inherited by RollingStock and both Trainset and Engine have the attribute "series". “名称”由RollingStock继承, TrainsetEngine都具有属性“系列”。

I've created a class "SharedIdSpace" to implement this feature.我创建了一个 class “SharedIdSpace”来实现这个功能。 But I am not quite sure how to solve this nicely (TreeMap, ..., ?).但我不太确定如何很好地解决这个问题(TreeMap,...,?)。

Now, my main problem is I have to implement the following feature:现在,我的主要问题是我必须实现以下功能:

" Rolling stock can be composed into a train. The following restrictions must be observed when composing: "机车车辆可以组合成火车。组合时必须遵守以下限制:

  • There must always be at least one locomotive/train set at the beginning or end of a valid train.在有效列车的起点或终点必须始终至少有一个机车/列车组。
  • When composing, it must always be considered whether the rolling stock has a suitable coupling at the desired composition point.组合时,必须始终考虑机车车辆在所需组合点是否具有合适的联轴器。
  • The rolling stock that is being composed has not yet been used in another train.正在组合的机车车辆尚未在另一列火车上使用。 [...] " [...]

How can I implement this?我该如何实施? I'm afraid I have no useful idea.恐怕我没有什么有用的主意。

Im not sure about what are you asking, but I will try to give you my aproximation:我不确定你在问什么,但我会尽量给你我的近似值:

You need to create a composite object where every object has an unique ID and need to pass some validations.您需要创建一个组合 object,其中每个 object 都有一个唯一的 ID,并且需要通过一些验证。

I would implement an "StoregeManager" a "ComposerManager" and add an abstract validator method in "RollingStock" that validate if the vagon can be added.我将实现一个“StoregeManager”和一个“ComposerManager”,并在“RollingStock”中添加一个抽象验证器方法来验证是否可以添加 vagon。

The flow would be something like this:流程将是这样的:

DISCLAIMER This is written in Java, but with notepad++, please dont check the sintaxis. DISCLAIMER这是在Java写的,但是用notepad++,请不要检查sintaxis。


RollingStock freightCoach = StoregeManager.getFreightCoach();
RollingStock specialCoach = StoregeManager.getSpecialCoach();
RollingStock dieselEngine = StoregeManager.getDieselEngine();

// Check if they are null or throw an exception if has no more elements. Maybe from BBDD or from where you want

Composer.compone()
.add(dieselEngine)
.add(freightCoach)
.add(specialCoach)
.build()

And inside the componer, something like this:在 componer 内部,是这样的:


public class Composer {

    private StoregeManager storeManager; //Injected or initialized, as you want.
    private static Train train;

    public Composer build(){
        train = new Train;
        return this;
    }

    public Composer add(RollingStock rs) {
        if(rs.isValid(train))
            train.add(rs);
        return this;
    }

    public RollingStock[] build() {
        storageManager.ckeckTrain(train);
        return train;
    }
}

You can put the storage inside the Composer and pass as argument the classname of the vagon if you need another aproximation to your problem.如果您需要另一个近似值来解决您的问题,您可以将存储放在 Composer 中并将 vagon 的类名作为参数传递。

I hope this helps you.我希望这可以帮助你。

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

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