CSC/ECE 517 Fall 2007/wiki1 8 s1

From Expertiza_Wiki
Revision as of 19:12, 12 September 2007 by Mbsimmon (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

The extend method in Ruby is designed to mix in methods of a module to a class at the class level. The term ‘class level’ is synonymous with the concept of static in other popular object oriented languages such as Java or C# where static behaviors and attributes exist independently of any instance of the class.


Module

Before presenting an example of the extend method, it is necessary to briefly define modules and introduce a small example.

A module is a collection of functionality that cannot be used to create objects. Alone it does not define a type. It exists to group functionality that will be used by objects either at the implementation level or the class level, depending on the mechanism by which it was added.

Below is an example of a module in Ruby. Its purpose is to group benefit calculation functionality for procedures performed at a general treatment facility. It has been simplified for illustration purposes – in practice benefit calculation is immensely complicated.


# module for encapsulating benefit calculation functionality
module BenefitCalculation
def getPatientCharge
puts “getPatientCharge() from BenefitCalculation”
end


def getCopay
puts “getCopay() from BenefitCalculation”
end
end


Extend Example

The most effective way to illustrate the use of extending a module is through an example, so consider a full-service treatment center (like a hospital) that provides medical, dental, optical and homeopathic services. A software solution developed to support this type of organization may consist of four modules to encapsulate the functionality specific to their services, but surely there would be functionality that would be common to all services in a base class.

A possible design of the high-level software architecture follows:


# Service is the base class for our system.
class Service
def getAddress
puts “getAddress in Service”
end
end


class DentalService < Service
# Added functionality for dental services.
end


class MedicalService < Service
# Added functionality for medical services.
end


class OpticalService < Service
# Added functionality for optical services.
end


class HomeopathicService < Service
# Added functionality for homeopathic services.
end