Registry pattern in Android application


When developing an Android project, we have many trouble with context. Therefore I suggest a solution to avoid to pass context through layers. This solution is registry pattern.

What is registry pattern ?

You can read about registry pattern in this link.

In summary, we create all instances of business classes, datasource classes, network classes when starting app. If you need to use those classed, you can get it from registry service.

How to use registry pattern in Android project


Step 1: Create your Application class extend Application class Android.

You can read detail in this link

Screen Shot 2015-12-17 at 13.24.10

Step 2: Create ServiceRegistry class.

Screen Shot 2015-12-17 at 13.32.27

Step 3: Create and registry all classes that you need.

  • Create class, such as: PageDataSource class.Screen Shot 2015-12-17 at 13.34.13
  • Register PageDataSource in OnCreate method of MyApplication classScreen Shot 2015-12-17 at 13.35.44

Step 4: Use these classes.Screen Shot 2015-12-17 at 13.39.22

In conclusion

With this solution, you can avoid to pass context to classes, such as: datasource, business, network, … Moreover, you can use ApplicationContext, you can avoid error when using context of activity (error when activity is killed by OS, …). Moreover code will be more clean, and avoid to use static method (easy to write unit-test)

One thought on “Registry pattern in Android application

  1. grass bean

    I used to use business, datasource classes as singleton pattern, and have to pass context in getInstance() method. Using this pattern is a better way.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s