Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Dependency injection use in java or android

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 127
    Comment on it

    We know that there are lots of design patterns uses in Android during development like factory pattern, abstract factory pattern or singleton pattern etc so that object can be handle easily, Dependency injection is also a design pattern.

    We know that during development of project all codes should be in loose coupling that means they shouldn't be highly dependent on each other so to prevent tight coupling we uses Dependency injection so that we don't need to change the lots of code in case of any change in any part of our codes. So its purpose to make code flexible, maintainable and easy to test.

    Lets say we have a class to send sms i.e

    public class SMSService {
    
        public void sendSMS(String message, String subject){
            System.out.println("sms sent " + subject + " & message="+message);
        }
    }

    This class contains main logic to send sms.

    Now we will create a Application class to call SMS service i.e

    public class SMSApplication {
    
        private SMSService sms = new SMSService();
    
        public void processMessage(String msg, String sub){
            this.sms.sendSMS(msg, sub);
        }
    }

    Now in our main class we will call function to send sms like this :

    public class MainClass {
    
        public static void main(String[] args) {
            SMSApplication app = new SMSApplication();
            app.processMessage("Hi friends", "Hello!!!");
        }
    }

    here everything seems fine but if we check all the classes then it leads to high dependency on Application class because we can't send the sms without using Application class so Main class has dependency on Application class.

    And in future if we want to extend the futures of sms service like facebook or third party then we have to change the code and even have to create another application for this service that is not good.

    Also unit testing is not easy with this, we can't even mock the object in test classes.

    We can make some changes in Application class like to create constructor to initialize service class like this :

    public class MyApplication {
    
        private SMSService sms = null;
    
        public MyApplication(SMSService sms){
            this.sms=sms;
        }
    
        public void processMessages(String msg, String rec){
            this.sms.processMessage("Hi friends", "Hello!!!");
        }
    }

    But in this case we will need Application class to initialize SMS service so this is not a good decision.

    Now Dependency injection comes in place, we have to follow three steps to create dependency injection pattern :

    1. Create a Service interface that will define which type of message you want to send.

    2. Create a consumer interface that will work like a bridge and will help us to only process message this class don't know which type of message will process.

    3. Create a injector that will inject consumer type like email, sms, facebook etc.

    With below example you can see the basic steps :

    step 1.

    public interface MSGService {
    	void sendMsg(String msg, String rec);
    }
    public class SMSServiceClass implements MSGService {
    	@Override
    	public void sendMsg(String msg, String rec) {
    		System.out.println(msg + "sent to reciever :" +rec);
    	}
    }
    public class FBServiceClass implements MSGService {
    	@Override
    	public void sendMessage(String msg, String rec) {
    		System.out.println("FB msg sent to "+rec);
    	}
    }

    Step 2.

    public interface Consumer {
    	void processMessages(String msg, String rec);
    }
    public class MyDIApplication implements Consumer{
    	private MSGService service;
    	public MyApplication(MSGService svc){
    		this.service=svc;
    	}
    	@Override
    	public void processMessages(String msg, String rec){
    		this.service.sendMsg(msg, rec);
    	}
    }

    Step3.

    public interface ServiceInjector {
    	public Consumer getConsumer();
    }
    public class SMSServiceInjector implements ServiceInjector {
    	@Override
    	public Consumer getConsumer() {
    		return new MyApplication(new SMSServiceClass());
    	}
    }
    public class FBServiceInjector implements ServiceInjector {
    	@Override
    	public Consumer getConsumer() {
    		return new MyApplication(new FBServiceClass());
    	}
    }

    Now from main class we can send message like this

            ServiceInjector injector = null;
    		Consumer consumer = null;
    
    		injector = new SMSServiceInjector();
    		consumer = injector.getConsumer();
    		consumer.processMessages("for sms only", 0000000000);

     

    Now we don't have to much dependency on Application or client class. All codes are clear to understand and easy to test. In case we want twitter message service then we will have to create another service class for twitter and injector for twitter.In this way we can mock object during unit testing and we can change the Consumer code accordingly.

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: