Decorating a String in Java

流过昼夜 提交于 2019-12-08 12:28:33

问题


Suppose I want to use the Decorator pattern to add functionality to the java.lang.String class. (Just for example, to add a toRussian() method.) String is a final class. Is the following the only way to do it?

class DecoratedString implements Serializable, CharSequence, 
    Comparable<DecoratedString> 
{
  private final String str;
  public DecoratedString (String str) {
    this.str = str;
  }
  public DecoratedString toRussian() {
     ...
  }
  public String toString() { return str; }
  public int hashCode() { return str.hashCode(); }
  public boolean equals(Object obj) { /* don't forget this one */ } 
  public char charAt(int index) { return str.charAt(index);}
  public int codePointAt(int index) { return str.codePointAt(index);}
  ... and so on for 30 or so more methods of String ...
}

Usage:

String greeting = new DecoratedString("Hello, world!").toRussian().toString();

Postscript: This is so EASY to do in Objective-C with "Categories". Python now has @Decorators. And of course it's trivial in JavaScript, where you routinely add trim() to the String prototype. Is it totally impossible in Java?

Postpostscript: OK, toRussian() is a bad example. How would you add trim(), if String didn't already have trim()? trim() is an example of something every String should have.


回答1:


The decorator pattern usually works by extending the original class with added functionality.

Since the String class is final, it cannot be extended.

The downside to using composition rather than inheritance as you are doing, is that typically a decorator is implemented in such a way that anywhere you can use an X, you can use a DecoratedX. (The pattern should in fact allow you to decorate an X with any number of decorators and still use it as an X.) You would not be able to do that with your current design, and would end up re-implementing any of the extended String functionality you need.

A different design pattern is probably in order.




回答2:


I'd have a Translator interface and pass an (immutable) String into it. Different implementations for different languages.

Why would you embed all that Russian specific logic into a decorated String?




回答3:


Isn't this sort of translation the entire point behind Java's ResourceBundle?



来源:https://stackoverflow.com/questions/3945218/decorating-a-string-in-java

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!