View Single Post
  #4 (permalink)  
Old 02-16-2012, 02:00 PM
Nephi Immortal
Guest
 
Posts: n/a
Default Re: Read / Write Pattern Design

On Feb 16, 12:25*am, "Alf P. Steinbach" <alf.p.steinbach
+use...@gmail.com> wrote:
> On 16.02.2012 06:05, Nephi Immortal wrote:
> [snip intro]
>
>
>
>
>
> > * *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;

>
> > }

>
> Regarding whether your code looks "neat", well it isn't untidy, but on
> the other hand "neat" is associated with an elegant or surprisingly
> simple solution of something.
>
> And I can't see that your code solves any problem?


Why not? Please provide me parameter object pattern design example
code.
Reply With Quote