简体   繁体   English

代码样式:“隐藏”数据类型(Java)

[英]Code style: “hiding” data types (Java)

I'm working on a set of classes to represent musical notes, bars, rhythms and so on. 我正在研究一组课程来表示音符,小节,节奏等。 Naturally, I'll have to deal with time signatures that are best represented by a fraction (like 4/4, 3/4 and so on). 当然,我将不得不处理最好用分数表示的时间签名(如4 / 4,3 / 4等)。

I'm wondering what is better style. 我想知道什么是更好的风格。 Having a constructor for a Bar contain a Fraction-object or just two ints as a time signature. 使用Bar的构造函数包含Fraction-object或仅包含两个int作为时间签名。

public Bar(Fraction timeSignature) {
    this.timeSignature = timeSignature;
}

or: 要么:

public Bar (int num, int den) {
    timeSignature = new Fraction(num, den);
}

I mean… i could have both but which one should I go for in my application? 我的意思是......我可以同时拥有这两个但我应该在申请中选择哪一个? Is there a "better" style? 是否有“更好”的风格?

Thank you for your thoughts! 谢谢你的想法!

Personally, I'd go w/ the first approach. 就个人而言,我会选择第一种方法。 If you know you need/want a Fraction object, why have the Bar not just take that? 如果你知道你需要/想要一个Fraction对象,为什么Bar不仅仅是那个? The person invoking the Bar class will need to understand the Fraction either way (to pass you the ints), so it's just cleaner. 调用Bar类的人需要以任何方式理解Fraction(向你传递整数),所以它更干净。

I'd use the first one and provide a factory method in the fraction class to be able to call something like: 我将使用第一个并在fraction类中提供一个工厂方法,以便能够调用类似于:

new Bar(Fraction.of(4, 4));

best of both world ;-) 两全其美;-)

First, simple APIs are nice, yes, but if you expand the constructor later to take more parameters (say, BPM), using primitives rather than objects makes the calling code harder to understand. 首先,简单的API很好,是的,但是如果稍后扩展构造函数以获取更多参数(比如BPM),则使用基元而不是对象会使调用代码更难理解。 ie: 即:

Bar myBar = new Bar(4, 4) // Okay
Bar myBar = new Bar(4, 4, 120) // ??
Bar myBar = new Bar(new TimeSignature(4, 4), 120) // Much clearer

When in doubt, explicit is better. 如果有疑问,明确表示更好。

Secondly, I would say don't store the time signature in a Fraction object -- while they are often written as though they are fractions, time signatures are not. 其次,我想说不要将时间签名存储在Fraction对象中 - 虽然它们通常被写成分数,但时间签名却不是。 3/4 and 6/8 are equivalent as fractions, but are different time signatures that produce a different sound as a result. 3/4和6/8相当于分数,但是不同的时间签名会产生不同的声音。 TimeSignature is within your program's domain, it warrants a custom type. TimeSignature属于您的程序域,它保证自定义类型。

You may want to consider making time signature an enum in order to prevent user input error. 您可能需要考虑将时间签名设置为枚举,以防止用户输入错误。 Since you may have a finite set of time signatures(2/4, 6/8, 4/4) you can control the time signatures a user creates by doing the following: 由于您可能拥有一组有限的时间签名(2 / 4,6 / 8,4 / 4),因此您可以通过执行以下操作来控制用户创建的时间签名:

//example
new Bar(TimeSignature.DUPLE_2_2)

public enum TimeSignature{

DUPLE_2_2(2,2), QUADRUPLE_2_4(2,4), TRIPLE_3_4(3,4);

private final int numerator;
private final int denominator;

private TimeSignature(int numerator, int denominator) {
this.numerator = numerator;
this.denominator = denominator;
}


}

Good Luck 祝好运

As with every design question, this depends. 与每个设计问题一样,这取决于。 In this case, you should consider how your class is going to be used. 在这种情况下,您应该考虑如何使用您的课程。

In general, I prefer simpler APIs. 一般来说,我更喜欢更简单的API。 Compare this: 比较一下:

Bar myBar = new Bar(4, 4);

to this: 对此:

Bar myBar = new Bar(new Fraction(4,4));

The latter alternative is more explicit (which is good), but might become tedious if you instantiate a lot of bars with literal arguments. 后一种选择更明确(这很好),但如果用文字参数实例化很多条形,可能会变得乏味。 On the other hand, if your Code needs to handle Fractions anyway, the first syntax might be better. 另一方面,如果你的代码需要处理分数,第一种语法可能会更好。

Another aspect to consider is the overhead of introducing the new class Fraction . 另一个需要考虑的方面是引入新类Fraction的开销。 Creating an additional class just to store two int s means a lot of overhead for little use. 创建一个额外的类只是为了存储两个int意味着很多开销很少使用。 However, if Fraction provides other services as well (such as simplification of fractions), the class earns its right to exist. 但是,如果Fraction提供其他服务(例如分数的简化),则该类获得其存在的权利。

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

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