N
Nephi Immortal
I create ten digits. Ten digits are stored into one array with ten
elements. They are the English number symbols. I want to translate
them into international number symbols into other arrays.
Notice global variable – const rgDigits[ 10 ]. It is fine, but each
array can have the choice to support read only, write only, or read/
write only.
I create two classes. First class is read only and second class is
write only. It is more flexible because suppose you have ten read
only arrays and two write only arrays such as “customize”.
Attempt to use const_cast keyword is bad idea because it might lead
to undefined behavior.
Both classes are the replacement of parameter objects which I mean
that I don’t need to put ten arguments or perhaps twenty arguments
into one constructor function. It should be called Parameter Object
Design Pattern, but I was unable to find good example code. Builder
Pattern Design is very similar.
I call it as Read / Write Object Design Pattern. Please let me know
if my code looks neat.
class Digits_Read_Only;
class Digits_Write_Only
{
private:
friend class Digits_Read_Only;
enum EDigits
{
eZero,
eOne,
eTwo,
eThree,
eFour,
eFive,
eSix,
eSeven,
eEight,
eNine
};
char data[ 10 ];
public:
Digits_Write_Only()
{
set_zero();
set_one();
set_two();
set_three();
set_four();
set_five();
set_six();
set_seven();
set_eight();
set_nine();
}
Digits_Write_Only& set_zero( char zero = 0x30 )
{
data[ eZero ] = zero;
return *this;
}
Digits_Write_Only& set_one( char one = 0x31 )
{
data[ eOne ] = one;
return *this;
}
Digits_Write_Only& set_two( char two = 0x32 )
{
data[ eTwo ] = two;
return *this;
}
Digits_Write_Only& set_three( char three = 0x33 )
{
data[ eThree ] = three;
return *this;
}
Digits_Write_Only& set_four( char four = 0x34 )
{
data[ eFour ] = four;
return *this;
}
Digits_Write_Only& set_five( char five = 0x35 )
{
data[ eFive ] = five;
return *this;
}
Digits_Write_Only& set_six( char six = 0x36 )
{
data[ eSix ] = six;
return *this;
}
Digits_Write_Only& set_seven( char seven = 0x37 )
{
data[ eSeven ] = seven;
return *this;
}
Digits_Write_Only& set_eight( char eight = 0x38 )
{
data[ eEight ] = eight;
return *this;
}
Digits_Write_Only& set_nine( char nine = 0x39 )
{
data[ eNine ] = nine;
return *this;
}
};
class Digits_Read_Only
{
private:
const Digits_Write_Only& _digits;
public:
Digits_Read_Only( const Digits_Write_Only& digits ) :
_digits( digits )
{
}
char get_zero() const
{
return _digits.data[ Digits_Write_Only::eZero ];
}
char get_one() const
{
return _digits.data[ Digits_Write_Only::eOne ];
}
char get_two() const
{
return _digits.data[ Digits_Write_Only::eTwo ];
}
char get_three() const
{
return _digits.data[ Digits_Write_Only::eThree ];
}
char get_four() const
{
return _digits.data[ Digits_Write_Only::eFour ];
}
char get_five() const
{
return _digits.data[ Digits_Write_Only::eFive ];
}
char get_six() const
{
return _digits.data[ Digits_Write_Only::eSix ];
}
char get_seven() const
{
return _digits.data[ Digits_Write_Only::eSeven ];
}
char get_eight() const
{
return _digits.data[ Digits_Write_Only::eEight ];
}
char get_nine() const
{
return _digits.data[ Digits_Write_Only::eNine ];
}
char operator[]( int index ) const
{
if( Digits_Write_Only::eZero <= index && index <=
Digits_Write_Only::eNine )
return _digits.data[ index ];
else
return 0xFF; // invalid index
}
int begin() const
{
return Digits_Write_Only::eZero;
}
int end() const
{
return Digits_Write_Only::eNine;
}
};
Digits_Write_Only initialize_digits;
const Digits_Read_Only digits( initialize_digits );
const char rgDigits[ 10 ] =
{
0xB0, 0xB1, 0xB2, 0xB3, 0xB4, 0xB5, 0xB6, 0xB7, 0xB8, 0xB9
};
int main()
{
char data[ 10 ];
for( int i = digits.begin() ; i <= digits.end(); ++i )
data[ i ] = digits[ i ];
for( int i = 0; i <= 9; ++i )
data[ i ] = rgDigits[ i ];
return 0;
}
elements. They are the English number symbols. I want to translate
them into international number symbols into other arrays.
Notice global variable – const rgDigits[ 10 ]. It is fine, but each
array can have the choice to support read only, write only, or read/
write only.
I create two classes. First class is read only and second class is
write only. It is more flexible because suppose you have ten read
only arrays and two write only arrays such as “customize”.
Attempt to use const_cast keyword is bad idea because it might lead
to undefined behavior.
Both classes are the replacement of parameter objects which I mean
that I don’t need to put ten arguments or perhaps twenty arguments
into one constructor function. It should be called Parameter Object
Design Pattern, but I was unable to find good example code. Builder
Pattern Design is very similar.
I call it as Read / Write Object Design Pattern. Please let me know
if my code looks neat.
class Digits_Read_Only;
class Digits_Write_Only
{
private:
friend class Digits_Read_Only;
enum EDigits
{
eZero,
eOne,
eTwo,
eThree,
eFour,
eFive,
eSix,
eSeven,
eEight,
eNine
};
char data[ 10 ];
public:
Digits_Write_Only()
{
set_zero();
set_one();
set_two();
set_three();
set_four();
set_five();
set_six();
set_seven();
set_eight();
set_nine();
}
Digits_Write_Only& set_zero( char zero = 0x30 )
{
data[ eZero ] = zero;
return *this;
}
Digits_Write_Only& set_one( char one = 0x31 )
{
data[ eOne ] = one;
return *this;
}
Digits_Write_Only& set_two( char two = 0x32 )
{
data[ eTwo ] = two;
return *this;
}
Digits_Write_Only& set_three( char three = 0x33 )
{
data[ eThree ] = three;
return *this;
}
Digits_Write_Only& set_four( char four = 0x34 )
{
data[ eFour ] = four;
return *this;
}
Digits_Write_Only& set_five( char five = 0x35 )
{
data[ eFive ] = five;
return *this;
}
Digits_Write_Only& set_six( char six = 0x36 )
{
data[ eSix ] = six;
return *this;
}
Digits_Write_Only& set_seven( char seven = 0x37 )
{
data[ eSeven ] = seven;
return *this;
}
Digits_Write_Only& set_eight( char eight = 0x38 )
{
data[ eEight ] = eight;
return *this;
}
Digits_Write_Only& set_nine( char nine = 0x39 )
{
data[ eNine ] = nine;
return *this;
}
};
class Digits_Read_Only
{
private:
const Digits_Write_Only& _digits;
public:
Digits_Read_Only( const Digits_Write_Only& digits ) :
_digits( digits )
{
}
char get_zero() const
{
return _digits.data[ Digits_Write_Only::eZero ];
}
char get_one() const
{
return _digits.data[ Digits_Write_Only::eOne ];
}
char get_two() const
{
return _digits.data[ Digits_Write_Only::eTwo ];
}
char get_three() const
{
return _digits.data[ Digits_Write_Only::eThree ];
}
char get_four() const
{
return _digits.data[ Digits_Write_Only::eFour ];
}
char get_five() const
{
return _digits.data[ Digits_Write_Only::eFive ];
}
char get_six() const
{
return _digits.data[ Digits_Write_Only::eSix ];
}
char get_seven() const
{
return _digits.data[ Digits_Write_Only::eSeven ];
}
char get_eight() const
{
return _digits.data[ Digits_Write_Only::eEight ];
}
char get_nine() const
{
return _digits.data[ Digits_Write_Only::eNine ];
}
char operator[]( int index ) const
{
if( Digits_Write_Only::eZero <= index && index <=
Digits_Write_Only::eNine )
return _digits.data[ index ];
else
return 0xFF; // invalid index
}
int begin() const
{
return Digits_Write_Only::eZero;
}
int end() const
{
return Digits_Write_Only::eNine;
}
};
Digits_Write_Only initialize_digits;
const Digits_Read_Only digits( initialize_digits );
const char rgDigits[ 10 ] =
{
0xB0, 0xB1, 0xB2, 0xB3, 0xB4, 0xB5, 0xB6, 0xB7, 0xB8, 0xB9
};
int main()
{
char data[ 10 ];
for( int i = digits.begin() ; i <= digits.end(); ++i )
data[ i ] = digits[ i ];
for( int i = 0; i <= 9; ++i )
data[ i ] = rgDigits[ i ];
return 0;
}