Skip to content

Instantly share code, notes, and snippets.

@zmarkan
Created December 17, 2015 10:23
Show Gist options
  • Save zmarkan/262c8547779298812227 to your computer and use it in GitHub Desktop.
Save zmarkan/262c8547779298812227 to your computer and use it in GitHub Desktop.
Cooking Clean Architecture sample code, used to present on Codemotion Tel Aviv 2015 (Absolutely non compilable pseudocode samples to show concepts)
// Sample basket data entity
class BasketItem{
int id;
String name;
String imageUrl;
int quantity;
float price;
}
// Model - location wrapper
interface LocationWrapper {
void requestLocation(…);
}
class LocationWrapperImpl implements LocationWrapper {
GoogleApiClient apiClient;
@Override void requestLocation(…){
// setup callbacks etc…
LocationServices.FusedLocationApi.getLastLocation( apiClient);
}
}
// Presenter
interface BasketPresenter {
void initialise(BasketView view);
void itemQuantityChanged(BasketItem basketItem);
void viewDestroyed();
}
// Clean presenter with interactors
interface Interactor<T> {
void performAction<T action>(InteractionCallback<T> actionCallback);
}
class CleanBasketPresenterImpl implements BasketPresenter {
@Override void initialise(BasketView view){
basketView = view;
}
@Override void itemQuantityChanged(BasketItem basketItem){
updateItemQuantityInteractor.performAction(basketItem);
}
}
// Cleanish presenter with tight coupling to the API
class CleanishBasketPresenterImpl implements BasketPresenter {
@Override void initialise(BasketView view){
basketView = view;
}
@Override void itemQuantityChanged(BasketItem basketItem){
basketAPI.updateItemQuantity(basketItem);
}
}
// View
interface BasketView {
void showItems(List<BasketItem> basketItems);
void update(BasketItem basketItem);
void proceedToCheckout();
}
class BasketActivity extends Activity implements BasketView {
BasketPresenter presenter;
Button button;
@Override public void onCreate(Bundle icicle){
// view stuff
presenter.initialise(this);
button.setOnClickListener(addToBasketListener);
}
OnClickListener addToBasketListener = new OnClickListener {
@Override public void onClick(View v){
presenter.itemQuantityChanged(basketItemWithNewQuantity);
}
}
@Override void update(BasketItem basketItem){
basketViewsList.getView(basketItem.id).setData(basketItem);
}
@Override void proceedToCheckout(){
startActivity(CheckoutActivity.buildIntent(this, basketItems));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment