Building a Hierarchy

  • Khan A
N/ACitations
Citations of this article
1Readers
Mendeley users who have this article in their library.
Get full text

Abstract

This section focuses on building hierarchies because objects and hierarchies go hand in hand. For example, a hierarchy of shapes might include rectangles, stars, and circles. A hierarchical imple- mentation allows one class to build on functions defined in another class. An object-oriented hierarchy can do this without a lot of rework. Throughout the first section, we simplified much of our code by coercing MATLAB into doing a lot of the work. In a small way, all classes are hierarchical because they build on the built-in types. MATLAB is always at the top of the hierarchy. A deeper hierarchy of classes follows the same philosophy. The lower-level class, sometimes called the child, tries to coerce a higher-level class, the parent, into doing as much as possible. This is the way of a hierarchy: always try to force the next higher level into doing all the work. When a child class coerces a parent to perform an operation, the child is said to inherit that particular function from the parent. There are different flavors of inheritance. Differences depend on how control passes to the parent. A parent–child relationship is what we normally think of as inheritance, but anytime one class passes control to another, this is inheritance. When one class uses another class as a private member variable, this too is inheritance. Called composition or aggrega- tion, using a class as a member variable often works better than parent–child inheritance and is just as powerful. In this section, we will examine both parent–child inheritance and composition. Also in this section, we will find that efficient, bulletproof hierarchies can be coded in MATLAB. Hierarchies are built using both types of inheritance, parent–child and composition. The group-of- eight implementations from Section 1 are already organized to support inheritance. In this section, we will expand on the organization. Recall from the first section how we tailored built-in MATLAB functions like subsref, subsasgn, display, and even mtimes to suit the needs of our classes. In a hierarchy, a child class can accomplish the same trick. This time, the child tailors a function already defined by the parent. The child simply includes a tailored version of the function in its own class directory. In the first section, even when a class redefined a function, we could still call MATLAB’s built-in version using builtin. When a child redefines a parent function, a similar mechanism allows a child to call the parent’s version. We can’t use builtin because that will skip over the parent. By the end of this section, you will be able to churn out bulletproof class implementations based on the reference designs. Soon the novelty will wear off and you will pine for a computer- aided way to create the group-of-eight scaffolding. The CD that accompanies this book includes a very complete MATLAB tool that will build the scaffolding and help you maintain and evolve each class. The last two chapters in this section document and demonstrate the Class Wizard tool. Now in its third version, Class Wizard will rapidly generate core class functions based on lists of private and public variables and functions. These lists are entered using a graphical interface. Once entered, Class Wizard generates group-of-eight functions that include all the special func- tionality discussed throughout this book. Class Wizard is a versatile and extremely powerful tool. It is found on the disk in /utils/wizard_gui, and this directory must be added to the path. The dialog screens in Class Wizard require MATLAB version 7 or greater but will generate classes that work with version 6.5 or greater.

Cite

CITATION STYLE

APA

Khan, A. (2016). Building a Hierarchy. In Jumpstart Tableau (pp. 147–155). Apress. https://doi.org/10.1007/978-1-4842-1934-8_16

Register to see more suggestions

Mendeley helps you to discover research relevant for your work.

Already have an account?

Save time finding and organizing research with Mendeley

Sign up for free