图-3 BIT框架结构
4 BITs重用研究案例
一个二分查找的 BIT对象的如标记B所示, 该BIT对象分为两部分:一般的方法(成员函数)和 BIT方法(函数)。该BIT对象中通过内建一个测试用例来说明BIT s方法。
在常态模式下,标记B中普通的方法被调用:BITsBinarySearch::BinarySearch(int Key, int DataSet[10])。
在测试模式下,内嵌的BIT成分通过调用 BITsBinarySearch::BIT1()被重用.
Class BITsBinarySearch {
///////////////////////////////////////////////////////////////////////////// //
Interface
///////////////////////////////////////////////////////////////////////////// //
Member functions
BITsBinarySearch(); // The constructor
~BITsBinarySearch(); // The destructor
int BinarySearch (int Key; int DataSet[10]); // The conventional object
void BIT1(); // The built-in-tests 1...3
///////////////////////////////////////////////////////////////////////////
// Implementation
///////////////////////////////////////////////////////////////////////////
// ================================
// Part 1: The conventional function code
// ================================
int BinarySearch (int Key, int DataSet[10])
{
// The conventional object
// Assume: DataSet is ordered
// LastElement -FirstElement >=0
// and FirstElement >=0
// Input: Key to be found in the DataSet
// Output: TestElemIndex
Private:
int bott, top, i;
int found;
found = false;
Bott = 1;
Top = ArraySize (DataSet); // The last element in DataSet
while (bott <= top) && (not found)
{
i = floor ((bott + top)/2));
if DataSet[i] == Key
Found = true;
else if DataSet[i] < Key
Bott = i +1
else Top = i +1;
}
if found == true
return i; // The index of the element
else return 0; // An indicator of not existence
}
// ==================================
// Part 2: The BITs
// ==================================
// BIT case 1
// ---------------------------------------------------------
void BIT1()
{
// BIT case 1: Array size of 1, key in array
private:
int DataSet[1] = {16};
int Key = 16;
int StdElemIndex = 1;
int TestElemIndex;
char TestResult1[5];
// Test implementation
TestElemIndex = BinarySearch (Key, DataSet);
// Test analysis
cout << “StdElemIndex1 = ” << StdElemIndex << “\n”;
cout << “TestElemIndex1 = ” << TestElemIndex << “\n”;
if TestElemIndex == StdElemIndex
TestResult1 = “OK”;
else TestResult1 = “FALSE”;
cout << “TestResult1: ” << TestResult1 << “\n”;
}
}