I have a (Java) WindowItem class that has a problem: one of the methods is not thread safe. I cannot fix WindowItem because it is part of the external structure. Therefore, I decided that I would implement a Decorator for it, which has a "synchronized" keyword in the method in question.
The decorator extends WindowItem and will also contain WindowItem. Following the Decorator drawing, I create methods in Decorator that invoke the WindowItem contained in it.
However, WindowItem has several final methods that I cannot override in Decorator. This violates the transparency of the Decorator. Let's make it explicit:
public class WindowItem { private List<WindowItem> windows; public Properties getMethodWithProblem() { ... } public final int getwindowCount() { return windows.size(); } } public class WindowItemDecorator extends WindowItem { private WindowItem item; public WindowItemDecorator(WindowItem item) { this.item = item; }
In my own code, whenever I have to skip WindowItem somewhere, I first wrapped it in a decorator: a new WindowItemDecorator (item) - and the thread safety issue will disappear. However, if my code calls getwindowCount () in WindowItemDecorator, it will always be zero: it executes getWindowCount () in the superclass, and not in the "item" element.
So, I would say that the design of WindowItem (the fact that it has public final methods) makes it impossible to create a Decorator for this class.
Is this right, or am I missing something?
In this case, I could save a copy of the window list in the decorator and keep it in sync, and then the result of getWindowCount () would be correct. But in this case, I prefer to develop and fix the framework ...
source share