ADT examples

Remember, it’s not that deep

An ADT in Snap! is just a set of blocks that together create an abstraction by hiding the details of how some related pieces of data are stored.

Quiz item

Constructor

Selectors

Quiz item constructor

Quiz item question selector

Quiz item answer selector

Quiz item choices selector

Student

Constructor

Selectors

Student constructor

Student name selector

Student grade selector

Student birthday selector

Book

Constructor

Selectors

Book constructor

Book title selector

Book author selector

Book pages selector

Book year selector

Describe book

Now use the abstraction to create a new block

Result

Without abstraction

This is not horrendous but it requires more decoding to understand.

It also has a bug. Can you see it?

Now can you see it?

Here’s a block with the same bug but written using our ADT.

Easier, right?

ADT recipe

Write a constructor that takes separate pieces of data as inputs and puts them in a list.

Write selectors that take a single input and extract the appropriate of the items from the list.

Obviously constructors and selectors have to agree on where each piece of data is put in the list.

But no other code should have to care about that.