The text below is selected, press Ctrl+C to copy to your clipboard. (⌘+C on Mac) No line numbers will be copied.
Guest
SOLID 5 - Dependency Inversion
By jaroslav.jaros on 14th August 2019 07:58:32 AM | Syntax: JAVA | Views: 0



New paste | Download | Show/Hide line no. | Copy text to clipboard
  1. /**
  2.  * Violation of Dependency Inversion Principle
  3.  * <p>
  4.  * S - Single Responsibility Principle
  5.  * O - Open-closed Principle
  6.  * L - Liskov Substitution Principle
  7.  * I - Interface Segregation Principle
  8.  * D - Dependency Inversion Principle
  9.  * <p>
  10.  * For more information about SOLID principle @see <a href="https://en.wikipedia.org/wiki/SOLID">https://en.wikipedia.org/wiki/SOLID</a>
  11.  */
  12.  
  13. package com.solarwinds.interview.dependencyinversion;
  14.  
  15. import java.sql.Connection;
  16.  
  17. class MySQLConnector {
  18.  
  19.     public Connection openConnection() {
  20.         return null; //to be implemented
  21.     }
  22.    
  23.     public void closeConnection(Connection connection) {
  24.         //to be implemented
  25.     }
  26.  
  27.     public String getValueFromTable(String tableName, String key){
  28.         return null; //to be implemented
  29.     }
  30. }
  31.  
  32. class PasswordReminder {
  33.    
  34.     private MySQLConnector connector;
  35.  
  36.     public PasswordReminder(MySQLConnector connector) {
  37.         this.connector = connector;
  38.     }
  39.    
  40.     public String getLostPassword(String userName){
  41.         return this.connector.getValueFromTable("users", userName);
  42.     }
  43. }
  44.  
  45. public class DependencyInversionMain {
  46.    
  47.     public static void main(String[] args){
  48.         PasswordReminder reminder = new PasswordReminder(new MySQLConnector());
  49.         String password = reminder.getLostPassword("admin");
  50.         System.out.println("your password is :"+password);
  51.         //we want to hadd a new connector : MongoDBConnector
  52.     }
  53. }



  • Recent Pastes