![]() But then, when you implement context menus, shortcuts, and other stuff, you have to either duplicate the operation’s code in many classes or make menus dependent on buttons, which is an even worse option. In other words, having the code for copying text inside the CopyButton subclass was fine. Initially, when our app only had the toolbar, it was okay to place the implementation of various operations into the button subclasses. For example, a user could click a small “Copy” button on the toolbar, or copy something via the context menu, or just hit Ctrl+C on the keyboard. Some operations, such as copying/pasting text, would need to be invoked from multiple places. Several classes implement the same functionality.Īnd here’s the ugliest part. Put simply, your GUI code has become awkwardly dependent on the volatile code of the business logic. First, you have an enormous number of subclasses, and that would be okay if you weren’t risking breaking the code in these subclasses each time you modify the base Button class. What can go wrong?īefore long, you realize that this approach is deeply flawed. ![]() These subclasses would contain the code that would have to be executed on a button click. Where would you put the code for the various click handlers of these buttons? The simplest solution is to create tons of subclasses for each place where the button is used. ![]() While all of these buttons look similar, they’re all supposed to do different things. You created a very neat Button class that can be used for buttons on the toolbar, as well as for generic buttons in various dialogs.Īll buttons of the app are derived from the same class. Your current task is to create a toolbar with a bunch of buttons for various operations of the editor. Imagine that you’re working on a new text-editor app.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |