Advertisement
Guest User

Untitled

a guest
Aug 26th, 2016
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.41 KB | None | 0 0
  1. // 1 - encapsulate xml marshalling in a class
  2. byte[] asXml(Invoice invoice) {
  3. ByteArrayOutputStream outStream = new ByteArrayOutputStream();
  4. JaxbMarshaller jaxbMarshaller = new JaxbMarshaller(Invoice.class);
  5. jaxbMarshaller.marshallObject(invoice, outStream);
  6. byte[] resultXml = outStream.toByteArray();
  7. IOUtils.closeQuietly(outStream);
  8. return resultXml;
  9. }
  10.  
  11.  
  12.  
  13. // 2 - refactor to iliminate all null checks and branching
  14. class AmazonS3File {
  15.  
  16. private final String path;
  17. private Metadata metadata;
  18.  
  19. void setMetadata(@Nullable Metadata metadata) {
  20. this.metadata = metadata;
  21. }
  22.  
  23. @Nullable
  24. Metadata getMetadata() {…}
  25. }
  26.  
  27. class AmazonS3Bucket {
  28. void upload(AmazonS3File file) {
  29. … upload ...
  30. if (file.getMetadata() != null) {
  31. uploadMetadata(file.getMetadata());
  32. }
  33. }
  34. }
  35.  
  36.  
  37.  
  38. // 3 - refactor to conform to command-query separation
  39. class Registration {
  40.  
  41. private final RegistrationForm form;
  42.  
  43. Optional<ValidationErrors> complete() {
  44. if (!form.isNotValid()) {
  45. … complete registration ...
  46. } else {
  47. return form.validationErrors();
  48. }
  49. }
  50. }
  51.  
  52.  
  53.  
  54. // 4 - does the method violate the principle of least astonishment? why?
  55. Ban ban(User user) {
  56. Ban ban = new Ban(user.ipAddress(), Period.ofDays(3));
  57. user.banned();
  58. return ban;
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement