vendredi 28 avril 2017

Applying the Decorator Pattern to Java String Class

I want to add functionality to the java.lang.String class. I thought of using the decorator pattern as follows:

public class StringDecorator {

String str;
public StringDecorator(String str)
{
    this.str = str;


}

// Wrap all methods in String class such as:

public int length()
{
    return str.length();
}

// Add other methods from String class here.. And then add new functions..

public void newFunction1()
{

}



}

However, I read on SO here: http://ift.tt/2oEKoqR that this is not appropriate because String is final and cannot be extended. The user also suggests that "a different design pattern is in order".

Can someone please confirm if indeed it is not appropriate to use the decorator pattern in this scenario and if so what other design pattern can be used instead?

Aucun commentaire:

Enregistrer un commentaire