Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

commit #12

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

implemented the review comments

Original file line number Diff line number Diff line change
@@ -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);
}

}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.scaler.lld.design.assignments.prototype;

public class Configuration {
public class Configuration implements ClonableObject<Configuration> {
private String themeColor;
private Boolean autoSave;
private String language;
Expand All @@ -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;
}
Expand Down Expand Up @@ -46,4 +56,11 @@ public String getFontFamily() {
public ConfigurationType getType() {
return type;
}



@Override
public Configuration cloneObject() {
return new Configuration(this);
}
}
Original file line number Diff line number Diff line change
@@ -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<ConfigurationType, Configuration> 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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
public abstract class FileBasedConfigurationManager {

protected final Properties properties;

public FileBasedConfigurationManager() {
public FileBasedConfigurationManager() {
this.properties = new Properties();
}

Expand All @@ -21,6 +20,7 @@ public void load(String filePath) {
}

public static FileBasedConfigurationManager getInstance() {

throw new UnsupportedOperationException("Not implemented yet");
}

Expand All @@ -46,6 +46,9 @@ protected Properties getProperties() {

protected <T> T convert(String value, Class<T> type) {
System.out.println("Converting " + value + " to " + type.getSimpleName());
if(value==null){
return null;
}
switch (type.getSimpleName()) {
case "Integer":
return (T) Integer.valueOf(value);
Expand All @@ -56,6 +59,7 @@ protected <T> T convert(String value, Class<T> type) {
case "Double":
return (T) Double.valueOf(value);
}

throw new UnsupportedOperationException("Invalid type: " + type.getSimpleName());
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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> T getConfiguration(String key, Class<T> 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 <T> 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;
}
}

}
Original file line number Diff line number Diff line change
@@ -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();
}
Original file line number Diff line number Diff line change
@@ -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());

}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -10,21 +9,21 @@ 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"
);
}

@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"
);
Expand Down