Sunday, September 11, 2011

Objects, Encapsulation, and Cheese Factories: Part 2 of Marcus and the Cursed Cheese

In object oriented programming, objects are types of data structures that include both data and methods for operating on the data. By including both the data and the methods in the same structure, it allows each object to trivially work with its own data. One of the major advantages of objects is that they allow easy encapsulation. Internal data can be hidden from external code and thus only be operated on by the object's own methods.

"I assure you that I inspect all of that machines daily. There is no way that they could produce any inferior cheese. We maintain the highest standards!" argued the foreman.

"I am not claiming that your cheese was inferior, spoiled, or even contained bog dragon droppings. I am not questioning your cheese making standards or your ability to maintain them." argued Marcus. "What I am saying is that the cheese was CURSED. Curses do not happen by accident. This was intentional."

After fifteen minutes of arguing, Marcus was already exasperated. He was on a mission to determine who had cursed his cheese, and the foreman of the Cheswick Castle Cheese Factory was not being helpful. It was not as if the foreman was intentionally dodging Marcus's questions; he simply seemed unable to accept that anything could be amiss with his cheese. Cheswick Castlle was known across the kingdom for producing superior quality cheeses.

"That is simply not possible." countered the foreman. "I would know!"

"How?" asked the cheese minstrel, who had been quietly standing off to the side and scribbling notes. He had promised Marcus that he would not get in the way. So he really did not want to interfere with this discussion, but this seemed like an important point for his new "Ballad of the Cheswick Cheeses". The minstrel wanted to make sure that he got the details correct.

"Yes. How?" added Marcus. "I already told you that this particular curse only targets wizards."

"We have a very sophisticated cheese making process here. We use the latest cheese making objects. They are fully self-contained." explained the foreman.

"Cheese making objects?" asked the minstrel. In all of his years as a cheese minstrel, he had never heard of cheese making objects. He suddenly wondered if he was failing as a cheese minstrel. The self-doubt was crushing.

"He means machines." explained Marcus. Then turning to the foreman, Marcus asked "Why do you call them objects?"

"It was something a recent visitor said," explained the foreman. "He was very impressed with the machines, and he spent a good hour looking one over. He said that the machines were like objects -- they contain both attributes and methods to work with those attributes. The attributes are things like temperature, tray orientation, or humidity. And the machine can operate on these with methods like Bake, Cool, or SpinTray. That way the operators do not need to work directly with the internal attributes, they can just use the externally facing methods."

"Isn't that the case with most machines?" asked Marcus. "That is what makes machines different from something like the minstrel's notebook. The notebook contains data, but cannot do anything with it. It just stores it." Marcus was unimpressed, and he was beginning to think that the foreman was stalling.

"Well..." The foreman thought for a while. "I think you are correct. But our machines are top of the line."

"How do you know?" asked Marcus. "One of the advantages of encapsulation is that you do not need to know about the internal workings. Even the values of the attributes can be hidden from you. How do you know the temperature is correct? Or that the machines even use a process involving temperature? Maybe the machines just bake the cheese at a random heat for a random period of time. All you know if that you get good cheese at the end."

"Ah." The foreman smiled. "In addition to consistently producing superior cheeses, we also have created an extensive suite of unit tests to confirm that the machines are doing what we expect. Here at Cheswick Castle, we maintain the highest standards."

"Yes. But do you test for curses targeting wizards?" pushed Marcus.

"Well... No." answered the foreman. He suddenly looked very worried.

"Tell me more about this visitor." commanded Marcus.

"I can't tell you much." answered the foreman. "I never got his name. He wore a long, dark cloak. He claimed that he was here to inspect the machines, but he only looked at one. He spent an hour with it."

"Can I see the machine that this visitor was so obsessed with?"

Marcus had to admit that the machine was impressive. It must have stored and controlled over a hundred different variables that represented the cheese's current state. But all of that complexity was hidden from the operator behind a simple, clean set of controls.

Unfortunately, a quick check of the machine revealed no signs of a curse causing subroutine.

"Did the visitor look at anything else?" inquired Marcus.

The foreman, now sufficiently panicked, nodded vigorously. "He wanted to see our shipping facility. He said something about wondering where all our cheese went."

Marcus was certain that in the shipping area, he would find both stacks of cheese and a list of wizards awaiting their shipments.


Read about how Marcus met the cheese minstrel in Part 1: Data Validation, Marcus, and the Minstrel of Cheese.

Or read about Marcus and the importance of unit testing in The Importance of Unit Testing Magical Spells or good version control in Version Control in Magic Spell Development.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.