Serialization of a Map [closed] - java

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
I have a a class that its like a wrapper for a map. It is a property list, mostly use for the sake of the name it looks like the following:
public class MyPropertyList implements Serializable
{
private static final long serialVersionUID = 7896123434L;
private LinkedHashMap<String,String> map;
public static final String key1 = "key1";
public static final String key2 = "key2";
//More key values
public MyPropertyList(){
map = new LinkedHashMap<String,String>();
}
public void addProperty(String key, String value){
map.put(key,value);
}
public LinkedHashMap<String,String> getMapping(){
return map;
}
public int getSize(){
return map.size;
}
//...Other delegated calls
}
It it a bad design? Does it present any issues? Is there a better design for that fulfills this purpose?

Use the Properties class from Java standar Library, which implements Serializable.

The code you have posted offers no additional functionality over LinkedHashMap, so any code which uses this might just as well use LinkedHashMap directly.

Related

Does Factory Pattern suits the best in my case? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 years ago.
Improve this question
I have a Feeder class that contains a method that feed data into a list, given data, the list and a custom class :
default void feedData(CustomData myData, List list, Class c){
ObjectMapper objectMapper = new ObjectMapper();
if(myData!= null) {
for (Map<String, Object> map : myData.getData()) {
list.add(objectMapper.convertValue(map, c));
}
}
}
I want to implement a design pattern because I have several classes that can should feed data regarding specific classes.
I thought about Factory pattern, is that a good idea?
Class 1 would be like:
public void feed(CustomData myData){
feedData(myData, myField, CustomClass1.class);
}
Class 2 :
public void feed(CustomData myData){
feedData(myData, myField2, CustomClass2.class);
}
etc.
And then, an interface for example IFeeder where I declare
void feed(CustomData myData);
But where can I put the feedData method? Inside this interface?
If yes, I should declare it as default, but default keyword is for a default method that return always the same result, isn't it?
Thanks for any advice.
public static <T> void feedData(CustomData myData, List<T> list, Class<T> clazz){
ObjectMapper objectMapper = new ObjectMapper();
if(myData!= null) {
for (Map<String, Object> map : myData.getData()) {
list.add(objectMapper.convertValue(map, clazz));
}
}
}
I believe this should work. This can be a static method in some util class.
I am assuming that all the other classes that you are referring can implement a common interface.

In Java, what is the better way to use Enums? With static constants or with constructors and off-file constants? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 3 years ago.
Improve this question
We are creating an enum for possible environments the user can login to. But in my team, we have a disagreement on what is the better way to proceed. I'm hoping to get some insight from the community, so that we can all learn.
The aim is to create an enum with environments (prod, staging, etc..)
Each environment, has a baseURL, a loginURLPostfix and a logoutURLPostfix.
What is the better way to go about this? And please explain why.
Method 1
public enum Environment {
Development,
Production;
//
// Constants
//
private static String BASE_URL_STRING_PROD = "https://prodBaseURL.com";
private static String BASE_URL_STRING_DEV = "https://devBaseURL.com";
private static String LOGIN_SUFFIX = "/login";
private static String LOGOUT_SUFFIX = "/logout";
public String loginURLString() {
return baseURL() + LOGIN_SUFFIX;
}
public String baseURL() {
switch (this) {
case Development: return BASE_URL_STRING_PROD;
case Production: return BASE_URL_STRING_DEV;
default: return REDIRECT_URI_PROD;
}
}
}
Method 2
public enum Environment {
Development(Constants.BASE_URL_STRING_DEV, Constants.LOGIN_SUFFIX),
Production(Constants.BASE_URL_STRING_, Constants.LOGIN_SUFFIX);
String baseURL;
String loginURLSuffix;
public Environment(String baseURL, String loginURLSuffix) {
this.baseURL = baseURL;
this.loginURL = loginURL
}
public String loginURLString() {
return this.baseURL + Constants.LOGIN_SUFFIX;
}
}
public class Constants {
public static final String BASE_URL_STRING_PROD = "https://prodBaseURL.com";
public static final String BASE_URL_STRING_DEV = "https://devBaseURL.com";
public static final String LOGIN_SUFFIX = "/login";
public static final String LOGOUT_SUFFIX = "/logout";
}
Edit: StackOverflow put the question on hold as an "opinion based" question. So my edit in order to clarify this is the following:
Is the difference between Method 1 and Method 2 purely opinion based?
I don't like either approach for the properties you are trying to set.
What you have here is environment configuration in code, which is something that you should avoid as it requires a code change should any of these parameters change. Environment URL's would be best suited to a configuration file (XML, YAML, .properties, pick your poison) rather than hard-coded into your source code.

Singleton design pattern [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I want to implement this singleton class in java . How can I
put these variables in this class?
This is one simple example for your singleton. Feel free to add setters and getters as you need it for the fields.
I'm not sure what the set-method in your diagram should do but maybe you don't need it anyway.
public class LibraryInfo {
private static final LibraryInfo instance = new LibraryInfo();
public static LibraryInfo getInstance() {
return instance;
}
private LibraryInfo() {}
private String name;
private int phone;
private String address;
private String openTime;
private String closeTime;
// getters
}

Java: Override protected String [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I don't know Java.
Just need a quick fix if possible.
How to override protected String?
I have:
public class Something {
protected String changeMe = "this";
public String viewForm() {
...
return getForm();
}
public String getForm() {
return changeMe;
}
}
myfile:
public class MySomething extends Something {
public String viewForm() {
return super.viewForm();
}
}
How to use changeMe = "that" in class MySomething?
Thanks.
Actually, others have answered your question. In case, you are really new to Java struggling to figure it out, the simplest answer to your question is just put your above mentioned statement in the constructor of the derived class:
Solution one (actually bad solution):
public class MySomething extends Something {
public MySomething() {
changeMe = "that"
}
}
A better solution for your problem is to define a setter method in the class Something as follows (in case you have access to its source code):
public void setChangeMe(String newValue) {
changeMe = newValue;
}
and you can change the value by calling
Something s = new Something();
s.setChangeMe("that");
the word override is a hint for the compiler, with that you let it know that you're writing code that at your own way implements the method of a parent class or the interface.
having said that overriding a variable/attribute/field is a wrong use of the terminology, and makes not much sense
to answer your question: you can access toso the changeMe object because it is not defined as private... so you can change the value directly..
Example:
public static void main(String args[]) {
MySomething ms = new MySomething();
System.out.println(ms.changeMe);
ms.changeMe = "that";
System.out.println(ms.changeMe);
}

Design pattern to convert a class to another [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 4 years ago.
Improve this question
I have a class called GoogleWeather, I want to convert it to another class CustomWeather.
Is there any design pattern which helps you to convert classes?
In that case I'd use a Mapper class with a bunch of static methods:
public final class Mapper {
public static GoogleWeather from(CustomWeather customWeather) {
GoogleWeather weather = new GoogleWeather();
// set the properties based on customWeather
return weather;
}
public static CustomWeather from(GoogleWeather googleWeather) {
CustomWeather weather = new CustomWeather();
// set the properties based on googleWeather
return weather;
}
}
So you don't have dependencies between the classes.
Sample usage:
CustomWeather weather = Mapper.from(getGoogleWeather());
There is one critical decision to make:
Do you need the object that is generated by the conversion to reflect future changes to the source object?
If you do not need such functionality, then the simplest approach is to use a utility class with static methods that create a new object based on the fields of the source object, as mentioned in other answers.
On the other hand, if you need the converted object to reflect changes to the source object, you would probably need something along the lines of the Adapter design pattern:
public class GoogleWeather {
...
public int getTemperatureCelcius() {
...
}
...
}
public interface CustomWeather {
...
public int getTemperatureKelvin();
...
}
public class GoogleWeatherAdapter implements CustomWeather {
private GoogleWeather weather;
...
public int getTemperatureKelvin() {
return this.weather.getTemperatureCelcius() + 273;
}
...
}
Besides, You can also use new Java8 feature 'Function' from java.util.function'.
More detailed explanation is provided in http://www.leveluplunch.com/java/tutorials/016-transform-object-class-into-another-type-java8/ . Kindly have a look!

Categories

Resources