Help me design a "modular" function

This is a discussion on Help me design a "modular" function within the C++ Programming forums, part of the General Programming Boards category; Hi again, A part of the functionality I'm putting into my program is comparing two files to check if they ...

  1. #1
    Registered User draugr's Avatar
    Join Date
    Mar 2009
    Posts
    9

    Help me design a "modular" function

    Hi again,

    A part of the functionality I'm putting into my program is comparing two files to check if they are equal. This can be done in a bunch of ways, eg. comparing bit by bit, by checksum, by date+size etc... I'd like to enable the user to choose which of these should be used, and it shouldn't be too hard to put new ways into the program.

    So what would be a good way to do it? There should be one function (or class?) that will delegate the work to the "modules" that actually compare by different ways, depending on which of those the user chooses (a parameter to the function).
    How to implement the modules? I thought maybe an abstract "Comparator" class with a virtual function that can compare two files, and each "way to compare" would subclass it and implement its stuff in that function. But it seems kinda strange to have to create an instance of the comparator just to compare two files and then throw it away.
    A simpler way would be just to write a bunch of functions that the delegate would call, but that doesn't seem to be a "clean" way to do it in an object-oriented language, but so far it seems like the better idea.

    Any tips?

  2. #2
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    I like the Comparator solution, but if you have a simple program that doesn't need all of the different methods of comparison, I would tend to avoid implementing a bunch of stuff that You Aren't Going To Need.

  3. #3
    Registered User draugr's Avatar
    Join Date
    Mar 2009
    Posts
    9
    Thanks, medievalelks

    The different comparison methods will be written for the user, so that he can decide for himself and choose if he needs slow bit by bit comparison or if a quick size+date comparison will do, it's not that I'm implementing them just in case .

    Could you say why would you prefer the classes? Am I wrong thinking that it's wierd having make an instance of a class just to call one method and throw it away?

    Let me elaborate on my dilemma

    Basically, I think I'm going to need to decide on three things:
    • form of the delegate:
      It could be a simple function, like "bool compare_files(file left, file right, int method)".
      Or it could be a class that will have a similar method, but I don't think it needs any data field (might hold the available modules though), so it might as well be static, but then it might as well be just a function...
    • form of the comparing modules:
      Could be just a bunch of functions, like compare_by_size(file left, file right) etc., called by the delegate above as needed.
      Or they could be subclasses of some abstract Comparator interface, as I talked about in the first post
    • some way of knowing which comparison methods are available, so that they can be presented to the user so he can choose:
      if the modules were just functions, there would probably have to be a hardcoded list of the available methods,
      If the modules were classes, perhaps they could register to the delegate (which would be a class in this case) that will hold instances of the different comparators and will be able to list them... this actually doesn't sound bad, altough probably much more complicated than the "bunch of functions" approach


    Perhaps someone will think of a better way than any of those I thought of. In any case, I'd love to hear some arguments for your suggested method.

  4. #4
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    In my opinion, and others may differ, if you are talking about a closed program that you are writing and maintaining, I wouldn't overarchitect it - do what you need now and refactor later if/when you need it.

    If you are designing a library that will be used by others, I'd tend to adhere to the Open-Closed Principle (open for extension, closed for modification). Expose some kind of Comparator interface, and maybe provide a concrete default implementation or two (e.g. ByteComparator), and then let users of the library extend it with their own concrete implementations.

    Am I wrong thinking that it's wierd having make an instance of a class just to call one method and throw it away?
    We do that all the time with strings, vectors, streams, and myriad other objects more complex than a simple file comparator. Objects are created, used, and destroyed. That's how they roll :-).
    Last edited by medievalelks; 03-21-2009 at 01:28 PM.

  5. #5
    Registered User draugr's Avatar
    Join Date
    Mar 2009
    Posts
    9
    Quote Originally Posted by medievalelks View Post
    In my opinion, and others may differ, if you are talking about a closed program that you are writing and maintaining, I wouldn't overarchitect it - do what you need now and refactor later if/when you need it.
    It is just a program, not a library, but I do intend to put it on my website with the code, so I'd like to use good practices and not have to be ashamed of it . What you say sounds reasonable though, the "bunch of functions" approach is crude, but straightforward and understandable, the other solution with classes and stuff is more elegant, but perhaps unnecessarily complex.
    But still I'm not completely decided. Perhaps I'll try both and see which one I like better .

    Quote Originally Posted by medievalelks View Post
    We do that with std::string (and myriad other objects more complex than a simple file comparator) all the time. Objects are created, used, and destroyed. That's how they roll :-).
    I guess you're right

    Thanks for your opinions.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 09:28 AM
  2. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 07:54 PM
  3. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 12:45 PM
  4. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  5. Question..
    By pode in forum Windows Programming
    Replies: 12
    Last Post: 12-19-2004, 07:05 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21