The rules of object ownership

December 14, 2000, 01:44 PM —  ITworld.com — 

Last week's installment, which introduced HREF="http://www.itworld.com/App/554/ITW814/">the four
rules of software modeling, ended with a spot-the-bug problem concerning a
financial ledger.

The bug occurred when the user deleted a transaction, then pressed F5 to refresh the
view. Since Item::OnDelete deleted the transaction
directly, the ledger did not know to remove it from its list. Hence, when size=+0>ListViewControl::Refresh iterates the transactions in the ledger,
it tries to create an item based on a deleted Transaction.

The rule of ownership defined

At the root of this bug is a violation of one of the rules of software modeling: the
rule of ownership. The way to fix this bug is to have size=+0>Item::OnDelete call size=+0>Ledger::DeleteTransaction, passing in the pointer to the
transaction to be deleted. Since the Ledger is the owner of all Transactions, it is
responsible for deleting them.

The rule of ownership, which defines the lifetime, control, and placement of objects,
stipulates the following three conditions:

  • The owner of an object is responsible for its creation and destruction
  • Every object has exactly one owner
  • Ownership is not transferable

A closer look

Let's study these parts individually. First, the owner of an object is responsible for
that object's creation and destruction. That is not to say that the owner necessarily
creates the object itself -- it may use a factory. We'll revisit factories toward the
end of this installment.

No one but the owner may destroy an object. As the financial ledger example shows, if
an object destroys another that it does not own, problems can occur. An object may
request that another be deleted, but it must always defer to the victim's owner.

Second, every object has exactly one owner. Since every object in existence was created
by something, each one has an owner. Furthermore, two objects cannot share the
responsibility of ownership, because an object can only be created or destroyed once.
Hence each has one and only one owner.

Because of this, the path of ownership defines a tree within the software system. As we
all know, a tree has one root. The root of a software system represents the system
itself (application, applet, module, etc.). The root and singleton objects are created
and destroyed intrinsically. They exist from the start of the system to its
termination, and therefore their owner is outside the developer's control.

The third part of the rule of ownership is that ownership is not transferable. Once an
object owns another, it cannot pass that responsibility off. The use of a factory
appears to be in violation of ownership, but semantically speaking it is not. The
factory creates the object on behalf of the owner. The owner is still responsible for
the object's creation; it simply uses the factory. Since the factory immediately
returns the object and does not retain any information about it, it is never actually
the owner.

In the next installment, we will study some of the ways to implement ownership using
different technologies.

» posted by abennett

ITworld.com

Sign up for ITworld's Daily newsletter
Follow ITworld on Twitter @IT_world

I like it!
Post a comment
The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
peer-to-peer

Esther Schindler
If the comments are ugly, the code is ugly

claird
SVG a graphics format for 21st century

pasmith
Take Chrome OS for a test spin

Sandra Henry-Stocker
Solaris Tip: Have Your Files Changed Since Installation?

sjvn
64-bits of protection?

jfruh
Android fragments vs. the iPhone monolith

mikelgan
What Gizmodo missed about the Pro WX Wireless USB disk drive

 

Where Google Chrome security fails: the password
I heard mention that the Chrome OS will have some sort of encryption available a la bitlocker. If it's possible to encrypt personal data using another password or key, then it may have potential for very secure data.... And Ubuntu has an 'encrypt home directory' option, perhaps google should follow suit.
- Dann

Join the conversation here

The Daily Tip

The Daily TipQuick, practical advice for IT pros. Made fresh daily.

Hot tips:

Want to cash in on your IT savvy? Send your tip to tips@itworld.com. If we post it, we'll send you a $25 Amazon e-gift card.

Newsletters

Subscribe to ITWORLD TODAY and receive the latest IT news and analysis.

I would like to receive offers via email from ITworld partners.
By clicking submit you agree to the terms and conditions outlined in ITworld's privacy policy.
Featured Sponsor

AISO founders envisioned a Web hosting company that was environmentally friendly. While the company employed energy-efficient innovations like solar panels, its infrastructure produced unacceptable power and cooling requirements. Find out how AISO leveraged AMD technology to overcome their challenge in this case study white paper.

In this whitepaper, Scalar explores the opportunity to change the landscape with respect to mission critical databases built around Oracle. Leveraging technologies such as Linux, high-end commodity processing power and Oracle RAC technology to architect, design, build and maintain database infrastructure that delivers maximum availability, reliability and performance at a fraction of traditional cost.

On a typical day, weather.com, the Web site for The Weather Channel in Atlanta, serves up between 15 million and 20 million page views. But in September 2004, when back-to-back hurricanes ransacked Florida, the peak traffic on one day more than tripled: over 70 million page views by more than 7 million unique visitors. Read the full success story now.

Marketplace