the devmin quest

linux administration-development tips and tricks

Si vous cherchez mon site professionnel, merci de cliquer ici.

Group ownership in object oriented programming


This post discusses the concept of ownership in oriented object programming, and more precisely, how to deal with instances shared by several instances of an other class.

Real world modeling

In oriented-object programming, we easily models concepts against real world things. I can create a Person class, to represent what is the abstraction of “person”. Then I define the person’s attributes, as gender, age, place, nutrition, etc. I continue with defining methods for the persons, like walking and eating.

In a ruby way, this will be something like this :

class Person
  def initialize
    @gender     = 'M'
    @age        = 20
    @place      = 'home'
    @nutrition  = 5
  end  

  def walk_to destination
    @place = destination
  end

def eat
    @nutrition += 1
  end
end

Then, I instantiate one Person, so say : john = Person.new .

ownership

But John like watches. No problem : I create a class Watch and I define a method for John to buy a watch.

Class Person
  def buy_a_watch
    @watch = Watch.new
  end
end

All of this is straightforward in OO programming. There’s a class for persons, there’s a class for watches, and one particular person has one particular watch, so I can simply create the Watch within the Person class, in an instance method.
We can also imagine things that will be shared by everyone, like the date. I create the class World_date, and I instantiate it as singleton in a global variable. Any particular person can access it through this global variable.

group ownership

But now, how to deal with class with many objects instantiated that are, nevertheless, shared by groups of objects, as the weather, or TV programs?

With things as political_context, it will be easier. I may create a State class, with a @political_context attributes, and Persons will be created and manipulated inside a State instantiated object.

But will I say that weather includes persons (will I instantiate Persons in the Weather objects)? The weather is common to many people of the same area, but we can’t say that people are within weather. And this is even more difficult for we will have to consider that they may be in Weather and in TV_Program.

I could let the weather and programs in global scope. But will I have as many global variables as there are TV programs? And even if I put those in a global array, how will a person create a new TV program? This would be an instance method acting on the global focus…

suggestion

The problem I want to point is : where to put “instance = AClass.new” when instance is shared between many objects of an other class?

To solve this, I took the habit of creating such shared objects in a hash as class variable.

class Weather
   def initialize( area )
     @clouds           = 10
     @humidity         = 5
     @@objects = Hash.new unless defined? @@objects
     @@objects[ area ] = self
   end

   # class methods
   def self.list
     @@objects.each_key { |key| puts key }
   end

   def self.objects
       @@objects
   end
end

Then, you create Weather.new “Paris”, Weather.new “London” and whatever you want from where you want, without needing to store it in a variable.

This way, you can easily access it in any method of the instances john or william simply like this :

Weather.objects[ @place ]
Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Technorati






Write a Comment

Note: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>