Abstract factory pattern'u kullanın.
Özet fabrikanız, iletişim kutuları ve diğer bildirimler oluşturmanıza izin verecektir. Daha sonra, Android sürümünü veya bildirimin JVM sürümünü oluşturan 2 somut uygulamaya sahip olacaksınız.
Örnek: oluşturulmasından sonra yöntemlerini çağırmak edebilmek için ihtiyaç iletişim kutuları ile
/*
* The abstract factory
*/
public abstract class NotificationFactory {
/**
* Pops a message up for the user.
*
* @param msg
*/
public abstract void newToast(String msg);
// other notifications can go here
}
/**
* Concrete Android notification factory
*
*/
public final class AndroidNotificationFactory extends NotificationFactory {
private final Context context;
public AndroidNotificationFactory(Context context) {
this.context = context;
}
@Override
public void newToast(String msg) {
// normal Android toast stuff using the context
}
}
/**
* Concrete JVM Notification factory
*
*/
public final class JvmNotificationFactory extends NotificationFactory {
@Override
public void newToast(String msg) {
// JVM toast stuff
}
}
/**
* A singleton used for accessing the factory.
*
*/
public enum Library {
Instance;
private NotificationFactory notificationFactory;
public NotificationFactory getNotificationFactory() {
// nb: add synchronized if needed
return notificationFactory;
}
public void setNotificationFactory(NotificationFactory notificationFactory) {
// nb: add synchronized if needed
this.notificationFactory = notificationFactory;
}
public void InitForAndroid(Context context) {
setNotificationFactory(new AndroidNotificationFactory(context));
}
public void InitForJvm() {
setNotificationFactory(new JvmNotificationFactory());
}
}
public class ExampleUsage {
/**
* @param args
*/
public static void main(String[] args) {
// init the library
// Library.Instance.InitForAndroid(context); //or:
Library.Instance.InitForJvm();
// usage
Library.Instance.getNotificationFactory().newToast("Test");
}
}
, bir IDialog
arayüzü ve bu arabirim uygulamak beton Android ve JVM sarmalayıcılarını yapmak gerekir.
Bağlam olmadan hiçbir şey yapamazsınız. belki de bildirimler için bir arabirim sağlayın ve kullanıcının bu arabirimi kullanmasına izin verin –