如何避免吸气剂并避免对 UI 进行硬编码?
我想将战士的描述打印到控制台,其中包括战士的力量和战士的武器,格式为 This
例如:这个强壮的战士使用黄油刀
。
为清楚起见进行编辑:我想在不使用 getter 或任何其他揭示对象内部实现的方法(如 toString)的情况下向对象询问数据的情况下执行此操作。我还想在不将当前 UI(控制台)硬编码到对象本身中的情况下执行此操作。
public class Warrior
{
private String description;
private Weapon weapon;
public Room(String description, Weapon weapon)
{
this.description = description;
this.weapon = weapon
}
}
public class Weapon
{
private String name;
public Weapon(String name)
{
this.name = name;
}
}
避免 Getters
我可以通过硬编码 UI 来避免 getters:
//Warrior class
public void display()
{
String.out.println("This " + description + " warrior uses a ");
weapon.display();
}
//Weapon class
public void display()
{
String.out.print(name);
}
避免硬编码 UI
我可以通过使用 getters 来避免硬编码 UI:
//Warrior class
public String getDescription()
{
return "This " + description + " warrior uses a " + weapon.getName();
}
//Weapon class
public String getName()
{
return name;
}
是否可以同时避免两者?在上面的示例中我该如何执行此操作?
注意:针对一些最初的答案,getter 不是遵循命名约定 getSomeFieldName
的方法。因此,将 getSomeFieldName
重命名为 aMethodThatIsNotPrefixedByGet
并不是解决方案。 getter 是一种将私有数据从对象传递到调用它的作用域的方法。
完全清楚的是,我在这里试图处理的问题与数据封装有关(正如这个问题所标记的那样)。如何防止将数据传递给不需要知道该数据的对象并仍然避免对 UI 进行硬编码?
此外,基于这些 问题,我认为 toString 不应该按照许多答案所建议的方式使用。 toString 似乎是用于生成对象的文本表示形式以进行调试等,而不是用于返回任意输出,尤其是不用于返回依赖于应用程序的输出。
I want to print a description of a warrior to the console that will include the warrior's strength and the warrior's weapon in the form This <description> warrior uses a <weapon>
For example: This strong warrior uses a butter knife
.
Edit for clarity: I want to do this without asking objects for data by using getters or any other method (like toString) which reveals the internal implementation of an object. I also want to do this without hard coding my current UI (a console) into the objects themselves.
public class Warrior
{
private String description;
private Weapon weapon;
public Room(String description, Weapon weapon)
{
this.description = description;
this.weapon = weapon
}
}
public class Weapon
{
private String name;
public Weapon(String name)
{
this.name = name;
}
}
Avoiding Getters
I can avoid getters by hard coding the UI:
//Warrior class
public void display()
{
String.out.println("This " + description + " warrior uses a ");
weapon.display();
}
//Weapon class
public void display()
{
String.out.print(name);
}
Avoiding hard coded UI
I can avoid a hard coded UI by using getters:
//Warrior class
public String getDescription()
{
return "This " + description + " warrior uses a " + weapon.getName();
}
//Weapon class
public String getName()
{
return name;
}
Is it possible to avoid both? How can I do so in the above example?
Note: In response to some initial answers, a getter is not a method that follows the naming convention getSomeFieldName
. Therefore, renaming getSomeFieldName
to aMethodThatIsNotPrefixedByGet
is not a solution. A getter is a method that passes private data from an object to the scope which called it.
To be completely clear, the issue I am trying to deal with here is to do with data encapsulation (as this question is tagged). How can I prevent passing data to objects which do not need to know that data and still avoid hard coding the UI?
Additionally, based on these questions, I don't think toString should be used in the way that it has been suggested by the many of the answers. toString seems to be for generating a text representation of an object for debugging and so forth, not for returning arbitrary output and especially not for returning application dependent output.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
是的,选择 i18n,
并且
Yes go for i18n,
and
我会重写 Warrior 和 Weapon 中的 toString() 方法,因为该方法自然会返回对象的 String 表示形式。然后,我将创建一个专门用于创建描述的类,例如
DescriptionMaker
,并在其中创建一个方法:然后可以将该方法的返回值打印到控制台。此外,内部化可以应用在
DescriptionMaker
类中。I would override the method toString() in both the Warrior and the Weapon, as this method naturally returns a String represenation of an object. Then I would create a class dedicated to creating the descriptions, for example a
DescriptionMaker
, and create a method in it:The return value of this method could then be printed to the console. Also, internalization could be applied in the
DescriptionMaker
class.在这种情况下,getter 对我来说似乎是一个很好的做法,因为它们允许您将数据(战士和武器类)与 UI(调用 getter 并创建描述字符串、小部件、html 代码等)分开。但是,我不会让 Warrior getter 创建字符串,它只会返回战士描述,UI 类将创建字符串(您建议在示例中执行的
Warrior.getDescription()
方法)。In that case getters seem a good practice to me as they permit you to have your data (Warrior and Weapon classes) separate from your UI (that calls the getters and create the description strings, widgets, html code, etc.). However I wouldn't let the Warrior getter create the string, it would just return the warrior description and the UI class would create the string (what the
Warrior.getDescription()
method you propose do in you sample).您可以在 Warrior 类中重写 toString 来实现它。
重写 toString inWeapon 以返回名称;
as
并且可以直接打印为
YOu can override toString in your Warrior class to achieve it.
override toString inWeapon to return the name;
as
and you can directly print as
将两者结合起来怎么样:
How about combining both: