问题
I may be wrong as I have not got too much experience with Java, but here is a question.
I have a class which contains many methods (basically it is a simple library).
I create an object of this class let's say MyLibrary obj = new MyLibrary(parameters);
The parameters set up any necessary functionality for the library to run correctly.
Then I can call obj.getSomething
/obj.setSomething
/obj.createSomething
etc etc...
In my main class I really need only one this kind of library object.
Now... Would it be more useful for me not to use it as an object, but put it as extends
and then create a function inside of the library like a constructor which I would call manually?
EDIT:
The relation between the one class and MyLibrary is very close. Basically, I have many classes which do similar things but have some different higher layer functionality. So I separated method which must be in all those classes.
It seems it is very similar to shape
class and triangle
, circle
, square
example. So MyLibrary
is similar to shape
which contains all the foundation.
回答1:
What you described strongly resembles a utility class, similar to Java's Collections. The class has only static
methods, and a private constructor to prevent instantiations. This is a well-known idiomatic pattern in Java - you can use it to create your own groups of methods providing related functionality.
You should not extend, or even instantiate, utility classes at all. Starting with Java-5, you can statically import them so that you could use their methods without making an explicit reference to their class.
回答2:
extends
is used when you need an inheritance hierarchy. It seems more logical to put your code in two separate classes here, like you have it now.
Also, if your "library class" does multiple unrelated things, it should probably be split into multiple classes - one for each task.
回答3:
You should really only use extends
when you have a is-a relationship. So, you can think, is my main class a MyLibrary or should my class have a MyLibrary.
From your described problem, it sounds like having MyLibrary is the way to go.
回答4:
With the limited detail that you have provided, you might want to consider the Singleton pattern.
extends
should only be used when one object needs to inherit the characteristics and functionality of another one because they are very closely related. For example, if you have a Shape
class, then you would extend Shape
to create Circle
, Square
, and Triangle
. Before you use extends
you should learn more about inheritence and when you should and should not use it.
回答5:
I would make this a static
class to use. Similiar to javas MATH class API for math class. You can just use the methods of the class without making an object of it.
回答6:
Well If your class if performing utility functions then you should mark all methods as static
and use operations like
MyLibrary.doSomething();
MyLibrary.createSomething();
MyLibrary.getSomething();
But this wont allow you to keep some data members in the class and if you keep them they will be static as well.
I don't think so that extends
suits your case.
Also if you want to keep only an object then you should look at Singleton
A class for which only one instance can be created.
回答7:
Assuming you are just using MyLibrary
and may not alter it, you should use a wrapper that makes the whole thing a Singleton, as already proposed by Code-Guru.
public class MyLibraryWrapper {
private static MyLibrary instance = null;
private MyLibraryWrapper() {}
public static MyLibrary getInstance() {
if (instance == null)
instance = new MyLibrary();
return instance;
So in your code you would use
MyLibraryWrapper.getInstance().getSomething();
回答8:
Best way to create singleton in java 1.5 or above is to use ENUM.
public enum Test {
INSTANCE;
}
INSTANCE is the only instance of Test class.
来源:https://stackoverflow.com/questions/11691095/object-vs-extend-in-java