In the right place Decorator design pattern shines, but i noticed in many applications few mistakes made while using decorator.
an exemple (using java.io
) which demonstrate the problem with bad using of Decorator.
public OutputStream foo(...){
return new BufferedOutputStream(...);
}
The problem with this method, it return a BufferedOutputStream
as OutputStream
, so a user may decorate again this object by BufferedOutputStream
(he don't know that the method return a buffered stream), so here we got double buffering for free (worsing more the performance for nothing).
another exemple :
public void foo(OutputStream os){
BufferedOutputStream bos = new BufferedOutputStream(is);
...
}
Here another exemple, the user may supply BufferedOutputStream
to foo which already decorate it for internal use, the user may want to increase performance by providing a buffered stream, but in reality he's making it more worse, its not his fault, he don't know what the method do inside (Black box).
So anyone can tell me what is the best practices for using Decorator design pattern, not only with java.io
but with general use for Decorator?
Aucun commentaire:
Enregistrer un commentaire