Object Oriented Design

Today we’re talking about the static keyword. Static properties are owned by a class not an object. A cooler way to use static, however, is with inner static classes.

class Widget {
  private Widget(...) { ... }

  public static class Factory {
    public Factory() { ... }

    public Widget create(...) {
    private int widgetIdCounter;

Basically, the reason we do this is because we only want to make a factory in the context of widgets. We can then access it like so:

Widget.Factory factObj = Widget.factory();

Widget w1 = factObj.create();
Widget w2 = factObj.create();

When to use statics:

Now we’re talking about generics in java, and their dangers.

class BinTree<T> { ... } // generic class
<T> void permute(List<T>); // generic method

When it’s a type, use a parameter:

BinTree<Widget> tree = new BinTree<Widget>();

When it’s a constructor, <> asks Java to guess:

BinTree<Widget> tree = new BinTree<>();

When using a static member, don’t use a parameter:

if (h < BinTree.MAX_HEIGHT) {
    return BinTree.singleton(value);

Using generics is dangerous because of backwards compatability. In older versions of java, generics type checking was bad. Instead use wildcard types.


Finally, we’re getting to design. We start with the class OOD structure: MVC (Model-View-Controller). The point of this pattern is to seperate as much as possible; no tight coupling. We want to be able to replace a part with another very easily. The controller doesn’t care how the model does its job, it just relys on the fact that it will get it done. The view just show things.


tic tac toe example

OO analysis:

Our TTT model needs to:

Error conditions:

Now we’re talking about a generic connect N game:

We need:

The best way to do MVC is by designing an interface by brainstorming the methods, coming up with the signature, and then reimplementing. Now onto a new pattern:


Next class we’re going to be talking more about the builder pattern.