AlgoViz

Object composition

medium 50 min

Build a big object from small ones.

Video lesson · Chapter 1 of 6

Make a prediction

0:00 / 4:23

See it run

Watch calls travel from the whole to its parts, and parts being exchanged.

Operations

One patrol

The robot knows the order. The parts know how.

Step 1 of 5

A robot and three parts

explorerRobotplacenonedatanonemovementWalkingsensorCameralinkWifi

Each part is an object of its own. The robot holds them.

Step 1: A robot and three parts

def patrol(self):    place = self._movement.move()    data = self._sensor.scan()    return self._link.send(f"{place} {data}")
Good for
Independent choices
Costs
Three small promises
Classes for 3 x 3 x 3
10, not 27

If the method of the whole reads like a story, the cuts are right.

In the real world

The same object composition doing real jobs. Pick one and step through it.

The number of classes as choices arrive.

Step 1 of 5

Two choices of two

Tree: leavesParts: classes
2 x 245
3 x 2
3 x 3
3 x 3 x 3
4 x 3 x 3

The tree is smaller: 4 against 5.

Step 1: Two choices of two

Common mistakes

The bugs interviewers watch for, with the fix beside each.

  • The whole creates its parts

    major

    Tests need the real thing, and another combination needs another class.

    Wrong
    def __init__(self):    self._link = Wifi()
    Right
    def __init__(self, link):    self._link = link
  • Reaching through

    major

    Callers know the part. Now it cannot be exchanged.

    Wrong
    robot.link.modem.restart()
    Right
    robot.recover()      # the robot passes it on
  • An empty field

    minor

    Every method must ask whether the part exists, and one forgets.

    Wrong
    if self._link is not None:    self._link.send(data)
    Right
    self._link.send(data)     # NoLink does nothing
  • Parts that know each other

    critical

    Each new part must know all others. Pairs grow with the square.

    Wrong
    class Radar:    def scan(self, movement):        if isinstance(movement, Flying): ...
    Right
    build(parts)      # checks rules between parts
  • Assign and close

    critical

    Work in progress still holds the old part.

    Wrong
    old, self._link = self._link, newold.close()
    Right
    old.retire()      # closed by its last user
  • Forward everything around a guard

    critical

    A method that is added to the inner class later goes around the check.

    Wrong
    def __getattr__(self, name):    return getattr(self._account, name)
    Right
    def withdraw(self, n):    self._check(n)    self._account.withdraw(n)

Interview tips

  • Find the cut

    • Which choices are independent?
    • Adjectives in class names
    • Always together: one part
  • Keep promises small

    • One question
    • A named failure
    • A fake in three lines
  • One place for wiring

    • Create and combine outside
    • Rules in the builder
    • A valid whole or all problems
  • Say the price

    • More names
    • More hops
    • Not closed against new questions