Question: question regarding Structural and Behavioural Design Patterns in Java. Below is my code, and I would like to apply thru the practical application the following
question regarding Structural and Behavioural Design Patterns in Java.
Below is my code, and I would like to apply thru the practical application the following design patterns/strategies: Adapter, Observer and Proxy.
package com.algonquin.loggy;
import java.util.Date; import java.util.Random; import java.util.UUID;
public abstract class Log implements Annotable { private String name; private String description; private Date date; private UUID uuid; private String code; private File attachment;
public File getAttachment() { return attachment; }
public void setAttachment(File attachment) { this.attachment = attachment; }
public void create() { System.out.println("Log record for " + uuid + " has been created"); }
public void read() { System.out.println("Log " + uuid + " name:" + name + ", description: " + description + " created on " + date); }
public void update(String name, String description) { System.out.println("Log record for " + uuid + " has been updated"); this.name = name; this.description = description; }
public void delete() { System.out.println("Log record for " + uuid + " has been deleted"); }
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public String getDescription() { return description; }
public void setDescription(String description) { this.description = description; }
public UUID getUuid() { return uuid; }
public void setUuid(UUID uuid) { this.uuid = uuid; }
public Date getDate() { return date; }
public void setDate(Date date) { this.date = date; } private Log(LogBuilder builder) { //builder implementation
this.name = builder.name;
this.description = builder.description;
this.date = builder.date;
this.uuid = builder.uuid;
this.code = builder.code;
this.attachment = builder.attachment;
} public static class LogBuilder {
private String name;
private String description;
private Date date;
private UUID uuid;
private String code;
private File attachment;
public LogBuilder(String name, String description, Date date, UUID uuid, String code, File attachment) {
super();
this.name = name;
this.description = description;
this.date = date;
this.uuid = uuid;
this.code = code;
this.attachment = attachment;
}
public LogBuilder LogBuilder() {
return null;
}
}
public Log() { this.name = null; this.description = null; this.date = null; this.uuid = null; this.code = null; }
public Log(String name) { this(name, ""); }
public Log(String name, String description) { this(name, description, new Date()); }
public Log(String name, String description, Date date) { this.name = name; this.description = description; this.date = date; this.uuid = UUID.randomUUID(); Singleton s=Singleton.getInstance(); this.code = s.shortCode(); }
public String toString() { String out = code + ":" + name + ":" + description + ":" + date; if (this.attachment != null) { out += " with attachment " + this.attachment.getName(); } return out; }
@Override public void attachFile(String name, String type, String content, Long size) throws Exception { if (!isValidContentType(type)) { System.out.println("ContentType " + type + " can not be attached"); throw new Exception("ContentType " + type + " can not be attached"); } }
@Override public boolean isValidContentType(String type) { return false; }
private String shortCode() { return randomChars(3) + "-" + randomChars(3) + "-" + randomChars(3); }
private String randomChars(int n) { String randomchars = ""; String chars = "abcdefghijklmnopqrstuvwxyz1234567890"; Random rnd = new Random(); for (int i = 0; i < n; i++) { randomchars += chars.charAt(rnd.nextInt(chars.length())); } return randomchars; } }
------
package com.algonquin.loggy;
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
Log log = null;
Log.LogBuilder logbuilder = new Log.LogBuilder(null, null, null, null, null, null).LogBuilder();
System.out.println("Log builder config::"+logbuilder);
ArrayList aList = new ArrayList<>();
aList.add(new Factory("first log", "Monday May 3, I had to wake up early",
"image.tif", "TIF", "******", Long.valueOf(1024)));
aList.add(new Factory("fourth log", "And even later, I need to have lunch",
"eating.png", "PNG", "******", Long.valueOf(1024)));
aList.add(new Factory("first log", "Monday May 4, I had to wake up early",
"image.png", "PNG", "******", Long.valueOf(1024)));
aList.add(new Factory("second log", "Few minutes later, I had my first cup of coffee",
"coffee.png", "PNG", "******", Long.valueOf(1024)));
aList.add(new Factory("third log", "Few minutes later, I am going for a ride",
"biking1.mp4", "MP4", "******", Long.valueOf(1024)));
aList.add(new Factory("fourth log", "And even later, I need to have lunch",
"recipie.txt", "TXT", "******", Long.valueOf(1024)));
for (int ctr = 0; ctr < aList.size(); ++ctr) {
try {
mockLog(aList.get(ctr).getLog(), aList.get(ctr).getFile());
}
catch (Exception e) {
System.out.println("Exception: " + e.getMessage() + " ");
// e.printStackTrace();
}
}
}
private static void mockLog(Log log, File file) throws Exception {
System.out.println(">>> New log (" + log.toString() + ")");
log.setAttachment(file);
log.create();
System.out.println(" ");
}
}
------------------
package com.algonquin.loggy;
public class File implements Processable {
private String name;
private String type;
private String content;
private Long size;
public File() {
}
public File(String name, String type, String content, Long size) {
this.name = name;
this.type = type;
this.content = content;
this.size = size;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public Long getSize() {
return size;
}
public void setSize(Long size) {
this.size = size;
}
@Override
public void postProcess() {
System.out.println("Default post processing");
}
}
-------------------
package com.algonquin.loggy;
public interface Annotable {
public void attachFile(String name, String type, String content, Long size) throws Exception;
public boolean isValidContentType(String contentType);
}
----------------
/** * */ package com.algonquin.loggy;
public class TextFile extends File {
/** * */ public TextFile() { }
/** * @param name * @param type * @param content * @param size */ public TextFile(String name, String type, String content, Long size) { super(name, type, content, size); }
/** * */ @Override public void postProcess() { super.postProcess(); // trigger translation System.out.println("This text file is going to be translated"); }
}
Step by Step Solution
There are 3 Steps involved in it
Adapter Design Pattern Create an adapter class LogAdapter that adapts the Factory class to the Log interface This adapter class should implement the L... View full answer
Get step-by-step solutions from verified subject matter experts
