Design Patterns
Design Patterns
Design Patterns
Reading 1
Reading 2
Introduction
Overview
Why?
Scenario
Design Patterns
Design patterns in any domain have two basic
parts
2. The solution
Carpet
Nooks
Book shelves
Unique shapes
City-Building Game
City-Building Game
Problem / Context
Solution
Command
PlaceStructure subclass
Command[] inputActions;
inputActions[0] = PickStructure
Undo
Command
execute
undo
Undo
AbstractAction
Command = AbstractAction
execute = actionPerformed
Attaching Actions
Menus
Traversing a Collection
But this still allows the user to manipulate the internal state
And what if the user wants to look at two elements at once?
Problem / Context
Solution
ITERATOR in Java
Breaking Encapsulation
Problem / Context
Solution
Iterator<T> iterator()
Example
public class LinkedList implements Iterable
{
public Node First;
public Node Last;
...
@Override
public java.util.Iterator iterator()
{
return new LinkedListIterator();
}
class LinkedListIterator implements Iterator<Object>
{
...
@Override
public boolean hasNext()
{
...
}
@Override
public Object next()
{
...
}
@Override
public void remove()
{
...
}
}
}
Problem / Context
Solution
Subject
attach = addActionListener
notify = actionPerformed
Cellular Automata
https://www.youtube.com/watch?v=7j1RGylsA40
We focused on elementary cellular automata
Displaying an Elementary CA
Displaying an Elementary CA
OBSERVER pattern
CA aggregates observers
Whenever the CA changes, it notifies its observers,
which can update appropriately
public class CA
{
private ArrayList<ActionListener> ActionListeners;
...
public void addActionListener(ActionListener NewActionListener)
{
if( NewActionListener != null ) ActionListeners.add(NewActionListener);
}
...
public void Update()
{
Cells = Ruleset.Calculate(Cells);
e.getSource
Putting it Together
What is MVC?
Two objects are decoupled when they don't rely on each other
So the front end shouldn't rely on one particular back end
implementation
And the back end should rely on one particular GUI
Polymorphism
The front end only needs to know that the back end supports saving,
and the back end only needs to know that the front end supports
displaying
Model
View
Controller
accepts input
Course editor
MVC Example
Drawbacks
PATTERNS EVERYWHERE!
Increased Complexity
VERY SMALL
Anti-Patterns
Anti-Patterns
http://en.wikipedia.org/wiki/Anti-pattern
Blob
God Object
Anti-Pattern Examples
Poltergeist
Magic-Numbers
Anti-Pattern Examples
Object Orgy
Error Hiding
Sequential Coupling
Scenario
Scenario
Questions?
More
Links
http://gameprogrammingpatterns.com/
Anti-Patterns
http://www.jacana.plus.com/pattern/P0.htm
http://en.wikipedia.org/wiki/Oregon_Experiment
http://en.wikipedia.org/wiki/A_Pattern_Language
http://en.wikipedia.org/wiki/Anti-pattern
r/cellular_automata
Links
http://www.reddit.com/r/cellular_automata/