Design Case Study: Java Swing

Design Case Study: Java Swing

Principles of Software Construction: Objects, Design, and Concurrency (Part 3: Design Case Studies) Design Case Study: GUI with Swing Christian Kästner Bogdan Vasilescu School of Computer Science 15-214 1 Administrivia • Homework 4a feedback tomorrow in recitation 15-214 2 3 15-214 3 Concurrency vs Parallelism • Concurrency – Doing multiple things in an undefined order – May be done in parallel, sequentially, or interleaved (e.g., multitasking on single core machine) • Parallelism – Actually doing multiple things at the same time (e.g., multi-core processor) 4 15-214 4 Learning Goals • Designing testable programs by separating GUI and Core • Understanding design patterns and how they achieve design goals 5 15-214 5 DECOUPLING THE GUI 15-214 6 A GUI design challenge • Consider a blackjack game, implemented by a Game class: – Player clicks “hit” and expects a new card – When should the GUI update the screen? Game GUI hit() getData update 15-214 7 A GUI design challenge, extended • What if we want to show the points won? Game GUI PointsPanel hit getData update update getData update 15-214 8 Game updates GUI? • What if points change for reasons not started by the GUI? (or computations take a long time and should not block) Game GUI PointsPanel hit getData update update getData update 15-214 9 Game updates GUI? • Let the Game tell the GUI that something happened Game GUI PointsPanel hit update(data) update update(data) update 15-214 10 Game updates GUI? • Let the Game tell the GUI that something happened Game GUI PointsPanel hit update(data) update update(data) update Problem: This couples the World to the GUI implementation. 15-214 11 Core implementation vs. GUI • Core implementation: Application logic – Computing some result, updating data • GUI – Graphical representation of data – Source of user interactions • Design guideline: Avoid coupling the GUI with core application – Multiple UIs with single core implementation – Test core without UI – Design for change, design for reuse, design for division of labor; low coupling, high cohesion 15-214 12 Decoupling with the Observer pattern • Let the Game tell all interested components about updates Game GUI PointsPanel register register hit notify update notify update 15-214 13 Separating application core and GUI, a summary • Reduce coupling: do not allow core to depend on UI • Create and test the core without a GUI – Use the Observer pattern to communicate information from the core (Model) to the GUI (View) GUI GUI Tests Core Core Tests 15-214 14 An architectural pattern: Model-View- Controller (MVC) Manage inputs from user: mouse, keyboard, menu, etc. Manage display of information on the screen Manage data related to the application domain 15-214 15 Model-View-Controller (MVC) Passive model Active model http://msdn.microsoft.com/en-us/library/ff649643.aspx 15-214 16 A DESIGN CASE STUDY ON GUIS (LEARN FROM EXPERIENCED DESIGNERS) 17 15-214 17 Design of GUIs • GUIs are full of design patterns – Strategy pattern – Template Method pattern – Composite pattern – Observer pattern – Decorator pattern – Façade pattern – Adapter pattern – Command pattern – Model-View-Controller 15-214 18 DESIGN OF LIST AND TABLE ENTRIES 15-214 19 JList and JTree • Lists and trees highly flexible (reusable) • Can change rendering of cells • Can change source of data to display //simple use String [] items = { “a”, “b”, “c” }; JList list = new JList(items); 15-214 20 Which design pattern The ListModel was used here? • Allows list widget (view) to react to changes in //withthe model a ListModel ListModel model = new DefaultListModel(); model.addElement(“a”); JList list = new JList(model); interface ListModel<T> { int getSize(); T getElementAt(int index); void addListDataListener(ListDataListener l); void removeListDataListener(ListDataListener l); } 15-214 21 The ListModel • Allows list widget (view) to react to changes in //withthe model a ListModel ListModel model = new DefaultListModel(); model.addElement(“a”); JList list = new JList(model); interface ListModel<T> { int getSize(); interfaceT getElementAt ListDataListener(int index); extends EventListener { void voidaddListDataListener intervalAdded(…);(ListDataListener l); void voidremoveListDataListener intervalRemoved(…);(ListDataListener l); } void contentsChanged(…); } 15-214 22 Scenario • Assume we want to show all anagrams found by a generator in a list and update the list as we find new solutions //design 1 class AnagramGen implements ListModel<String> { List<Word> items … int getSize() { return items.size(); } String getElementAt(int index) { items.get(index).toString(); } void addListDataListener(ListDataListener l) {…} protected void fireListUpdated() {…} 15}- 214 23 Scenario • Assume we want to show all anagrams found by a generator in a list and update the list as we find new solutions //design 2 class AnagramGen { DefaultListModel<String> items … public getListModel() { return items; } public Iterable<String> getItems() { return items.elements(); } 15-214 24 Which design pattern Scenario was used here? • Assume we want to show all anagrams found by a generator in a list and update the list as we find new solutions //design 3 class AnagramAdapter implements ListModel<String> { private final AnagramGen an; public AnagramAdapter(AnagramGen s) {an = s;} int getSize() { return count(an.getWords()); } String getElementAt(int index) { find(an.getWords(), index).toString(); } void addListDataListener(ListDataListener l) {…} … 25 15}- 214 Comparing the three solutions «interface» JList AnagramGen ListModel 2 +getSize() +getItems() 1 +getElementAt() 1 DefaultListModel AnagramGen JList -items +getSize() +getItems() +getElementAt() +getSize() +getElementAt() «interface» JList 3 ListModel +getSize() +getElementAt() AnagramAdapter AnagramGen -items +getSize() +getItems() 15-214 +getElementAt() 26 The Adapter Design Pattern 15-214 27 The Adapter Design Pattern • Applicability – You want to use an existing class, and its interface does not match the one you need – You want to create a reusable class that cooperates with unrelated classes that don’t necessarily have compatible interfaces – You need to use several subclasses, but it’s impractical to adapt their interface by subclassing each one •Consequences • Exposes the functionality of an object in another form • Unifies the interfaces of multiple incompatible adaptee objects • Lets a single adapter work with multiple adaptees in a hierarchy • -> Low coupling, high cohesion 15-214 28 Other Scenarios for Adapters • You have an application that processes data with an Iterator. Methods are: – boolean hasNext(); – Object next(); • You need to read that data from a database using JDBC. Methods are: – boolean next(); – Object getObject(int column); • You might have to get the information from other sources in the future. 15-214 29 Façade vs. Adapter • Motivation – Façade: simplify the interface – Adapter: match an existing interface • Adapter: interface is given – Not typically true in Façade • Adapter: polymorphic – Dispatch dynamically to multiple implementations – Façade: typically choose the implementation statically 15-214 30 Design Goals • Design to explicit interfaces – Façade – a new interface for a library – Adapter – design application to a common interface, adapt other libraries to that • Favor composition over inheritance – Façade – library is composed within Façade – Adapter – adapter object interposed between client and implementation • Design for change with information hiding – Both Façade and Adapter – shields variations in the implementation from the client • Design for reuse – Façade provides a simple reusable interface to subsystem – Adapter allows to reuse objects without fitting interface 15-214 31 Custom Renderer • Renderer of list items and table cells exchangable • Interface TableCellRenderer • Strategy design pattern (again) 15-214 32 DESIGN OF A LAYOUT MECHANISM 33 15-214 33 Swing Layout Manager see http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html 15-214 34 A naïve Implementation • Hard-code layout algorithms class JPanel { protected void doLayout() { switch(getLayoutType()) { case BOX_LAYOUT: adjustSizeBox(); break; case BORDER_LAYOUT: adjustSizeBorder(); break; ... } } private adjustSizeBox() { … } } • A new layout requires changing or overriding JPanel 15-214 35 Layout Manager • A panel has a list of children • Different layouts possible – List of predefined layout strategies – Own layouts possible • Every widget has preferred size • Delegate specific layout to a separate class implementing an explicit interface – Use polymorphism for extensibility 15-214 36 Which design pattern Layout Managers was used here? panel.setLayout(new BorderLayout(0,0)); abstract class Container { // JPanel is a Container private LayoutManager layoutMgr; public doLayout() { LayoutManager m = this.layoutMgr; if (m!=null) m.layoutContainer(this); } public Component[] getComponents() { … } } interface LayoutManager { void layoutContainer(Container c); Dimension getMinimumLayoutSize(Container c); Dimension getPreferredLayoutSize(Container c); } 15-214 37 Behavioral: Strategy 15-214 38 Remember Design Discussion for Strategy Pattern • Design to explicit interfaces – Strategy: the algorithm interface • Design for change and information hiding – Find what varies and encapsulate it – Allows adding alternative variations later • Design for reuse – Strategy class may be reused in different contexts – Context class may be reused even if existing strategies don’t fit • Low coupling – Decouple context

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    71 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us