Created
June 22, 2014 12:25
-
-
Save cogani/d14e0f81765ceee62312 to your computer and use it in GitHub Desktop.
Trancoder example - Java Flavour
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package Transcoding; | |
public class ASCIIDecimalCodeTranscoderStrategy implements TranscoderStrategy { | |
@Override | |
public String transcode(String message) { | |
return (message + ": to ASCII Code "); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package Transcoding; | |
public class BrailleTranscoderStrategy implements TranscoderStrategy { | |
@Override | |
public String transcode(String message) { | |
return (message+": to Brailling "); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package Transcoding; | |
public class EncryptTranscoderStrategy implements TranscoderStrategy { | |
@Override | |
public String transcode(String message) { | |
return (message+": to Encrypt code "); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package Transcoding; | |
public class Main { | |
public static void main(String[] args) { | |
TranscoderStrategy transcoderStrategies[] = { | |
new BrailleTranscoderStrategy(), | |
new EncryptTranscoderStrategy(), | |
new MorseTranscoderStrategyStrategy()}; | |
TranscoderStrategy asciiDecimalCodeTranscoderStrategy = new ASCIIDecimalCodeTranscoderStrategy(); | |
TranscodingTextService transcodingTextService = new TranscodingTextService(asciiDecimalCodeTranscoderStrategy); | |
String message = "Meessage to translate"; | |
System.out.println("TranscodingTextService is using the behavior injected by constructor...\n\t"+ transcodingTextService.execute(message)); | |
System.out.println("\nTranscodingTextService can change its behavior dinamicaly by setter..."); | |
for (int i = 0; i < transcoderStrategies.length; i++) { | |
transcodingTextService.setTranscoderStrategy(transcoderStrategies[i]); | |
String result = transcodingTextService.execute(message); | |
System.out.println("\t" + result); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package Transcoding; | |
public class MorseTranscoderStrategyStrategy implements TranscoderStrategy { | |
@Override | |
public String transcode(String message) { | |
return (message + ": to Morse Code "); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package Transcoding; | |
public interface TranscoderStrategy { | |
public String transcode(String source); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package Transcoding; | |
public class TranscodingTextService { | |
private TranscoderStrategy transcoderStrategy; | |
public TranscodingTextService(TranscoderStrategy transcoderStrategy) { | |
this.transcoderStrategy = transcoderStrategy; | |
} | |
public String execute(String message) { | |
return transcoderStrategy.transcode(message); | |
} | |
public void setTranscoderStrategy(TranscoderStrategy transcoderStrategy) { | |
this.transcoderStrategy = transcoderStrategy; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment