diff --git a/src/main/java/com/scaler/lld/design/assignments/builder/DatabaseConfigurationBuilder.java b/src/main/java/com/scaler/lld/design/assignments/builder/DatabaseConfigurationBuilder.java index 592aa32..ea22e86 100644 --- a/src/main/java/com/scaler/lld/design/assignments/builder/DatabaseConfigurationBuilder.java +++ b/src/main/java/com/scaler/lld/design/assignments/builder/DatabaseConfigurationBuilder.java @@ -1,6 +1,80 @@ package com.scaler.lld.design.assignments.builder; +import lombok.Getter; +import lombok.Setter; + @WithBuilder +@Getter public class DatabaseConfigurationBuilder { + private String databaseUrl; + private String username; + private String password; + private int maxConnections; + private boolean enableCache; + private boolean isReadOnly; + + + private DatabaseConfigurationBuilder(String databaseUrl, String username, String password, int maxConnections, boolean enableCache, boolean isReadOnly) { + this.databaseUrl = databaseUrl; + this.username = username; + this.password = password; + this.maxConnections = maxConnections; + this.enableCache = enableCache; + this.isReadOnly = isReadOnly; + } + + public static Builder getBuilder(){ + return new Builder(); + } + + @Setter + @Getter + public static class Builder{ + + private String databaseUrl; + private String username; + private String password; + private int maxConnections; + private boolean enableCache; + private boolean isReadOnly; + + + + public Builder setDatabaseUrl(String databaseUrl) { + this.databaseUrl = databaseUrl; + return this; + } + + public Builder setUsername(String username) { + this.username = username; + return this; + } + + public Builder setPassword(String password) { + this.password = password; + return this; + } + + public Builder setMaxConnections(int maxConnections) { + this.maxConnections = maxConnections; + return this; + } + + public Builder setEnableCache(boolean enableCache) { + this.enableCache = enableCache; + return this; + } + + public Builder setReadOnly(boolean readOnly) { + isReadOnly = readOnly; + return this; + } + + public DatabaseConfigurationBuilder build(){ + return new DatabaseConfigurationBuilder + (this.databaseUrl, this.username, this.password, this.maxConnections, this.enableCache, this.isReadOnly); + } + + } } \ No newline at end of file diff --git a/src/main/java/com/scaler/lld/design/assignments/prototype/Configuration.java b/src/main/java/com/scaler/lld/design/assignments/prototype/Configuration.java index 597f36e..4397c28 100644 --- a/src/main/java/com/scaler/lld/design/assignments/prototype/Configuration.java +++ b/src/main/java/com/scaler/lld/design/assignments/prototype/Configuration.java @@ -1,6 +1,6 @@ package com.scaler.lld.design.assignments.prototype; -public class Configuration { +public class Configuration implements ClonableObject { private String themeColor; private Boolean autoSave; private String language; @@ -19,6 +19,16 @@ public Configuration(String themeColor, Boolean autoSave, String language, Boole this.type = type; } + public Configuration(Configuration configuration) { + this.themeColor = configuration.getThemeColor(); + this.autoSave = configuration.getAutoSave(); + this.language = configuration.getLanguage(); + this.darkMode = configuration.getDarkMode(); + this.fontSize = configuration.getFontSize(); + this.fontFamily = configuration.getFontFamily(); + this.type = configuration.getType(); + } + public String getThemeColor() { return themeColor; } @@ -46,4 +56,11 @@ public String getFontFamily() { public ConfigurationType getType() { return type; } + + + + @Override + public Configuration cloneObject() { + return new Configuration(this); + } } \ No newline at end of file diff --git a/src/main/java/com/scaler/lld/design/assignments/prototype/ConfigurationPrototypeRegistryImpl.java b/src/main/java/com/scaler/lld/design/assignments/prototype/ConfigurationPrototypeRegistryImpl.java new file mode 100644 index 0000000..e90e560 --- /dev/null +++ b/src/main/java/com/scaler/lld/design/assignments/prototype/ConfigurationPrototypeRegistryImpl.java @@ -0,0 +1,25 @@ +package com.scaler.lld.design.assignments.prototype; + +import java.util.HashMap; +import java.util.Map; + +public class ConfigurationPrototypeRegistryImpl implements ConfigurationPrototypeRegistry{ + + private Map registryMap = new HashMap<>(); + + + @Override + public void addPrototype(Configuration user) { + registryMap.putIfAbsent(user.getType(), user); + } + + @Override + public Configuration getPrototype(ConfigurationType type) { + return registryMap.get(type); + } + + @Override + public Configuration clone(ConfigurationType type) { + return registryMap.get(type).cloneObject(); + } +} diff --git a/src/main/java/com/scaler/lld/design/assignments/singleton/FileBasedConfigurationManager.java b/src/main/java/com/scaler/lld/design/assignments/singleton/FileBasedConfigurationManager.java index 93e4ffd..16057c0 100644 --- a/src/main/java/com/scaler/lld/design/assignments/singleton/FileBasedConfigurationManager.java +++ b/src/main/java/com/scaler/lld/design/assignments/singleton/FileBasedConfigurationManager.java @@ -7,8 +7,7 @@ public abstract class FileBasedConfigurationManager { protected final Properties properties; - - public FileBasedConfigurationManager() { + public FileBasedConfigurationManager() { this.properties = new Properties(); } @@ -21,6 +20,7 @@ public void load(String filePath) { } public static FileBasedConfigurationManager getInstance() { + throw new UnsupportedOperationException("Not implemented yet"); } @@ -46,6 +46,9 @@ protected Properties getProperties() { protected T convert(String value, Class type) { System.out.println("Converting " + value + " to " + type.getSimpleName()); + if(value==null){ + return null; + } switch (type.getSimpleName()) { case "Integer": return (T) Integer.valueOf(value); @@ -56,6 +59,7 @@ protected T convert(String value, Class type) { case "Double": return (T) Double.valueOf(value); } + throw new UnsupportedOperationException("Invalid type: " + type.getSimpleName()); } diff --git a/src/main/java/com/scaler/lld/design/assignments/singleton/FileBasedConfigurationManagerImpl.java b/src/main/java/com/scaler/lld/design/assignments/singleton/FileBasedConfigurationManagerImpl.java index 15470fc..42cc3dc 100644 --- a/src/main/java/com/scaler/lld/design/assignments/singleton/FileBasedConfigurationManagerImpl.java +++ b/src/main/java/com/scaler/lld/design/assignments/singleton/FileBasedConfigurationManagerImpl.java @@ -1,50 +1,62 @@ package com.scaler.lld.design.assignments.singleton; +import java.util.Optional; + public class FileBasedConfigurationManagerImpl extends FileBasedConfigurationManager { + private static FileBasedConfigurationManagerImpl INSTANCE = null; + + private FileBasedConfigurationManagerImpl() { + super(); + } + @Override public String getConfiguration(String key) { - // TODO Auto-generated method stub - throw new UnsupportedOperationException("Unimplemented method 'getConfiguration'"); + return properties.getProperty(key); } @Override public T getConfiguration(String key, Class type) { - // TODO Auto-generated method stub - throw new UnsupportedOperationException("Unimplemented method 'getConfiguration'"); + return convert(properties.getProperty(key), type); } @Override public void setConfiguration(String key, String value) { - // TODO Auto-generated method stub - throw new UnsupportedOperationException("Unimplemented method 'setConfiguration'"); + properties.setProperty(key, value); } @Override public void setConfiguration(String key, T value) { - // TODO Auto-generated method stub - throw new UnsupportedOperationException("Unimplemented method 'setConfiguration'"); + properties.setProperty(key, String.valueOf((value))); } @Override public void removeConfiguration(String key) { - // TODO Auto-generated method stub - throw new UnsupportedOperationException("Unimplemented method 'removeConfiguration'"); + properties.remove(key); } @Override public void clear() { - // TODO Auto-generated method stub - throw new UnsupportedOperationException("Unimplemented method 'clear'"); + if(!properties.isEmpty()) { + properties.clear(); + } } public static FileBasedConfigurationManager getInstance() { - // TODO Auto-generated method stub - return null; + if(INSTANCE == null ){ + synchronized (FileBasedConfigurationManager.class){ + if(INSTANCE == null){ + INSTANCE = new FileBasedConfigurationManagerImpl() ; + } + } + } + return INSTANCE; } public static void resetInstance() { - // TODO Auto-generated method stub + if(INSTANCE != null ){ + INSTANCE = null; + } } } \ No newline at end of file diff --git a/src/main/java/com/scaler/lld/design/creational/parleg/Button.java b/src/main/java/com/scaler/lld/design/creational/parleg/Button.java index 4b22140..e56f2c5 100644 --- a/src/main/java/com/scaler/lld/design/creational/parleg/Button.java +++ b/src/main/java/com/scaler/lld/design/creational/parleg/Button.java @@ -1,2 +1,11 @@ -package com.scaler.lld.design.creational.parleg;public class Button { +package com.scaler.lld.design.creational.parleg; +public abstract class Button { + private Double border; + public Button(Double border) { + this.border = border; + } + + abstract public void onClick() ; + + abstract public void render(); } diff --git a/src/main/java/com/scaler/lld/design/creational/simplefactory/button/ButtonFactory.java b/src/main/java/com/scaler/lld/design/creational/simplefactory/button/ButtonFactory.java index 84d177b..f195a00 100644 --- a/src/main/java/com/scaler/lld/design/creational/simplefactory/button/ButtonFactory.java +++ b/src/main/java/com/scaler/lld/design/creational/simplefactory/button/ButtonFactory.java @@ -1,16 +1,66 @@ package com.scaler.lld.design.creational.simplefactory.button; +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.Setter; + +@AllArgsConstructor +@Getter public class ButtonFactory { + private ScreenSize screenSize; + private Double border; + private Double radius; + private Double length; + + private ButtonFactory(){ + + } + public static Builder getBuilder(){ + return new Builder(); + } + @Setter + @Getter + static class Builder{ + private ScreenSize screenSize; + private Double border; + private Double radius; + private Double length; + + public Builder setScreenSize(ScreenSize screenSize) { + this.screenSize = screenSize; + return this; + } + + public Builder setBorder(Double border) { + this.border = border; + return this; + } + + public Builder setRadius(Double radius) { + this.radius = radius; + return this; + } + + public Builder setLength(Double length) { + this.length = length; + return this; + } + + public Button build(){ + return createButton(this); + } + } + // Step 3 - Create a static factory method - public static Button createButton(ScreenSize screenSize, Double border, Double radius, Double length) { - switch (screenSize) { + public static Button createButton(Builder build) { + switch (build.getScreenSize()) { case PHONE: - case TABLET: return new RoundButton(border, radius); - case DESKTOP: return new SquareButton(border, length); + case TABLET: return new RoundButton(build.getBorder(), build.getRadius()); + case DESKTOP: return new SquareButton(build.getBorder(), build.getLength()); } - throw new IllegalArgumentException("Invalid type: " + screenSize); + throw new IllegalArgumentException("Invalid type: " + build.getScreenSize()); } } diff --git a/src/test/java/com/scaler/lld/design/creational/simplefactory/button/ButtonTest.java b/src/test/java/com/scaler/lld/design/creational/simplefactory/button/ButtonTest.java index 5031c2c..7d1e419 100644 --- a/src/test/java/com/scaler/lld/design/creational/simplefactory/button/ButtonTest.java +++ b/src/test/java/com/scaler/lld/design/creational/simplefactory/button/ButtonTest.java @@ -1,6 +1,5 @@ -package com.scaler.lld.design.creational; +package com.scaler.lld.design.creational.simplefactory.button; -import com.scaler.lld.design.creational.parleg.*; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -10,10 +9,10 @@ public class ButtonTest { @Test public void testRoundButton() { - Button button = ButtonFactory.createButton( + /* Button button = ButtonFactory.createButton( ScreenSize.PHONE, 10.0, 1.0, null - ); - + );*/ + Button button = ButtonFactory.getBuilder().setScreenSize(ScreenSize.PHONE).setRadius(10.0).setBorder(1.0).build(); assertTrue(button instanceof RoundButton, "If the screen size is of a phone, the btn should be round" ); @@ -21,10 +20,10 @@ public void testRoundButton() { @Test public void testSquareButton() { - Button button = ButtonFactory.createButton( - ScreenSize.DESKTOP, 10.0, null, 10.0 - ); - +// Button button = ButtonFactory.createButton( +// ScreenSize.DESKTOP, 10.0, null, 10.0 +// ); + Button button = ButtonFactory.getBuilder().setScreenSize(ScreenSize.DESKTOP).setLength(10.0).setBorder(10.0).build(); assertTrue(button instanceof SquareButton, "If the screen size is of a desktop, the btn should be square" );