Thư viện tri thức trực tuyến
Kho tài liệu với 50,000+ tài liệu học thuật
© 2023 Siêu thị PDF - Kho tài liệu học thuật hàng đầu Việt Nam

CRC.Press A Guide to MATLAB Object Oriented Programming May.2007 Episode 1 Part 7 potx
Nội dung xem thử
Mô tả chi tiết
94 A Guide to MATLAB Object-Oriented Programming
6.5 INDEPENDENT INVESTIGATIONS
1. In the ‘-full’ case, use class to inspect and assign the field’s type. What are you
going to do for object arrays?
2. Modify display.m to take advantage of fieldnames.
C911X_C006.fm Page 94 Thursday, March 1, 2007 2:09 PM
95
7 struct.m
In the previous chapter, we patched a hole in MATLAB’s default encapsulation by tailoring
fieldnames. In this chapter, we patch another hole by tailoring struct. As we have already
seen, the built-in version of struct returns the names and values of private member variables.
In fact, struct’s default behavior represents a risky situation because clients can use it to gain
access to private data. We need to eliminate this possibility by developing a type-specific version
of struct.m.
As a bonus, a standard function that returns an object’s public structure is broadly useful. For
example, look back at the scalar case of the tailored version of display. The strategy of allowing
MATLAB to perform most of the formatting requires a structure of public variables. At that time,
public structure generation was coded directly in line and we could not easily make use of it in
developer view. Further proliferation of in-line structure-generation code makes class extensions
very tedious and error prone. Consolidating structure generation into one function makes a lot of
sense. We can even take advantage of the tailored version of fieldnames so that even public
names are not directly coded into struct.
7.1 STRUCT
While we could easily write a specific get function for this task (e.g., getPublicStructure),
MATLAB already defines a suitable function. The built-in version of struct already returns a
structure associated with the object. The built-in version will also operate on objects. Unlike the help
for fieldnames, help struct does not promise to return a structure of “public data fields.”
The help files for struct describe a function that converts an object into its equivalent
structure. Here our idea of “equivalent structure” and MATLAB’s idea are definitely not the same.
In our world, the structure should contain public member variables; however, the built-in version
of struct returns a structure made up of the private variables. The fieldnames function was
bad enough, but the struct function is even more despicable. It returns both the names and the
values of the private variables!
While it is still true that MATLAB prevents changes to the private data, that does not prevent clients
from using the values and even passing around the structures created from the object. Here is yet another
place where MATLAB seems very lax about preventing client access to private data. We need to
reinforce this area because the potential result of such permissiveness can be devastating. If left
unchecked, clients will use this back door to destroy most of the benefits of encapsulation. Once a
client ties code to the private structure returned by the default version of struct, later attempts to
reorganize or improve the class will result in broken code and angry clients. While it might indeed be
their fault, it becomes our problem. I have personally witnessed such chaos and can tell you it is no
easy chore to clean it up. Preventing it from happening in the first place is a much better plan.
Like always, if we don’t like what the built-in version gives us, we simply tailor the function
to suit our purposes. In this particular case, tailoring is not perfect because we can’t prevent clever
or devious clients from using builtin to tie their code to an object’s private data. A client can
bypass all our carefully crafted member functions by using, for example,
shape_struct = builtin(‘struct’, shape)
When a client uses builtin in this way, shape_struct is not an object, but rather a structure.
With a structure, the client loses the ability to call member functions, but in return gains the ability
C911X_C007.fm Page 95 Thursday, March 1, 2007 2:15 PM