I want to design a class which should return a singleton of some third party object.
For e.g., I want to create a singleton of 3rd party B class object. Below is the design I have made.
public class A{
private static A A = null;
private static B B = null;
private A() {
B = code to instantiate B Object;
}
public static synchronized A getAInstance() {
if(A ==null){
synchronized(A.class){
if(A == null){
A = new A();
}
}
}
return A;
}
public B getB(){
return B;
}
}
Can you please help me is this a proper singleton
If I understand your question correctly, you want to have a single instance of a third party class. First of all it is a good practice to access third party obj via wrapper class obj,(clean code handbook of agile software craftsmanship chapter8), in your case class b is wrapped by class a.
In order to make a single instance of class b you can just make it an instance variable of class a and then make the class a singleton, code bellow
Public class A{
private static A A = null;
private B B = null;
private A() {
B = code to instantiate B Object;
}
public static synchronized A getAInstance() {
if(A ==null){
synchronized(A.class){
if(A == null){
A = new A();
}
}
}
return A;
}
public B getB(){
return B;
}
}
You can simply have this structure. No explicit synchrnoization required, just leave it to JVM.
public class A {
private static class BInstanceHolder {
B BInstance = new B();
}
private A(){}
public static B getB(){
return BInstanceHolder.BInstance;
}
}
If you only want to have one copy of B, just do it that way!
You dont even need a Singleton of Class A.
So you could try:
public final class A{
private A(){}
private static B instance;
static{
instance = code to instantiate B Object
}
public static synchronized B getInstance() {
return B;
}
}
The static Block will create a instance of B when the Class is first mentioned and will instantiate the instance. The constructor will prevent the A from being made, but you can still access the only instance of B.
Related
Being new to Java but an old hand on older procedural languages and structured programming, I have a question on how to accomplish something in Java
I have three classes, let's say they're called CLASSA, CLASSB, and TESTCLASSA. CLASSA has a class definition with instance variables, and a constructor for some data. TESTCLASSA creates an instance of CLASSA and passes data to CLASSA by creating an instance of the object for CLASSA. SImiliarly CLASSB has another class definition with instance variables, and a constructor for some data. TESTCLASSA creates an instance of CLASSB and passes data to CLASSB by creating an instance of the object for CLASSB. I am trying to access CLASSB's data from CLASSA. Can someone suggest how I might go about doing this. Many thanks in advance for any assistance/suggestions you can provide.
Wayne Hann
Either declare the variable as public:
public class classA {
public Integer data;
}
or create a public getter (preferred), such as:
public class classA{
private Integer data;
public Integer getData() {
return data;
}
}
If you want to access properties of class B from class A then it's either possible that you create a object of B in the method from where you want to getData like
private String nameofA;
public String getNameofA() {
return nameofA;
}
public void setNameofA(String nameofA) {
this.nameofA = nameofA;
}
public String getClassBData(){
B b = new B();
return b.getNameofB();
}
else you create a class level instance or dependency of B type.
public class TestClassA{
public static void main(String[] args) {
B b = new B();
b.setNameofB("class B Name");
A a = new A("class A Name",b);
}
}
class A{
private String nameofA;
private B b = new B(); //either this
public String getNameofA() {
return nameofA;
}
public A(String nameofA, B b) {//or constructor
super();
this.nameofA = nameofA;
this.b = b;
}
public void setNameofA(String nameofA) {
this.nameofA = nameofA;
}
public String getClassBData(){
B b = new B(); // or creating local instance
//but here a new instance will be created
return b.getNameofB();
}
}
Then only you will be able to access the data of instance of B. Anyway you if your method in B is not private or protected(assuming A doesn't extend B), you can access the method by creating or passing a instance of B to the method of "A" from where you want to access.
I am bit new to patterns and using them.
I want to configure an object of a dependency class(say A.) once and use it through out my app. I tried making a singleton wrapper class on top of it, but failed miserably. I tried something like:
public class B {
public static A a = new A();
public static A getInstance() {
return a;
}
private B() {
a.configure();
}
}
I think calling B.getInstance() won't configure the object here. What I want here is to configure A's instance once and use it everywhere.
Basically your singleton class is B right ? in your sample code you use the singleton pattern for the inner A, which doesnt make much sense.
From my understanding you should go for something like that :
public class B {
private static B _instance;
public static B getInstance() {
if (_instance == null) {
_instance = new B(new A());
}
return _instance;
}
private A _a;
private B (A a) {
_a = a;
_a.configure();
}
}
I am thinking of an optimum design pattern which I can use to transfer objects to the methods in different classes other than passing them as arguments.
class A{
}
class B{
public A a;
public B()
{
a = new A();
}
}
class C
{
public void c()
{
//need to access "a" of class B other than passing "a" as argument;
}
}
Here, a in class A attribute needs to be accessed in many other class methods. Is there an optimum design pattern or any possible way other than passing this object (a) as arguments?
It's hard to say how your program is really structured but two options come to mind:
Pass an instance of B to C's constructor.
class A {};
class B {
public A a;
public B() {
a = new A();
}
};
class C {
public B b;
public C( B b ) {
this.b = b;
}
public void someMethod() {
System.out.println( b.a );
}
};
If only one instance of class A ever exists (ie a Singleton). That means that class B holds an instance of class A, not each instance of class B holds an instance of class A.
class A {};
class B {
public static final A a = new A();
};
class C {
public void someMethod() {
System.out.println( B.a );
}
};
Let's say I have 3 classes; A , B and C. Class A and B are in one package. C is in another package. B is having a public function that returns a boolean value.
Object of B is created in Class A and he can call the functions of Class B. But the problem is that in Class C I want to have the reference of Class B object created by Class A, I don't want to create one more Class B object in Class C.How can I get that.
Example: Class B and Class A are in same package
B.java
class B
{
public boolean fun()
{
returns boolValue;
}
}
A.java
class A
{
B b = new B(); //Creates object of Class B and can access function.
}
Class C is in another package and different project also.
C.java
class C
{
//How to get the reference of Class B object created in Class A?
}
Once I get the reference to that I am going to call the functions of Class B to get the values. I tried writing some get() which returns the object of class B but to call that function I should have the object. But I don't know how to do that. This might be easy but I am new to java and I don't know how to do that. Please do help me to solve this problem.
**UPDATE : I can't create Class A object in Class C :( **
Make class A public
publi class A {
private static B b = new B(); //Creates object of Class B and can access function.
public static B getB() {
return b;
}
}
class C
{
//I want to get the reference of Class B object created in Class A....But How??? :(
B b = A.getB(); //if it works ????
}
Classes A and B need to be public and class A needs to provide a public accessor to the required field.
public class B {
}
public class A {
public B getB() {
return this.b;
}
}
Note that in your current code structure you will not be able to automatically instantiate B from your A object outside of a method.
You can create an A object in the class description but to initialize B you'll need to use the class constructor.
public class A {
private B b = new B();
public B getB() {
return b;
}
}
public class C {
private A a = new A();
private B b;
public C() {
b = a.getB();
}
}
or, if you don't want to create A, you may make it static.
public static class A {
private static B b = new B();
public static B getB() {
return b;
}
}
public class C {
private B b;
public C() {
b = A.getB();
}
}
You have to get access in C to A class and then share B reference by field.
So you may do something like this:
class B { //your function... }
class A {
B b = new B();
public B getB() { return b }
}
class C {
A a = new A();
public B getB () { return a.getB() }
}
}
Add a constructor to class C with object b as parameter
public class C{
private B bObj;
public C(B b){
bObj=b;
}
}
call this constructor from within your A class, which will create a reference to the created B object and you'll have this ref in bObj.
Please advise me the difference between two ways of declaration of java constructor
public class A{
private static A instance = new A();
public static A getInstance() { return instance;
}
public static void main(String[] args) {
A a= A.getInstance();
}
}
AND
public class B{
public B(){};
public static void main(String[] args) {
B b= new B();
}
}
Thanks
Class A is supposed to be a Singleton, where you can only have one instance of A. You retrieve that single instance by calling getInstance();
In software engineering, the singleton
pattern is a design pattern used to
implement the mathematical concept of
a singleton, by restricting the
instantiation of a class to one
object. This is useful when exactly
one object is needed to coordinate
actions across the system.
There are a few ways to go about this depending on your requirements:
public class A{
private static A instance = new A();
private A(){} // private constructor
public static A getInstance() {return instance;}
}
or not creating the instance until the first call
public class A{
private static A instance = null;
private A(){} // private constructor
public static A getInstance() {
if(instance == null){
instance = new A(); // create the one instance.
}
return instance;
}
}
Class B is a class with a no-parameter constructor. You can create as many B instances as you want by calling new B();
It looks like A is an attempt at implementing the singleton pattern, but it's not quite right - it should have a private constructor:
class A {
private static final A INSTANCE = new A();
private A() { }
public static A getInstance() { return INSTANCE; }
}
This ensures that only one instance of A ever exists in your application - if any other object needs to use an instance of A to do something, the only way it can get one is via the getInstance() method, which returns the same instance all the time.
With B, you can have as many instances of B as needed/desired, and any other object is free to make a new instance of B if it chooses.
In the first case, only one instance available. In the second case, one can have as many as possible. You need to make the constructor private in the in the first case.