Object Oriented Design & Patterns · Strategy Pattern • Solution – Define an interface type...
Transcript of Object Oriented Design & Patterns · Strategy Pattern • Solution – Define an interface type...
![Page 1: Object Oriented Design & Patterns · Strategy Pattern • Solution – Define an interface type that is an abstraction for the algorithm – Actual strategy classes realize this interface](https://reader035.fdocuments.in/reader035/viewer/2022071017/5fd0797677e6e753da5d83b3/html5/thumbnails/1.jpg)
1
Object Oriented Design & Patterns
![Page 2: Object Oriented Design & Patterns · Strategy Pattern • Solution – Define an interface type that is an abstraction for the algorithm – Actual strategy classes realize this interface](https://reader035.fdocuments.in/reader035/viewer/2022071017/5fd0797677e6e753da5d83b3/html5/thumbnails/2.jpg)
2
Model/View/Controller architecture
• Describes interaction of objects in a user interface with multiple editable views
• Example: PowerPoint or Open Office Impress – outline view – slide view – notes view – slide sorter view
• Edit in one view updates another; seems instantaneous
![Page 3: Object Oriented Design & Patterns · Strategy Pattern • Solution – Define an interface type that is an abstraction for the algorithm – Actual strategy classes realize this interface](https://reader035.fdocuments.in/reader035/viewer/2022071017/5fd0797677e6e753da5d83b3/html5/thumbnails/3.jpg)
3
Model/View/Controller architecture
• Model: data structure, no visual representation (example: array)
• Views: visual representations – drawn using specific format – for example, number table Vs. bar chart
• Controllers: user interaction
![Page 4: Object Oriented Design & Patterns · Strategy Pattern • Solution – Define an interface type that is an abstraction for the algorithm – Actual strategy classes realize this interface](https://reader035.fdocuments.in/reader035/viewer/2022071017/5fd0797677e6e753da5d83b3/html5/thumbnails/4.jpg)
4
Model/View/Controller architecture
• Each aspect has discrete responsibilities: – Views/controllers update model – Model tells views that data has changed – Views redraw themselves
![Page 5: Object Oriented Design & Patterns · Strategy Pattern • Solution – Define an interface type that is an abstraction for the algorithm – Actual strategy classes realize this interface](https://reader035.fdocuments.in/reader035/viewer/2022071017/5fd0797677e6e753da5d83b3/html5/thumbnails/5.jpg)
5
Model/View/Controller architecture
![Page 6: Object Oriented Design & Patterns · Strategy Pattern • Solution – Define an interface type that is an abstraction for the algorithm – Actual strategy classes realize this interface](https://reader035.fdocuments.in/reader035/viewer/2022071017/5fd0797677e6e753da5d83b3/html5/thumbnails/6.jpg)
6
Model/View/Controller architecture
• Minimizes coupling between models, views & controls – model has no knowledge of views; just knows
to notify them of change – views know nothing about controllers – easy to add more views to model – easy to change controller of a view
• Views are example of Observer pattern
![Page 7: Object Oriented Design & Patterns · Strategy Pattern • Solution – Define an interface type that is an abstraction for the algorithm – Actual strategy classes realize this interface](https://reader035.fdocuments.in/reader035/viewer/2022071017/5fd0797677e6e753da5d83b3/html5/thumbnails/7.jpg)
7
Design Patterns
• OO design patterns : – Encourage re-use of code between projects,
programmers – catalog common interactions between objects
that programmers have found useful • Each pattern includes:
– description of problem context – prescription for solution
![Page 8: Object Oriented Design & Patterns · Strategy Pattern • Solution – Define an interface type that is an abstraction for the algorithm – Actual strategy classes realize this interface](https://reader035.fdocuments.in/reader035/viewer/2022071017/5fd0797677e6e753da5d83b3/html5/thumbnails/8.jpg)
8
Observer Pattern
• Model notifies views when something interesting happens
• Button notifies action listeners when something interesting happens
• Views attach themselves to model in order to be notified
• Action listeners attach themselves to button in order to be notified
• Generalize: Observers attach themselves to subject
![Page 9: Object Oriented Design & Patterns · Strategy Pattern • Solution – Define an interface type that is an abstraction for the algorithm – Actual strategy classes realize this interface](https://reader035.fdocuments.in/reader035/viewer/2022071017/5fd0797677e6e753da5d83b3/html5/thumbnails/9.jpg)
9
Observer Pattern
• Context – An object, called the subject, is source of events – One or more observer objects want to be
notified when such an event occurs.
![Page 10: Object Oriented Design & Patterns · Strategy Pattern • Solution – Define an interface type that is an abstraction for the algorithm – Actual strategy classes realize this interface](https://reader035.fdocuments.in/reader035/viewer/2022071017/5fd0797677e6e753da5d83b3/html5/thumbnails/10.jpg)
10
Observer Pattern
• Solution – Define an observer interface type. All concrete
observers implement it. – The subject maintains a collection of observers. – The subject supplies methods for attaching and
detaching observers. – Whenever an event occurs, the subject notifies
all observers.
![Page 11: Object Oriented Design & Patterns · Strategy Pattern • Solution – Define an interface type that is an abstraction for the algorithm – Actual strategy classes realize this interface](https://reader035.fdocuments.in/reader035/viewer/2022071017/5fd0797677e6e753da5d83b3/html5/thumbnails/11.jpg)
11
Observer Pattern
![Page 12: Object Oriented Design & Patterns · Strategy Pattern • Solution – Define an interface type that is an abstraction for the algorithm – Actual strategy classes realize this interface](https://reader035.fdocuments.in/reader035/viewer/2022071017/5fd0797677e6e753da5d83b3/html5/thumbnails/12.jpg)
12
Observer pattern example: Swing buttons
Pattern name: Subject Observer ConcreteObserver attach() notify()
Actual name: JButton ActionListener ActionListener implementor addActionListener() actionPerformed()
![Page 13: Object Oriented Design & Patterns · Strategy Pattern • Solution – Define an interface type that is an abstraction for the algorithm – Actual strategy classes realize this interface](https://reader035.fdocuments.in/reader035/viewer/2022071017/5fd0797677e6e753da5d83b3/html5/thumbnails/13.jpg)
Strategy Pattern
• Context: – A class can benefit from different variants for
an algorithm – Clients sometimes want to replace standard
algorithms with custom versions
13
![Page 14: Object Oriented Design & Patterns · Strategy Pattern • Solution – Define an interface type that is an abstraction for the algorithm – Actual strategy classes realize this interface](https://reader035.fdocuments.in/reader035/viewer/2022071017/5fd0797677e6e753da5d83b3/html5/thumbnails/14.jpg)
Strategy Pattern
• Solution – Define an interface type that is an abstraction
for the algorithm – Actual strategy classes realize this interface
type. – Clients can supply strategy objects – Whenever the algorithm needs to be executed,
the context class calls the appropriate methods of the strategy object 14
![Page 15: Object Oriented Design & Patterns · Strategy Pattern • Solution – Define an interface type that is an abstraction for the algorithm – Actual strategy classes realize this interface](https://reader035.fdocuments.in/reader035/viewer/2022071017/5fd0797677e6e753da5d83b3/html5/thumbnails/15.jpg)
Strategy Pattern
15
![Page 16: Object Oriented Design & Patterns · Strategy Pattern • Solution – Define an interface type that is an abstraction for the algorithm – Actual strategy classes realize this interface](https://reader035.fdocuments.in/reader035/viewer/2022071017/5fd0797677e6e753da5d83b3/html5/thumbnails/16.jpg)
Strategy Pattern - example
• Pluggable strategy for layout management – Layout manager object responsible for executing
concrete strategy – Generalizes to Strategy Design Pattern
16
![Page 17: Object Oriented Design & Patterns · Strategy Pattern • Solution – Define an interface type that is an abstraction for the algorithm – Actual strategy classes realize this interface](https://reader035.fdocuments.in/reader035/viewer/2022071017/5fd0797677e6e753da5d83b3/html5/thumbnails/17.jpg)
Layout Management - Strategy
• Name in Pattern – Context – Strategy – ConcreteStrategy
– doWork()
• Actual name Container LayoutManager BorderLayout, FlowLayout,
GridLayout, etc. layoutContainer()
17
![Page 18: Object Oriented Design & Patterns · Strategy Pattern • Solution – Define an interface type that is an abstraction for the algorithm – Actual strategy classes realize this interface](https://reader035.fdocuments.in/reader035/viewer/2022071017/5fd0797677e6e753da5d83b3/html5/thumbnails/18.jpg)
18
Layout Managers
• User interfaces made up of components • Components placed in containers; need to arrange
components • Swing doesn't use hard-coded pixel coordinates • Advantages:
– Can switch "look and feel" – Can internationalize strings
• Layout manager controls arrangement
![Page 19: Object Oriented Design & Patterns · Strategy Pattern • Solution – Define an interface type that is an abstraction for the algorithm – Actual strategy classes realize this interface](https://reader035.fdocuments.in/reader035/viewer/2022071017/5fd0797677e6e753da5d83b3/html5/thumbnails/19.jpg)
19
Using Layout Managers
• Create container JPanel p = new JPanel();
• Set layout manager p.setLayout(new GridLayout(4,4,3,3));
• Add components p.add(darken);
![Page 20: Object Oriented Design & Patterns · Strategy Pattern • Solution – Define an interface type that is an abstraction for the algorithm – Actual strategy classes realize this interface](https://reader035.fdocuments.in/reader035/viewer/2022071017/5fd0797677e6e753da5d83b3/html5/thumbnails/20.jpg)
20
Using Layout Managers
![Page 21: Object Oriented Design & Patterns · Strategy Pattern • Solution – Define an interface type that is an abstraction for the algorithm – Actual strategy classes realize this interface](https://reader035.fdocuments.in/reader035/viewer/2022071017/5fd0797677e6e753da5d83b3/html5/thumbnails/21.jpg)
Creating a Custom Layout Manager
• Layout manager determines how components are arranged/displayed in a container
• Can determine your own layout pattern by implementing the LayoutManager interface
21
![Page 22: Object Oriented Design & Patterns · Strategy Pattern • Solution – Define an interface type that is an abstraction for the algorithm – Actual strategy classes realize this interface](https://reader035.fdocuments.in/reader035/viewer/2022071017/5fd0797677e6e753da5d83b3/html5/thumbnails/22.jpg)
LayoutManager interface
22
public interface LayoutManager { void layoutContainer(Container parent); Dimension minimumLayoutSize(Container parent); Dimension preferredLayoutSize(Container parent); void addLayoutComponent(String name, Component comp); void removeLayoutComponent(Component comp); }
![Page 23: Object Oriented Design & Patterns · Strategy Pattern • Solution – Define an interface type that is an abstraction for the algorithm – Actual strategy classes realize this interface](https://reader035.fdocuments.in/reader035/viewer/2022071017/5fd0797677e6e753da5d83b3/html5/thumbnails/23.jpg)
LayoutManager methods
• minimumLayoutSize() and preferredLayoutSize() determine the minimum and default size of the container when components are laid out
• Start with preferredLayoutSize - determine the height and width of each component, and use the combined sizes to determine how large window must be
• Can implement minimumLayoutSize() by just having it call preferredLayoutSize()
23
![Page 24: Object Oriented Design & Patterns · Strategy Pattern • Solution – Define an interface type that is an abstraction for the algorithm – Actual strategy classes realize this interface](https://reader035.fdocuments.in/reader035/viewer/2022071017/5fd0797677e6e753da5d83b3/html5/thumbnails/24.jpg)
LayoutManager methods
• layoutContainer() lays out components by setting position and size of each
• Method computes positions of each component then calls setBounds to put each component in its correct location
24
![Page 25: Object Oriented Design & Patterns · Strategy Pattern • Solution – Define an interface type that is an abstraction for the algorithm – Actual strategy classes realize this interface](https://reader035.fdocuments.in/reader035/viewer/2022071017/5fd0797677e6e753da5d83b3/html5/thumbnails/25.jpg)
Composite Pattern
• Context – Primitive objects can be combined to composite
objects – Clients treat a composite object as a primitive
object
25
![Page 26: Object Oriented Design & Patterns · Strategy Pattern • Solution – Define an interface type that is an abstraction for the algorithm – Actual strategy classes realize this interface](https://reader035.fdocuments.in/reader035/viewer/2022071017/5fd0797677e6e753da5d83b3/html5/thumbnails/26.jpg)
Composite Pattern • Solution
– Define an interface type that is an abstraction for the primitive objects
– Composite object collects primitive objects – Composite and primitive classes implement same
interface type. – When implementing a method from the interface type,
the composite class applies the method to its primitive objects and combines the results
26
![Page 27: Object Oriented Design & Patterns · Strategy Pattern • Solution – Define an interface type that is an abstraction for the algorithm – Actual strategy classes realize this interface](https://reader035.fdocuments.in/reader035/viewer/2022071017/5fd0797677e6e753da5d83b3/html5/thumbnails/27.jpg)
Composite Pattern
27
![Page 28: Object Oriented Design & Patterns · Strategy Pattern • Solution – Define an interface type that is an abstraction for the algorithm – Actual strategy classes realize this interface](https://reader035.fdocuments.in/reader035/viewer/2022071017/5fd0797677e6e753da5d83b3/html5/thumbnails/28.jpg)
Containers and Components
• Containers collect GUI components – Sometimes, want to add a container to another
container – Container should be a component
• Composite design pattern – Composite methods typically invoke component
methods – E.g. Container.getPreferredSize invokes getPreferredSize of components
28
![Page 29: Object Oriented Design & Patterns · Strategy Pattern • Solution – Define an interface type that is an abstraction for the algorithm – Actual strategy classes realize this interface](https://reader035.fdocuments.in/reader035/viewer/2022071017/5fd0797677e6e753da5d83b3/html5/thumbnails/29.jpg)
AWT components - composite
• Design name – Primitive – Composite – Leaf
– method()
• Actual name Component Container JButton, JPanel, JOptionPane, or
other class with no children Component method such as
getPreferredSize()
29
![Page 30: Object Oriented Design & Patterns · Strategy Pattern • Solution – Define an interface type that is an abstraction for the algorithm – Actual strategy classes realize this interface](https://reader035.fdocuments.in/reader035/viewer/2022071017/5fd0797677e6e753da5d83b3/html5/thumbnails/30.jpg)
Decorator Pattern
• Context – Component objects can be decorated (visually or
behaviorally enhanced) – The decorated object can be used in the same way
as the undecorated object – The component class does not want to take on the
responsibility of the decoration – There may be an open-ended set of possible
decorations
30
![Page 31: Object Oriented Design & Patterns · Strategy Pattern • Solution – Define an interface type that is an abstraction for the algorithm – Actual strategy classes realize this interface](https://reader035.fdocuments.in/reader035/viewer/2022071017/5fd0797677e6e753da5d83b3/html5/thumbnails/31.jpg)
Decorator Pattern
• Solution – Define an interface type that is an abstraction
for the component – Concrete component classes realize this
interface type. – Decorator classes also realize this interface
type.
31
![Page 32: Object Oriented Design & Patterns · Strategy Pattern • Solution – Define an interface type that is an abstraction for the algorithm – Actual strategy classes realize this interface](https://reader035.fdocuments.in/reader035/viewer/2022071017/5fd0797677e6e753da5d83b3/html5/thumbnails/32.jpg)
Decorator Pattern
• Solution – A decorator object manages the component
object that it decorates – When implementing a method from the
component interface type, the decorator class applies the method to the decorated component and combines the result with the effect of the decoration.
32
![Page 33: Object Oriented Design & Patterns · Strategy Pattern • Solution – Define an interface type that is an abstraction for the algorithm – Actual strategy classes realize this interface](https://reader035.fdocuments.in/reader035/viewer/2022071017/5fd0797677e6e753da5d83b3/html5/thumbnails/33.jpg)
Decorator Pattern
33
![Page 34: Object Oriented Design & Patterns · Strategy Pattern • Solution – Define an interface type that is an abstraction for the algorithm – Actual strategy classes realize this interface](https://reader035.fdocuments.in/reader035/viewer/2022071017/5fd0797677e6e753da5d83b3/html5/thumbnails/34.jpg)
Scroll Panes & Scroll Bars
• Scroll bars useful when a component contains more information than can be displayed in available space
• Add functionality to underlying component • JScrollPane decorates a component and is
itself a component
34
![Page 35: Object Oriented Design & Patterns · Strategy Pattern • Solution – Define an interface type that is an abstraction for the algorithm – Actual strategy classes realize this interface](https://reader035.fdocuments.in/reader035/viewer/2022071017/5fd0797677e6e753da5d83b3/html5/thumbnails/35.jpg)
JScrollPane
• Scroll bars can be attached to components • Approach #1: Component class can turn on
scroll bars • Approach #2: Scroll bars can surround
component JScrollPane pane = new
JScrollPane(component); • Swing uses approach #2 - JScrollPane is
again a component
35
![Page 36: Object Oriented Design & Patterns · Strategy Pattern • Solution – Define an interface type that is an abstraction for the algorithm – Actual strategy classes realize this interface](https://reader035.fdocuments.in/reader035/viewer/2022071017/5fd0797677e6e753da5d83b3/html5/thumbnails/36.jpg)
Decorator Pattern: Scroll Bars
• Design name: – Component – ConcreteComponent – Decorator – method()
• Actual name: Component JTextArea JScrollPane paint() or other method of
Component
36