Grokking the Object Oriented Design Interview
Vote

0% completed

OO Analysis and Design

Analysis and design are two different activities

The four steps

The four steps on one sentence

Why UML

Three ways this goes wrong

Key Takeaways

The previous lesson gave you the four principles a design is made of. This lesson gives you the process that produces one.

Object oriented analysis and design is usually shortened to OOAD. It turns a description of a system into a set of objects and the connections between them. It is old, it is standard, and it is where the vocabulary of this interview comes from.

Analysis and design are two different activities

The two words in the name are not decoration. They describe two activities with different questions and different outputs. Mixing them is the most common way to lose time.

ActivityThe question it answersWhat exists at the end
AnalysisWhat is this system, and what must it do?A list of things in the problem, in the language of the business
DesignHow will objects deliver that?Classes, their responsibilities, and the relationships between them
ImplementationHow is it written in a language?Working code

Analysis talks about books, members and loans. Design talks about Book, Member and Loan, and about which one holds which. Implementation talks about ArrayList and shared_ptr.

A candidate who names a HashMap in the first two minutes has already reached implementation. They skipped both activities they were asked to show.

The four steps

Image

Step 1: find the objects. Read the description and list the things that hold data or do work. Most of them are nouns in the description. A thing that only holds one value, like a color, is a field, not an object.

Step 2: define the relationships. For every pair of objects that appear together, decide which one holds the other, whether it owns it, and how many. A library holds many books. A loan refers to one book and one member.

Step 3: establish each interface. An interface is the list of operations an object offers to the rest of the system. Write that list for each object. This is the point where encapsulation is decided: everything you do not list becomes private.

Step 4: make the design. Draw the classes, their fields, their operations and their relationships in one picture. Check it against the description you started from. The picture is the design; the code follows from it.

Steps 1 and 2 are analysis. Steps 3 and 4 are design. The order matters, because each step is the input to the next one.

Choosing operations before you know the objects puts operations on the wrong class. That is slow to undo.

The four steps on one sentence

Take a description small enough to finish here: "members of a library borrow books, and a borrowed book is due back on a date".

Step 1, the objects. The nouns are member, library, book, date and borrowed book. Date is a single value, so it is a field. The other four survive: Library, Member, Book, and the borrowed book, which is better named Loan.

Loan is the interesting one. It is not in the sentence as a noun, and it is the object most people miss.

There is a general rule here. When two objects are connected and the connection itself carries data, that connection is an object. A borrowed book has a borrower and a due date. Neither belongs on Book, because neither is true of a book on the shelf.

Step 2, the relationships. A library owns many books, and it owns them in the strong sense: destroy the library and those book records go with it. That is the filled diamond in the picture below.

A library also registers many members, but it does not own them. A person exists whether or not the library does. That is a plain arrow.

A loan refers to exactly one book and exactly one member, and it owns neither.

Step 3, the interfaces. A library lends, takes back, and says whether a copy is out. A book gives its title and its ISBN. A member says how many loans it has.

A loan says whether it is late. Everything else, including every field, stays private.

Notice which class is_out belongs to. A book cannot answer it, because a book does not know about loans. The library holds the loans, so the library is the only object that can answer the question.

Step 4, the design.

Image

That picture is a complete answer to the sentence we started from. It took four steps and no code.

Why UML

Everything above has to be written down in a form another engineer can read. That form is UML, the Unified Modeling Language. It is the standard set of symbols for drawing a design.

UML replaced a set of competing notations decades ago. It won because it can express the ideas of object oriented analysis and design.

The filled diamond in the picture above means ownership. The plain arrow means one object refers to another. The plus and minus signs mark what is public and what is private.

None of that is decoration. Each symbol is a design decision made visible.

This is why the notation comes after the concepts and not before. A diagram is only useful once you know what each shape means.

Three ways this goes wrong

Naming classes for tables. UserRow, OrderTable, BookData. That is database thinking with an object oriented name. An object combines data with the code that changes it. If a class has only fields and getters, ask what it is supposed to do.

Missing the connection object. Loan above, Reservation in a hotel, Ticket in a parking lot. If you are putting borrower and due_date on Book, the connection object is the class you have not created yet.

Listing every operation you can think of. Step 3 asks what an object offers to other objects, not everything it could possibly do. A long method list on every class is a design that has not decided anything.

Key Takeaways

  • Analysis asks what the system is; design asks how objects deliver it. They are different activities with different outputs.
  • The four steps are: find the objects, define the relationships, establish each interface, make the design.
  • Objects usually come from the nouns, but a thing that holds only one value is a field.
  • When a connection between two objects carries its own data, that connection is an object.
  • Everything you do not list in step 3 is private, which is where encapsulation is actually decided.
  • UML exists so that the result can be read by someone else. It comes after the concepts, not before.

You now have the concepts and the process. What is missing is the notation the process is written in. The rest of this chapter covers it, one diagram type per lesson. It starts with what UML is, and which of its diagrams matter here.

Reading Progress

0%


Vote for new content

On This Page

Analysis and design are two different activities

The four steps

The four steps on one sentence

Why UML

Three ways this goes wrong

Key Takeaways