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

"Singleton Pattern Solution" #25

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
Original file line number Diff line number Diff line change
@@ -1,6 +1,105 @@
package com.scaler.lld.design.assignments.builder;

@WithBuilder
public class DatabaseConfigurationBuilder {
public class DatabaseConfigurationBuilder {

// NO SETTER IN OUTER CLASS

private String databaseUrl;
private String username;
private String password;
private int maxConnections;
private boolean enableCache;
private boolean isReadOnly;

public String getDatabaseUrl() {
return databaseUrl;
}

public String getUsername() {
return username;
}

public String getPassword() {
return password;
}

public int getMaxConnections() {
return maxConnections;
}

public boolean isEnableCache() {
return enableCache;
}

public boolean isReadOnly() {
return isReadOnly;
}

public static Builder builder()
{
return new Builder();
}




public static class Builder
{
private DatabaseConfigurationBuilder obj;

public Builder() {
obj = new DatabaseConfigurationBuilder();
}

public Builder setDatabaseUrl(String databaseUrl) {
this.obj.databaseUrl = databaseUrl;
return this;
}

public Builder setUsername(String username) {
this.obj.username = username;
return this;
}

public Builder setPassword(String password) {
this.obj.password = password;
return this;

}

public Builder setMaxConnections(int maxConnections) {
this.obj.maxConnections = maxConnections;
return this;
}

public Builder setEnableCache(boolean enableCache) {
this.obj.enableCache = enableCache;
return this;

}

public Builder setReadOnly(boolean isReadOnly) {
this.obj.isReadOnly = isReadOnly;
return this;
}

public DatabaseConfigurationBuilder build(){
DatabaseConfigurationBuilder db=new DatabaseConfigurationBuilder();
db.databaseUrl=this.obj.databaseUrl;
db.enableCache=this.obj.enableCache;
db.isReadOnly=this.obj.isReadOnly;
db.maxConnections=this.obj.maxConnections;
db.password=this.obj.password;
db.username=this.obj.username;
return db;
}






}

}
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<T> implements ClonableObject<T>{
private String themeColor;
private Boolean autoSave;
private String language;
Expand All @@ -19,6 +19,27 @@ public Configuration(String themeColor, Boolean autoSave, String language, Boole
this.type = type;
}

public Configuration()
{

}
public Configuration(Configuration c)
{

this.themeColor=c.themeColor;
this.autoSave = c.autoSave;
this.language = c.language;
this.darkMode = c.darkMode;
this.fontSize = c.fontSize;
this.fontFamily = c.fontFamily;
this.type = c.type;

}
@Override
public T cloneObject()
{
return (T)(new Configuration(this));
}
public String getThemeColor() {
return themeColor;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.scaler.lld.design.assignments.prototype;
import java.util.HashMap;
import java.util.Map;
public class ConfigurationPrototypeRegistryImpl implements ConfigurationPrototypeRegistry{

Map<ConfigurationType,Configuration> map;
public ConfigurationPrototypeRegistryImpl()
{
map=new HashMap<>();
}
@Override
public void addPrototype(Configuration user) {

map.put(user.getType(),user);


}

@Override
public Configuration getPrototype(ConfigurationType type) {
return map.get(type);
}

@Override
public Configuration clone(ConfigurationType type) {

Configuration obj=map.getOrDefault(type,null);

if(obj!=null)
return (Configuration) obj.cloneObject();

return null;

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,49 +2,78 @@

public class FileBasedConfigurationManagerImpl extends FileBasedConfigurationManager {

private static FileBasedConfigurationManager INSTANCE=null;

private FileBasedConfigurationManagerImpl()
{

}

@Override
public String getConfiguration(String key) {
// TODO Auto-generated method stub
throw new UnsupportedOperationException("Unimplemented method 'getConfiguration'");
//throw new UnsupportedOperationException("Unimplemented method 'getConfiguration'");
return this.properties.getProperty(key);
}

@Override
public <T> T getConfiguration(String key, Class<T> type) {
// TODO Auto-generated method stub
throw new UnsupportedOperationException("Unimplemented method 'getConfiguration'");
//throw new UnsupportedOperationException("Unimplemented method 'getConfiguration'");
T ans=null;
String val=this.properties.getProperty(key);
if(val!=null)
ans=convert(val,type);
return ans;
}

@Override
public void setConfiguration(String key, String value) {
// TODO Auto-generated method stub
throw new UnsupportedOperationException("Unimplemented method 'setConfiguration'");
//throw new UnsupportedOperationException("Unimplemented method 'setConfiguration'");
this.properties.setProperty(key,value);
}

@Override
public <T> void setConfiguration(String key, T value) {
// TODO Auto-generated method stub
throw new UnsupportedOperationException("Unimplemented method 'setConfiguration'");
//throw new UnsupportedOperationException("Unimplemented method 'setConfiguration'");
this.properties.setProperty(key,(value+""));
}

@Override
public void removeConfiguration(String key) {
// TODO Auto-generated method stub
throw new UnsupportedOperationException("Unimplemented method 'removeConfiguration'");
//throw new UnsupportedOperationException("Unimplemented method 'removeConfiguration'");

this.properties.remove(key);
}

@Override
public void clear() {
// TODO Auto-generated method stub
throw new UnsupportedOperationException("Unimplemented method 'clear'");
//throw new UnsupportedOperationException("Unimplemented method 'clear'");
this.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
INSTANCE=null;
}

}