CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Jul 2007
    Posts
    249

    Exclamation Abstraction and Implementation

    Hi,
    I have the a doubt on how what is the benefit on separating the abstraction and implementation.

    The below code
    Code:
    class FileProcessor
    {
    public:
    virtual void processFile();
    };
    
    class DocProcessor : public FileProcessor
    {
    public:
    void processFile();// Implementation is given it will implement to process the doc files
    };
    
    class ExcelProcessor : public FileProcessor
    {
    public:
    void processFile();//Implements to process the excel files
    };
    
    Till now it is fine
    
    suppose DocProcessor and ExcelProcessor uses two big algorithm to process these file types then
    
     I still can have subclasses like
    
    class Algorithm1 : public DocProcessor
    {
    public:
    void algorithm(); //which will be called as per the algorithm class instantiated for to process
    };
    
    class Algorithm2 : public DocProcessor
    {
    public:
    void algorithm(); //which will be called as per the algorithm class instantiated for to process
    };
    
    same for ExcelProcessor class

    Here everything looks fine to me. [ I have not used the bridge pattern to separate the abstraction and implementation]. The only thing i can achieve using bridge is that the number of classes will be reduced if new class like jpgProcessor is introduced or new algorithim is introduced.

    Then why it is recommended that always separate the abstraction and implementation

    please help
    Regards

  2. #2
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: Abstraction and Implementation

    Quote Originally Posted by Rajesh1978 View Post
    Here everything looks fine to me.
    Not to me. Why would Algorithm1 be derived from DocProcessor? Why doesn't Algorithm1 simply take a poiner/reference to a FileProcessor (composition instead of inheritance)?
    Quote Originally Posted by Rajesh1978 View Post
    The only thing i can achieve using bridge is that the number of classes will be reduced if new class like jpgProcessor is introduced or new algorithim is introduced.

    Then why it is recommended that always separate the abstraction and implementation
    You mention a very good reason already. In your design you have (# processors * # algorithms) implementations, each of which needs to be written, tested, documented and maintained. By programming against an abstraction instead of an implementation, you have (#processors + #algorithms) implementations. If you have 4 of each, you already saved yourself half the work!

    Have a look at the following articles:
    http://www.objectmentor.com/resources/articles/ocp.pdf
    http://www.objectmentor.com/resources/articles/lsp.pdf
    http://www.objectmentor.com/resources/articles/dip.pdf
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

  3. #3
    Join Date
    Jul 2007
    Posts
    249

    Re: Abstraction and Implementation

    Thanks.
    I agree.

    i will now make two hierarchy one for FileProcessor and one for Algorithm.

    File processor will use specialization of Algorithm base class.

    now the number of classes will not increase in that rate what i have posted priviously.
    Now it is exactly what the bridge patterns says.Abstraction and implementation is separated.

    GOF says the client code will not change if we extend the implementation code in future.
    This is how ?
    both the abstractions and their implementations should be extensible by subclassing. In this case, the Bridge pattern lets you combine the different abstractions and implementations and extend them independently.
    changes in the implementation of an abstraction should have no impact on clients; that is, their code should not have to be recompiled.
    (C++) you want to hide the implementation of an abstraction completely from clients. In C++ the
    representation of a class is visible in the class interface.
    how we no need to recompile the client code

  4. #4
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: Abstraction and Implementation

    Maybe I'm not following, but why isn't Algorithm just a virtual member of the base class and overridden in the derived classes?

    I agree with D_Drmmr, you're not using inheritance correctly here.

  5. #5
    Join Date
    Jul 2007
    Posts
    249

    Re: Abstraction and Implementation

    The whole point is that.
    If we use virtual function as my first post the problems arise is pointed out by drmmr and also in bridge pattern.

    My doubt is how the client code do not change as per GOF if we extend the application hierarchy?

  6. #6
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: Abstraction and Implementation

    Quote Originally Posted by Rajesh1978 View Post
    My doubt is how the client code do not change as per GOF if we extend the application hierarchy?
    Because the client should only depend on the abstraction. As long as that doesn't change, the client code does not have to be recompiled, regardless of how many implementations we add or change.
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured