Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

227
Visualizações
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: https://stackoverflow.com/a/3945482 that this is not appropriate because String is final and cannot be extended. The user also suggests that "a different design pattern is probably 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?

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

One problem with this is that in the decorator pattern, the decorator and the object being wrapped usually implement a common interface:

interface Interface {...}
class A implements Interface {...}
class ADecorator implements Interface {...}

So that where a method expects an Interface, both an A and ADecorator can be passed.

But in the case of String, almost all API using it require the implementation type String and not the interface type, CharSequence. So when you create this wrapper, you can not just use it with a method requiring a String. And since String is final you can't extend it and get around the problem that way.


What you have is still be useful, but it should be viewed as composition, and it would be helpful to have a getString (or whatever) method to retrieve the wrapped object (as someone suggested), so that you can still use it with methods requiring a String.

Assuming you will also make your class immutable, it could still be useful to have those delegating methods, but instead return an instance of the wrapper class:

public StringDecorator replace(char oldChar, char newChar) {
    return new StringDecorator(str.replace(oldChar, newChar));
}

But in most cases, you could get away with something like decorator.getString().length().

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda