I suggest you to use a Singleton class. The mimimum contents of a singleton are:
- A static private field of the class type.
- Other private fields you need ("business" fields).
- A private constructor.
- A public static method that returns the only class instance.
- The business methods you need.
I usually call it Store, since it plays the role of a common store for the whole application.
For example:
public class Store {
private static Store me;
private Facebook fb;
private Twitter twitter;
private Store() {
this.fb=new Facebook();
this.twitter=new Twitter();
}
public static Store get() {
if (me==null)
me=new Store();
return me;
}
public void postInFaceBook(FBPost post) {
this.fb.post(post);
}
...
}
Sometimes you need a context to be shared among activities. In this case, I suggest to pass the application context to the get method:
public class Store {
private static Store me;
private Facebook fb;
private Twitter twitter;
private Context ctx;
private Store(Context ctx) {
this.ctx=ctx;
this.fb=new Facebook();
this.twitter=new Twitter();
}
public static Store get(Context ctx) {
if (me==null)
me=new Store(ctx);
return me;
}
public void postInFaceBook(FBPost post) {
this.fb.post(this.ctx, post);
}
...
}
The use of the Store is simply:
public void m(FBPost p) {
Store.get().postInFacebook(p);
}
The first time the store is used, the only instance (which will be shared by the whole application) will be created; the second and next times, the get method will return the previous existing instance.