Article 139 - Design Pattern - Decorator Pattern Example

Design patterns were generalized to aid less experienced programmers to learn and adapt theoretical strategies into real world applications. This article goes over a decorator design pattern example.

It is recommended that the reader review the concepts by reviewing the section Decorator Pattern in Article 17 - Technical Interview Questions - Design Patterns.

Implementation

An existing class that we want to decorate.

class StringClass{
    private:
    String * string

    public:
    StringClass()
        : string( 0 ){
        string = newString();

        if( !string ){
            throw 0
        }
    }

    print(){
        snprintf( stdout, string->length, "%s", string->content );
    }
}

One way to decorate the boring class.

class StringUnix: public StringClass{
    public:
    print(){
        StringClass::print();
        snprintf( stdout, 1, "\n" );
    }
}

Another layer to decorate the decorated but still boring class.

class StringWindow: public StringUnix{
    public:
    print(){
        StringUnix::print();
        snprintf( stdout, 1, "\r" );
    }
}

Comments (0)

Post a comment

  • Name:
  • Post:
  • Challenge:

Register or login to post comments easier.


Vantasy World Copyright 2011 - 2017. Vantasy World is a project developed by Vantasy Online. Privacy Policy.