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

Design pattern Assignment of singleton,builder,prototype #27

Open
wants to merge 4 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
Expand Up @@ -2,5 +2,88 @@

@WithBuilder
public class DatabaseConfigurationBuilder {
private String databaseUrl;
private String username;
private String password;
private int maxConnections;
private boolean enableCache;
private boolean isReadOnly;
private DatabaseConfigurationBuilder(DBConfigureBuilder dbConfigurationBuilder){
this.databaseUrl = dbConfigurationBuilder.databaseUrl;
this.username = dbConfigurationBuilder.username;
this.password = dbConfigurationBuilder.password;
this.maxConnections = dbConfigurationBuilder.maxConnections;
this.enableCache = dbConfigurationBuilder.enableCache;
this.isReadOnly = dbConfigurationBuilder.isReadOnly;
}

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

public static class DBConfigureBuilder{
private String databaseUrl;
private String username;
private String password;
private int maxConnections;
private boolean enableCache;
private boolean isReadOnly;

public String getDatabaseUrl() {
return databaseUrl;
}

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

public String getUsername() {
return username;
}

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

public String getPassword() {
return password;
}

public DBConfigureBuilder setPassword(String password) {
this.password = password;
return this;
}

public int getMaxConnections() {
return maxConnections;
}

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

public boolean isEnableCache() {
return enableCache;
}

public DBConfigureBuilder setEnableCache(boolean enableCache) {
this.enableCache = enableCache;
return this;
}

public boolean isReadOnly() {
return isReadOnly;
}

public DBConfigureBuilder setReadOnly(boolean readOnly) {
isReadOnly = readOnly;
return this;
}
public DatabaseConfigurationBuilder build(){
return new DatabaseConfigurationBuilder(this);
}
}
}
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 Down Expand Up @@ -46,4 +46,11 @@ public String getFontFamily() {
public ConfigurationType getType() {
return type;
}

@Override
public Configuration cloneObject() {
return new Configuration(this.themeColor,this.autoSave,
this.language,this.darkMode,this.fontSize,
this.fontFamily,this.type);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.scaler.lld.design.assignments.prototype;

import java.util.HashMap;
import java.util.Map;

public class ConfigurationPrototypeRegistryImpl implements ConfigurationPrototypeRegistry{

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

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

@Override
public Configuration clone(ConfigurationType type) {
if(map.containsKey(type)){
return map.get(type).cloneObject();
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,49 +2,68 @@

public class FileBasedConfigurationManagerImpl extends FileBasedConfigurationManager {

private static FileBasedConfigurationManager fileBasedConfigurationManager;

private FileBasedConfigurationManagerImpl(){
}

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

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

@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)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'");
properties.clear();
}

public static FileBasedConfigurationManager getInstance() {
// TODO Auto-generated method stub
return null;
if(fileBasedConfigurationManager==null){
synchronized (FileBasedConfigurationManagerImpl.class){
if(fileBasedConfigurationManager==null){
fileBasedConfigurationManager=new FileBasedConfigurationManagerImpl();
}
}
}
return fileBasedConfigurationManager;
}

public static void resetInstance() {
// TODO Auto-generated method stub
fileBasedConfigurationManager=null;
//test changes
}

}