Software Development (CSC 321 2015F) : EBoards
Primary: [Front Door] [Schedule] - [Academic Honesty] [Disabilities] [Email] [FAQ] [Teaching & Learning]
Sections: [Assignments] [EBoards] [Examples] [Handouts] [Outlines]
Reference: [Slack] [Ruby@CodeCademy]
Related Courses: [CSC 321 2015F @ EdX] [Berkeley CS 169 2014F] [CSC 321 2015S (Davis)] [CSC 322 2015F (Rebelsky)]
Misc: [SamR] [Glimmer Labs] [CS@Grinnell] [Grinnell] [Issue Tracker]
Overview
Be ready to give
Can spawn creatures in a game.
interface Creature
{
} // ...
class Alien implements Creature
{
...
}
class Centipede implements Creature
{
...
}
interface CreateFactory
{
public Creature makeCreature();
}
class Game
{
CreatureFactory factory;
public Game(CreatureFactory cf)
{
this factory = cf;
}
public void play()
{
everyTenSeconds
factory.makeCreature();
}
} // class Game
new Game(new CreatureFactory() {
Creature makeCreature()
{
return new Centipede();
}
}
new Game(new CreatureFactory() {
Creature makeCreature()
{
return new Alien();
}
}
Example interface GUIFactory { Button createButton(String buttonName); } // interface GUIFactory
class WindowFactory implements GUIFactory
{
Button createButton(String buttonName)
{
return new windowButton(String buttonName);
} // createButton
}
class iFactory implements GUIFactory
{
Button createButton(string buttonName)
{
app.setCustomLayout(...)
app.getLayout.addButton()
return app.getLayout.getLatestButton();
} // class iFactory
Implementation: Use a private constructor with a class method for extracting the value.
public class Singleton
{
public static Singleton theOneAndOnly;
public static Singleton get()
{
if (theOneAndOnly == null)
theOneAndOnly = new Singleton();
return theOneAndOnly;
}
private Singleton()
{
...;
}
} // class Singleton
Java example public class Wall { String getContents() { return "Brick"; } }
wall1 = new Wall() {
String getContents()
{
if (random() < 0.5)
return "You see a smokey mist";
else
return "You see misty smoke";
}
}
wall2 = new Wall();
Key idea: Once you've made an object (or a copy of an object), you can change its methods (or values).
Alternate version (probably closer to the original).
public class Wall
{
public Wall(String type, int length, int width, int height
int x, int y, int z, int loadbearingcapability,
...)
{
...
}
} // class Wall
wall1 = new Wall("stone", 10, 10, 5, 100, 200, 300, 50);
wall2 = new Wall("brick", 10, 10, 5, 110, 200, 300, 50);
wall3 = new Wall("stone", 10, 10, 6, 100, 210, 300, 50);
// Another approach
wall1 = new Wall("stone", 10, 10, 5, 100, 200, 300, 50);
wall2 = wall1.clone();
wall2.setType("brick");
wall2.setX(wall2.x() + 10);
wall3 = wall1.clone();
wall3.setX(wall3.x() - 10);
// That is, we started with one generic wall and then
// customized the parts we needed to customize for each
// subsequent wall.
Bridges are similar; you write them while building your code, while adapters are designed for legacy code.
public interface Comparator
{
/**
* Return a negative number if x comes before y
* Returns 0 if x equals y
* Returns a positive number if x is greater than y
*/
public int compare(Object x, Object y);
} // interface Comparator
public class LessThan
{
public boolean compare(Number x, Number y)
{
return x < y;
}
} // class LessThan
public static void sort(Integer[] vals, Comparator compare)
...
// We need to change the interface of LessThan to match the
// interface of comparator.
// That's a task for an adapter.
public class AdaptedLessThan
implements Comparator
{
this.lt = new LessThan();
public int compare(Object x, Object y)
{
if (this.lt.compare(x, y))
return -1;
else if this.lt.compare(y,x)
return 1;
else
return 0;
}
}
public interface Lightning
{
}
public interface TradiPhone
{
}
public class UpdateiPhone
implements Lightning
{
TradiPhone thingToBeAdapted;
...
}
Sam refers to this as "Wrapping" a class.
Template Method: Redefine the steps of an algorithm without changing the algorithm. public abstract class SelectionSorter { public int compare(Object val1, Object val2); // abstract public void sort(Object[] values) { ... if (compare(val1,val2) < 0) ... } }
public class IncreasingSorter
extends SelectionSorter
{
public int compare(Object val1, Object val2)
{
return val1 < val2;
}
} // extends SelectionSorter
public class DecreasingSorter
extends SelectionSorter
{
public int compare(Object val1, Object val2)
{
return val1 > val2;
}
} // extends SelectionSorter
Strategy: Redefine implementation of the same algorithm
public interface Sorter
{
public void sort(Object[] values);
}
public class QuickSorter
{
public void sort(Object[] values)
{
int pos = partition(values);
sort(...);
sort(...);
} // sort
}
public class MergeSorter
{
}