DESIGN & IMPLEMENTATION
5.6 Class Hierarchies
It sometimes makes sense to define a new class in terms of existing classes. This enables you to reuse the designs and techniques in a new class that represents a similar entity. You accomplish this reuse by creating a subclass. A subclass defines objects that are a subset of those defined by the super class. A subclass is more specific than its super class and might add new properties, methods, and events to those inherited from the super class.
Mathematical sets can help illustrate the relationships among classes. In the following diagram, the set of Positive Integers is a subset of the set of Integers and a subset of Positive numbers. All three sets are subsets of Real numbers, which is a subset of All Numbers.
The definition of Positive Integers requires the additional specification that members of the set be greater than zero. Positive Integers combine the definitions from both Integers and Positives. The resulting subset is more specific, and therefore more narrowly defined, than the supersets, but still shares all the characteristics that define the supersets.
Fig. Class Hierarchies
The "is a" relationship is a good way to determine if it is appropriate to define a particular subset in terms of existing supersets. For example, each of the following statements makes senses:
(a). A Positive Integer is an Integer
(b). A Positive Integer is a Positive number
If the "is a" relationship holds, then it is likely you can define a new class from a class or classes that represent some more general case.
Reusing Solutions
Classes are usually organized into taxonomies to foster code reuse. For example, if you define a class to implement an interface to the serial port of a computer, it would probably be very similar to a class designed to implement an interface to the parallel port. To reuse code, you could define a super class that contains everything that is common to the two types of ports, and then derive subclasses from the super class in which you implement only what is unique to each specific port. Then the subclasses would inherit all of the common functionality from the super class. Objects
A class is like a template for the creation of a specific instance of the class. This instance or object contains actual data for a particular entity that is represented by the class. For example, an instance of a bank account class is an object that represents a specific bank account, with an actual account number and an actual balance. This object has built into it the ability to perform operations defined by the class, such as making deposits to and withdrawals from the account balance.
Objects are not just passive data containers. Objects actively manage the data contained by allowing only certain operations to be performed, by hiding data that does not need to be public, and by preventing external clients from misusing data by performing operations for which the object was not designed. Objects even control what happens when they are destroyed.
Encapsulating Information
An important aspect of objects is that you can write software that accesses the information stored in the object via its properties and methods without knowing anything about how that information is stored, or even whether it is stored or calculated when queried. The object isolates code that accesses the object from the internal implementation of methods and properties. You can define classes that hide both data and operations from any methods that are not part of the class. You can then implement whatever interface is most appropriate for the intended use. Define a Simple Class
The basic purpose of a class is to define an object that encapsulates data and the operations performed on that data. For example, Basic Class defines a property and two methods that operate on the data in that property:
Value — Property that contains the data stored in an object of the class Sq — Method that square the value of the property.
Here is the definition of Basic Class: classdef clas1
properties x end methods function p=sq(obj) p= obj.x*obj.x end end end
To use the class, create an object of the class, assign the class data, and call operations on that data.when we run above code then result is as follow
Create object of class >> y=clas1
y =clas1
Assign value of property >> y.x=9
y =
clas1
accessing member function of class and passing object as parameter >> sq(y)
p = 81
Testing by passing wrong value assert_equals(82,sq(y))
p = 81
??? Error using ==> mlunit_fail at 34
Data not equal: Expected : 82 Actual : 81 Error in ==> abstract_assert_equals at 115 mlunit_fail(msg); Error in ==> assert_equals at 42
abstract_assert_equals(true, expected, actual, varargin{:});
Creating Test Case for MLUnit test_cl1.m
function self = test_cl1(name) %test_cl1 constructor.
%
% Class Info / Example
% ====================
% The class test_cl1 is the fixture for all tests of the test-driven % cl1. The constructor shall not be called directly, but through % a test runner.
tc = test_case(name);
self = class(struct([]), 'test_cl1', tc); test_v1
function self = test_v1(self)
y=clas1; y.x=9; assert_equals(81,sq(y)) assert_equals(80,sq(y)) Output : 5.7
Overloading Functions
Classes can implement existing functionality, such as addition, by defining a method with the same name as the existing MATLAB function. For example, suppose you want to add two Basic Class object. It makes sense to add the values of the Object Value properties of each object. Here is an overload of the MATLAB plus function. It defines addition for this class as adding the property values: method function r = plus(o1,o2) r = [o1.Value] + [o2.Value]; end 43
end
By implementing a method called plus, you can use the "+" operator with objects of Basic Class. a = Basic Class(pi/3);
b = Basic Class(pi/4); a + b
ans = 1.8326
Class-Based Unit Tests
(a) Author Class-Based Unit Tests in MATLAB (b) Write Simple Test Case Using Classes
(c) Write Setup and Teardown Code Using Classes (d) Tag Unit Tests
(e) Write Tests Using Shared Fixtures (f) Create Basic Custom Fixture (g) Create Advanced Custom Fixture (h) Create Basic Parameterized Test (i) Create Advanced Parameterized Test Run Unit Tests
Run test suites in the testing framework
To run a group of tests in matlab. Unit test, create a test suite from the matlab.unit test. Test Suite class. Test suites can contain:
All tests in a package All tests in a class All tests in a folder A single test method Arrays of test suites
Proposed implementation in Run Unit Tests (a) Create simple test suites
(b) Run Tests for Various Workflows (c) Add Plugin to Test Runner
(d) Write Plugin to Save Diagnostic Details (e) Write plugin to extend test runners (f) Create custom plugin
Analyze Test Results
Use test results to identify failures: (a) Analyze Test Case Results (b) Analyze Failed Test Results