简体   繁体   English

用Java实现创建者类和类本身设计的正确方法是什么?

[英]What is the correct way to implement a Creator Class and the Class itself design in Java?

I am trying to decide on a design for all my classes and their creators. 我正在尝试为我所有的班级及其创建者设计一个设计。

For example I have 2 classes, Level and LevelLoader : 例如,我有两个类, LevelLevelLoader

The design I have come so far is: 到目前为止,我设计的是:

This is Level class: 这是Level类:

public class Level implements Serializable {

    private byte[] map; 

    Level(LevelLoader loader){
        map = loader.getMap();
    }

    // ...
}

This is the Creator: 这是创建者:

public interface LevelLoader {

    public Level loadLevel(InputStream stream);

    public byte[] getMap();
}

So for example to create a level instance in main file I'll write: 因此,例如,我将在文件中创建一个关卡实例:

TextLoader is implementing LevelLoader interface above: TextLoader正在实现上面的LevelLoader接口:

File file = new File("src/Level/level2.txt");
Level level1 = new Level(new TextLoader(file));

So I am curious what is the most logical way to connect them to the other? 所以我很好奇将它们彼此连接的最合乎逻辑的方法是什么?

certainly you will have more implementations in the near future. 当然,在不久的将来您将有更多的实现。 So without directly creating instances like TextLoader, get them using a Factory. 因此,无需直接创建诸如TextLoader之类的实例,就可以使用Factory来获取它们。

Sorry I quite not get what question is about? 对不起,我还不太了解什么问题?

perhaps: - you need to create many instances of your Level class during application run time - also you may need different kind of loaders (TextLoader, FileLoader, URLLoader and so on..) 也许:-您需要在应用程序运行期间创建Level类的许多实例-您可能还需要其他类型的加载器(TextLoader,FileLoader,URLLoader等)。

Then you can create each loader once somewhere outside (ie Utiltiy or ObjectFactory class) and then reuse them to create instances. 然后,您可以一次在外部某个地方(即Utiltiy或ObjectFactory类)创建每个加载器,然后重用它们来创建实例。

something like that: 像这样的东西:

ObjectFactory class ObjectFactory类

private static LevelLoader filetLoaderInstance = new FileLoader("level2.txt");
private static LevelLoader urlLoaderInstance = new URLLoader("ftp://foo.com/level2.txt");

public static Level createLevelFromFile()
{
   return new Level(textLoaderInstance);
}

public static Level createLevelFromURL()
{
   return new Level(urlLoaderInstance);
}

In general without necessity of multiple usage such design is not needed. 通常,不需要多次使用就不需要这种设计。 Like in your example with 就像在你的例子中

Level level1 = new Level(new TextLoader(file));

It is quite overhead. 这是相当大的开销。 What is the reason to have and create another object (TextLoader) if it will run only once? 创建另一个对象(TextLoader)仅运行一次的原因是什么?

But if that code has to run many times - it is not good. 但是,如果该代码必须运行很多次,那就不好了。 It is better to have only one instance of TextLoader rather than create it every time when instance of Loader class creates. 最好只有一个TextLoader实例,而不是每次创建Loader类的实例时都创建它。 It affects memory usage as well as performance of application. 它影响内存使用以及应用程序的性能。

What I feel is the below way. 我的感觉是下面的方式。

InputStream is = new File("src/Level/level2.txt");
LevelLoader levelLoader = new TextLoader();
Level level = levelLoader.loadLevel(file);

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

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