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

Assignment Singleton - Soorya #24

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,80 @@
package com.scaler.lld.design.assignments.builder;

import lombok.Getter;

@WithBuilder
@Getter
public class DatabaseConfigurationBuilder {
private String databaseUrl;
private String username;
private String password;
private int maxConnections;
private boolean enableCache;
private boolean isReadOnly;

public DB_builer Builder()
{
return new DB_builer();
}

// Step 1: create static inner class
public static class DB_builer
{
// Step 2: copy all fields into builder class
private DatabaseConfigurationBuilder dbConfig;

public DB_builer()
{
this.dbConfig = new DatabaseConfigurationBuilder();
}

// Step 2:
// expose setter (fluent interfaces)


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

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

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

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

public DB_builer setReadOnly(boolean readOnly) {
this.dbConfig.isReadOnly = readOnly;
return this;
}

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

// Step 4: build method
public DatabaseConfigurationBuilder build()
{
DatabaseConfigurationBuilder db = new DatabaseConfigurationBuilder();

db.databaseUrl = dbConfig.databaseUrl;
db.isReadOnly = dbConfig.isReadOnly;
db.enableCache = dbConfig.enableCache;
db.maxConnections = dbConfig.maxConnections;
db.password = dbConfig.password;
db.username = dbConfig.username;

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

// Step 1 : Cloneable interface
public interface ClonableObject<T> {
T cloneObject();
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package com.scaler.lld.design.assignments.prototype;

public class Configuration {
import lombok.AllArgsConstructor;
import lombok.Getter;

// Step 2: implement clone method
@Getter
public class Configuration implements ClonableObject<Configuration>{
private String themeColor;
private Boolean autoSave;
private String language;
Expand All @@ -19,31 +24,8 @@ public Configuration(String themeColor, Boolean autoSave, String language, Boole
this.type = type;
}

public String getThemeColor() {
return themeColor;
}

public Boolean getAutoSave() {
return autoSave;
}

public String getLanguage() {
return language;
}

public Boolean getDarkMode() {
return darkMode;
}

public Integer getFontSize() {
return fontSize;
}

public String getFontFamily() {
return fontFamily;
}

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,24 @@
package com.scaler.lld.design.assignments.prototype;

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

public class ConfigurationRegistryImpl implements ConfigurationPrototypeRegistry{
private Map<ConfigurationType, Configuration> registry = new HashMap<>();

@Override
public void addPrototype(Configuration user)
{
registry.put(user.getType(), user);
}

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

@Override
public Configuration clone(ConfigurationType type) {
return getPrototype(type).cloneObject();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,49 +2,75 @@

public class FileBasedConfigurationManagerImpl extends FileBasedConfigurationManager {

// Step 3: static attribute
static FileBasedConfigurationManager INSTANCE = null;

// Step 1: Private constructor
private FileBasedConfigurationManagerImpl()
{
super();
}
@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'");
if(properties.containsKey(key)) {
return convert(properties.getProperty(key), type);
}
return null;
}

@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, value.toString());
}

@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();
}

// Step 2: global access point for instance
public static FileBasedConfigurationManager getInstance() {
// TODO Auto-generated method stub
return null;
// Step 4: Double-checked locking
if(INSTANCE == null)
{
synchronized (FileBasedConfigurationManagerImpl.class)
{
if(INSTANCE == null)
{
INSTANCE = new FileBasedConfigurationManagerImpl();
}
}
}

return INSTANCE;
}

public static void resetInstance() {
// TODO Auto-generated method stub
synchronized (FileBasedConfigurationManagerImpl.class)
{
INSTANCE = null;
}
}

}