Skip to content

Instantly share code, notes, and snippets.

View amitkma's full-sized avatar
🏠
Working from home

Amit Kumar amitkma

🏠
Working from home
View GitHub Profile
class ServiceLocator...
private static ServiceLocator sInstance;
public static void load(ServiceLocator arg) {
sInstance = arg;
}
private Map services = new HashMap();
public static Object getService(String key){
return sInstance.services.get(key);
}
public void loadService (String key, Object service) {
@Module
public class BookFinderModule {
@Provides
BookFinder provideBookFinder() {
return new OxfordBookFinder();
}
@Provides
@LocalFinder
BookFinder provideLocalBookFinder() {
return new LocalLibraryBookFinder();
public interface BookFinder {
List findAll();
}
class Library…
private BookFinder finder;
public Library() {
finder = new(RemoteBookFinder("https://oxfordlibrary.com/books"); // Can be a local database implementation as well.
}
....
class Library…
public Book[] booksAuthoredBy(String arg) {
List allBooks = finder.findAll();
for (Iterator it = allBooks.iterator(); it.hasNext();) {
Book book = (Book) it.next();
if (!book.getAuthor().equals(arg)) it.remove();
}
return (Book[]) allBooks.toArray(new Book[allBooks.size()]);
}
@Inject Map<MessageType, Provider<ViewHolder>> mViewHolders
//...
for (Message message : messages) {
mViewHolders.get(message.getType()).get().display(message);
}
@Module
public abstract class ViewHolderModule {
@Binds
@IntoMap
@ViewHolderKey(MessageType.TEXT_MESSAGE_TYPE)
abstract ViewHolder bindUserViewModule(TextMessageViewHolder textMessageViewHolder);
@Binds
@IntoMap
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@MapKey
@interface ViewHolderKey {
MessageType value();
}
public enum MessageType {
TEXT_MESSAGE_TYPE,
IMAGE_MESSAGE_TYPE,
INFO_MESSAGE_TYPE;
}
public class MainPresenterImpl implements MainContract.Presenter {
private final MainContract.View mView;
private final DataManager mDataManager;
// Constructor with view and datamanger parameters.
public MainPresenterImpl(MainContract.View view, DataManager dataManager) {
this.mView = view;
mDataManager = dataManager;
mView.setPresenter(this); // Set presenter to the mView.
public class MainActivity extends AppCompatActivity implements MainContract.View, View.OnClickListener {
// Contract Presenter
private MainContract.Presenter mPresenter;
......
// Other fields
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);