如何避免吸气剂并避免对 UI 进行硬编码?

发布于 2024-10-31 15:28:10 字数 1918 浏览 5 评论 0原文

我想将战士的描述打印到控制台,其中包括战士的力量和战士的武器,格式为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

失去的东西太少 2024-11-07 15:28:10

是的,选择 i18n

messages.properties
displayMessage = This {0} warrior uses a {1}

messages.properties_en_US
displayMessage = This {0} warrior uses a {1}

并且

public static String getString(String key, Object... params  ) {
        try {
            return MessageFormat.format(RESOURCE_BUNDLE.getString(key), params);
        } catch (MissingResourceException e) {
            return '!' + key + '!';
        }
}

Yes go for i18n,

messages.properties
displayMessage = This {0} warrior uses a {1}

messages.properties_en_US
displayMessage = This {0} warrior uses a {1}

and

public static String getString(String key, Object... params  ) {
        try {
            return MessageFormat.format(RESOURCE_BUNDLE.getString(key), params);
        } catch (MissingResourceException e) {
            return '!' + key + '!';
        }
}
逆夏时光 2024-11-07 15:28:10

我会重写 Warrior 和 Weapon 中的 toString() 方法,因为该方法自然会返回对象的 String 表示形式。然后,我将创建一个专门用于创建描述的类,例如 DescriptionMaker,并在其中创建一个方法:

String createDescription(Warrior warrior, Weapon weapon)
{
    return "This " + warrior + " uses a " + weapon;
}

然后可以将该方法的返回值打印到控制台。此外,内部化可以应用在 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:

String createDescription(Warrior warrior, Weapon weapon)
{
    return "This " + warrior + " uses a " + weapon;
}

The return value of this method could then be printed to the console. Also, internalization could be applied in the DescriptionMaker class.

澉约 2024-11-07 15:28:10

在这种情况下,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).

乖不如嘢 2024-11-07 15:28:10

您可以在 Warrior 类中重写 toString 来实现它。

public String toString() {


return "This " + this.description + " warrior uses a " + weapon.toString();

}

重写 toString inWeapon 以返回名称;

as

public String toString() {

    return this.name;

    }

并且可以直接打印为

System.out.println(warrior);

YOu can override toString in your Warrior class to achieve it.

public String toString() {


return "This " + this.description + " warrior uses a " + weapon.toString();

}

override toString inWeapon to return the name;

as

public String toString() {

    return this.name;

    }

and you can directly print as

System.out.println(warrior);
伊面 2024-11-07 15:28:10
public Interface IWarriorView {
  public void display(String description, Weapon weapon);
}

public Interface IWeaponView {
  public void display(String name);
}

public class WeaponViewImpl {
  public void display(String name) {
    System.out.println(name);
  }
}

public class WarriorViewImpl {
  public void display(String description, Weapon weapon) {
    System.out.println("This " + description + " warrior uses a ");
    weapon.display(new WeaponImpl());
  }
}

// Warrior class
public String display(IWarriorView view) {
  view.display(description, weapon);
}

// Weapon class
public String display(IWeaponView view) {
  view.display(name);
}
public Interface IWarriorView {
  public void display(String description, Weapon weapon);
}

public Interface IWeaponView {
  public void display(String name);
}

public class WeaponViewImpl {
  public void display(String name) {
    System.out.println(name);
  }
}

public class WarriorViewImpl {
  public void display(String description, Weapon weapon) {
    System.out.println("This " + description + " warrior uses a ");
    weapon.display(new WeaponImpl());
  }
}

// Warrior class
public String display(IWarriorView view) {
  view.display(description, weapon);
}

// Weapon class
public String display(IWeaponView view) {
  view.display(name);
}
浅笑依然 2024-11-07 15:28:10

将两者结合起来怎么样:

//Warrior class
public String display() 
{
  return "This " + description + " warrior uses a "+weapon.display();;

}

//Weapon class
public String display() 
{
  return name;
}

How about combining both:

//Warrior class
public String display() 
{
  return "This " + description + " warrior uses a "+weapon.display();;

}

//Weapon class
public String display() 
{
  return name;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文