Factory pattern not following OPEN CLOSE Principle [closed] - java

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 9 years ago.
Improve this question
When we implement factory pattern in java
Suppose the factory of products needs to be implemented, A Product interface is required, All the concrete classes like AProduct, BProduct and CProduct will implement the Product Interface.
Public Static Product createProduct(String prdName);
The signature of the factory method will be as shown above.
Now if a new product say DProduct is introduced then we need to make changes to createProduct method, So this pattern does not follow OPEN CLOSE Principle.
(Open close principle is your code should be open for extension and closed for modification.)
Can I apply any other pattern over this, that will solve my problem.

if a new product say DProduct is introduced then we need to make changes to createProduct method
No, or at least not necessarily: consider java.sql.DriverManager class for a counterexample. Its getConnection(String url) is clearly a factory method, yet it handles addition of new drivers with ease, with no changes made to the method itself.
The trick is to make your factory configurable: rather than hardcoding a bunch of if-then-elses, you could build a "registry" of classes that maps the key passed into the factory to the name of the class, and use reflection APIs to instantiate the classes that your method needs to return.

Related

Where I define wrapper function and functional interface for exception handling on stream [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 8 months ago.
Improve this question
While using streams, I learned how to handle exceptions using functional interfaces and wrapper functions.
If so, what is the best way to structure the package?
Is it defined together in the class where stream is used? Or should I create a separate common class and define the wrapper function in it?
Thanks!
I think it depends. If you have one instance of using this technique, then it probably makes sense to simply use an functional interface and a wrap function that are part of the class which utilizes it. If you are using this same pattern in several places (and they all have the same function interface signature) then you can define the functional interface at the package level and put the wrap function in a utility class.

Java: Create Instances based on prototype [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 6 years ago.
Improve this question
How can I, and What is the best/proper way (ie, most performant and clearest syntactically) in Java to create object instances based on a prototype object instance, when this will occur repeatedly and in a performance critical code path?
I have thought about cloning via a cloning support library, but is that the best/only way? (These need to be arbitrary objects, btw, not ones that implement Clonable).
To clarify what I mean: I have an existing instance of Class T, which has fields set on it, and I want to pop out many versions of the same object to use separately, with the best performance and syntactic clarity possible.
Thanks.
Create a builder, which receives this class instance:
Person newOne = new PersonBuidler(oldOne).setAge(42)
Implementation of this builder may use apache common BeanUtils for cloning Java Beans or some other utility library for cloning arbitrary class.
See How do I copy an object in Java?

Which design patterns are implemented by java.util.Currency? [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 7 years ago.
Improve this question
Which design patterns are implemented by java.util.Currency, if any?
http://docs.oracle.com/javase/6/docs/api/java/util/Currency.html
I assume this is homework.
While there could be several design patterns at play in the implementation, when you see getInstance, you should immediately think Singleton:
http://en.wikipedia.org/wiki/Singleton_pattern
When the API manual at http://docs.oracle.com/javase/6/docs/api/java/util/Currency.html says:
The class is designed so that there's never more than one Currency
instance for any given currency. Therefore, there's no public
constructor.
You obtain a Currency instance using the getInstance methods.
(emphasis mine), well, you should think Singleton again - it's the Intent given by the GoF for the Singleton pattern, almost verbatim.
Currency uses Factory Method design pattern to create instances of this class. Method getInstance() returns Singleton object per Locale, but Currency itself isn't pure Singleton - there might be multiple instances of this class (for different currencies).
Not sure if that's what you mean, but one way of looking at it would be to say it's singleton design pattern

Why Java utilizes both Collection and Collections classes to provide different things [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 8 years ago.
Improve this question
In many places Java utilizes the approach to have both, for example, Collection and Collections class.
Collection is interface, it defined some methods.
Collections class also provide some method.
Why didn't they choose to place all methods inside the interface?
because the names are too puzzled word. Collection is like Collections.
I know the historical reason. like interrupt() and interrupted() , because java must fit to old version, the methods' names are likely, make developer difficult to write and read.
But the collection framwork must have reason in this way.
For starters, an interface cannot have static methods. Note: until Java 8.
Arguably, some of the static methods of Collections should have been made instance methods of Collection, but that would create a lot of "clutter". Plus, extra work for implementations not derived from AbstractCollection etc.

Class design - create a object from String data? [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 9 years ago.
Improve this question
I am working on simple application of Universal Turing Machine. I have a data to create machine from, my questions is not about UTM I would like to illustrate it only on this app. I need to create an object from String data, e.g. this method create one transition function of my UTM:
public static Transition createFromData(String data) {
Transition trans = new Transition();
String[] dataSplitted = data.split("1");
trans.setInputState(new State(dataSplitted[0]));
trans.setInputSymbol(dataSplitted[1]);
trans.setNewState(new State(dataSplitted[2]));
trans.setNewSymbol(dataSplitted[3]);
trans.setMovement(Movement.getByCode(dataSplitted[4]));
return trans;
}
Is it good idea (from class design perspective) to have such methods in Transition class or should I separate them to another / tool classes? What is bets practice for this issue?
Using tools/utils classes always remind me of structured programming ;) but would help if you just had a constructor on the Transition class the would receive the raw data and create a new transition object with that. Since you will be creating a new transition each time you find some raw data the best fit is in the constructor (or maybe in a factory method, but that is another discussion)
what about having this method in a another class for eg: RawData, which will have static factory methods for creating different objects from data.
In your case you can have one method will will return Transition object and then you can have multiple methods and each return a different object depending upon how data is organized / represented in that object.

Categories