Skip to content

Instantly share code, notes, and snippets.

@dinhnhat0401
Created February 18, 2016 03:26
Show Gist options
  • Save dinhnhat0401/3473468a39c2aa9d1a40 to your computer and use it in GitHub Desktop.
Save dinhnhat0401/3473468a39c2aa9d1a40 to your computer and use it in GitHub Desktop.
StatePattern
package com.aiming.link.purchase.model;
import com.aiming.link.purchase.AimingLinkPurchase;
import com.aiming.link.purchase.util.Purchase;
import java.util.List;
import lombok.Getter;
import lombok.Setter;
public class StateContext {
@Getter
@Setter
private Statelike state;
public StateContext () {
state = new StateNone();
}
public void onBegin(AimingLinkPurchase.BuyListener listener) {
state.onBegin(this, listener);
}
public void onFinished(AimingLinkPurchase.BuyListener listener, AimingLinkPurchase.BuyResult result, String details, String productId, String gameInfo, boolean isAlreadyDelivered, List<Purchase> restoreSucceededPurchases, List<Purchase> restoreFailedPurchase){
state.onFinished(this, listener, result, details, productId, gameInfo, isAlreadyDelivered, restoreSucceededPurchases, restoreFailedPurchase);
}
}
package com.aiming.link.purchase.model;
import com.aiming.link.purchase.AimingLinkPurchase;
import com.aiming.link.purchase.util.Purchase;
import java.util.List;
interface Statelike {
void onBegin(StateContext context, AimingLinkPurchase.BuyListener listener);
void onFinished(StateContext context, AimingLinkPurchase.BuyListener listener, AimingLinkPurchase.BuyResult result, String details, String productId, String gameInfo, boolean isAlreadyDelivered, List<Purchase> restoreSucceededPurchases, List<Purchase> restoreFailedPurchase);
}
class StatePurchasing implements Statelike{
@Override
public void onBegin(StateContext context, AimingLinkPurchase.BuyListener listener) {
if (!(context.getState() instanceof StateNone)) {
listener.onBuyFailure(AimingLinkPurchase.BuyResult.ERROR_PROCESS_CONFLICTED, "Process conflicted");
}
context.setState(new StatePurchasing());
}
// void onBuySuccess(String productId, String gameInfo, boolean isAlreadyDelivered);
// void onBuyFailure(BuyResult result, String details);
@Override
public void onFinished(StateContext context, AimingLinkPurchase.BuyListener listener, AimingLinkPurchase.BuyResult result, String details, String productId, String gameInfo, boolean isAlreadyDelivered, List<Purchase> restoreSucceededPurchases, List<Purchase> restoreFailedPurchase) {
if (result == null) {
// 購入成功
listener.onBuySuccess(productId,gameInfo,isAlreadyDelivered);
} else {
// 購入失敗
listener.onBuyFailure(result, details);
}
if (context.getState() instanceof StatePurchasing) {
context.setState(new StateNone());
}
}
}
// void onRestoreBegin();
// void onRestoreFinished(BuyResult result, String details, List<Purchase> restoreSucceededPurchases, List<Purchase> restoreFailedPurchase);
class StateRestoring implements Statelike{
@Override
public void onBegin(StateContext context, AimingLinkPurchase.BuyListener listener) {
if (!(context.getState() instanceof StateNone)) {
listener.onRestoreFinished(AimingLinkPurchase.BuyResult.ERROR_PROCESS_CONFLICTED, "Process conflicted", null, null);
}
listener.onRestoreBegin();
context.setState(new StateRestoring());
}
@Override
public void onFinished(StateContext context, AimingLinkPurchase.BuyListener listener, AimingLinkPurchase.BuyResult result, String details, String productId, String gameInfo, boolean isAlreadyDelivered, List<Purchase> restoreSucceededPurchases, List<Purchase> restoreFailedPurchase) {
listener.onRestoreFinished(result, details, restoreSucceededPurchases, restoreFailedPurchase);
context.setState(new StateNone());
}
}
class StateNone implements Statelike {
@Override
public void onBegin(StateContext context, AimingLinkPurchase.BuyListener listener) {
}
@Override
public void onFinished(StateContext context, AimingLinkPurchase.BuyListener listener, AimingLinkPurchase.BuyResult result, String details, String productId, String gameInfo, boolean isAlreadyDelivered, List<Purchase> restoreSucceededPurchases, List<Purchase> restoreFailedPurchase) {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment