Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I need to create a string using the following algorithm:

  1. Generate a GUID as a byte array.
  2. Convert that GUID to a string.
  3. From this string, get the UTF-8 encoding as a byte array.
  4. From this byte array, hash those bytes to another byte array.
  5. From this byte array, get the base64 encoding as a string.
  6. Append a string constant to this string.

What design pattern(s) would I use? I am thinking builder and/or chain-of-responsibility, although I am not sure I would handle the changing between string and byte array types.

UPDATE:

I am programming in an object-oriented language (C++, Java, C#).

The encoding of UTF-8 may change in the requirements, as well as the hashing algorithm in step 4. I am asking about design patterns to adapt for a change in requirements.

share|improve this question
2  
Given that Design Patterns as such are for Object Oriented languages generally (and not say, Functional or Procedural), what language are you doing this in? –  World Engineer Jun 13 '13 at 21:20
8  
Design patterns are often overused, and in this case you definitely do not need one. Just split things into small functions, keep single responsibility principle in mind. –  Job Jun 13 '13 at 21:29
2  
Why do you think you need a pattern here? –  Andres F. Jun 13 '13 at 21:35
4  
I think the arbitrary-obtusification design pattern applies here O_o whatever you're trying to do, I'd be surprised if your approach is really optimal, maybe you should ask on here about a good solution to your given problem rather than a pattern to implement the solution you came up with –  Jimmy Hoffa Jun 13 '13 at 21:40
    
This kind of problem is not what design patterns are for. –  Darth Satan Jun 13 '13 at 22:14

4 Answers 4

up vote 11 down vote accepted

That seems simple enough to me that you don't really need a design pattern, per say. You're got 6 steps, each of which requires only a single line of execution (except Line 4, as that could be a complex hash).

Chain of Command is more of a fire-and-forget method between distinct objects. That's not really the case here. And Builder is more of using a single execution path to perform the same action on multiple types of objects. Again in your case, it's a single, consistent path, so I wouldn't think it'd fit either.

share|improve this answer
1  
+1 If you don't need to be able to place input or extract output at any step but the first and last, or exchange one of those steps, why complicate things. SRP holds, as your goal is to generate a random formulated string. –  HorusKol Jun 13 '13 at 22:34

The class which does all this would be a Factory with a function generate().

When the hashing algorithm and the character encoding have to be exchangeable, you would do so by implementing these as Strategies.

These strategies could either be passed to the constructor of the factory, you could have different sub-classes of factories which use different sets of strategies (Abstract Factory pattern), you could pass the strategies to each call of the generate function or you could set the strategies to use for all subsequent calls through setters of the factory (Builder pattern). Which one of these option to use (or maybe a mix of them) is a matter of taste.

share|improve this answer

I am asking about design patterns to adapt for a change in requirements.

Don't think about design patterns to adapt for a change in requirements

Use design patterns to simplify code that you have written to meet known requirements. Attempting to predict future requirements is only slightly easier than predicting stock prices.

share|improve this answer

This seems like typical use-case for a template method pattern; have a class which has 7 methods, 6 for each corresponding step and one method that calls all the other 6 methods in order. The methods that can change make them abstract and come with implementation for them in subclasses; for example make subclasses for UTF-8 and for other encoding; the same for other methods that may very. In a simplified way:

public abstract class StringProcessor {
    public void perform() {
        generateGuid();
        generateEncoding();
        //...
    }

    protected void generateGuid() {
        //logic
    }

    protected abstract void generateEncoding();
}

public class Utf8EncodingStringProcessor extends StringProcessor {
    protected generateEncoding() {
         //UTF-8 logic
    }
}

public class OtherEncodingStringProcessor extends StringProcessor {
    protected generateEncoding() {
         //Other logic
    }
}

This would not be so useful if you need UTF-8 with hashing algorithm 1, UTF-8 with hasing algorithm 2, other encoding with hashing algorithm 1, other encoding with hashing algorithm 2 etc. If you need that much flexibility I would recommend using template method pattern combined with strategy pattern for each method that may vary. Something simplified might look:

public abstract class StringProcessor {
    private Encoding encoding;   
    private Hashing hashing;

    public StringProcessor(EncodingType encodingType, HashingType hashingType) {
         if (UTF8.equals(encodingType)) {
              encoding = new Utf8Encoding();
         } else if( ....)
    }

    public void perform() {
        generateGuid();
        encoding.encode();
        hashing.generateHash();
        //...
    }

    public void generateGuid() {
        //logic
    }    
}

public enum EncodingType {
    UTF8, OTHER, //...
}

public interface Encoding {
    void encode();
}    

public class Utf8Encoding implements Encoding {
    public void encode() {
         //UTF-8 logic
    }
}

Builder and chain of responsibility do not seem to work since builder lets you create only a partial object, but you need to call all the methods; also chain of responsibility is not useful since you always will need to delegate upper in the hierarchy and always reach the top in command to in order to call all the methods.

share|improve this answer
4  
That reads a lot like an over-engineered approach to something that should be just 6 lines of code. Can building it in this way be justified? –  Darth Satan Jun 13 '13 at 23:03
    
@mh01 Yes - flexibility; that's the whole point of OOP. Otherwise it can be written with 6 lines of code and add tons of if-elses when requirements change. The question was not how to write it in a concise/easy to understand way; the asker already knows that. The question was how to build it in a flexible way in order to properly accommodate changing requirements, preferably using design patterns. This is useful only if it is known that requirements are to be changed; if only one implementation for each algorithm is needed, than I agree it's over-engineering but then this question is useless. –  m3th0dman Jun 13 '13 at 23:12
2  
He is asking if that flexibility is worth the tradeoffs you are making. Unless the requirements are going to change weekly, or something, it is not. You can also write 30 classes for the task of printing hello world, there is no end to accidental complexity you can add. –  Esailija Jun 14 '13 at 4:28
1  
@Esailija With all respect, but you do not seem to understand OOP design. –  m3th0dman Jun 14 '13 at 7:12
1  
@m3th0dman I do think your answer, while not what I would do, is still a valid answer, but Esailija has valid concerns about code verbosity. If you disagree with his reasoning then question his reasoning, but please refrain from belittling others. –  maple_shaft Jun 14 '13 at 11:06

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.