Advertisement
STANAANDREY

dependency inversion principle

Sep 20th, 2023 (edited)
874
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.06 KB | None | 0 0
  1. import java.io.FileWriter;
  2. import java.io.IOException;
  3.  
  4. interface ILoger {
  5.     void log(final String message);
  6. }
  7.  
  8. class FileLogger implements ILoger {
  9.     private FileWriter fileWriter;
  10.     public FileLogger(final String fName) throws IOException {
  11.         fileWriter = new FileWriter(fName);
  12.     }
  13.  
  14.     @Override
  15.     public void log(final String message) {
  16.         try {
  17.             fileWriter.write(System.currentTimeMillis() + ": " + message);
  18.             fileWriter.close();
  19.         } catch (Exception e) {
  20.             System.err.println("ERROR: ");
  21.             e.printStackTrace();
  22.         }
  23.     }
  24. }
  25.  
  26. class DataAccessLayer {
  27.     private ILoger loger;
  28.     public DataAccessLayer(final ILoger loger) {
  29.         this.loger = loger;
  30.     }
  31.     public void addClient(final String name) {
  32.         loger.log("added " + name);
  33.     }
  34. }
  35.  
  36. public class Main {
  37.  
  38.     public static void main(String[] args) throws IOException {
  39.         var fl = new FileLogger("data.txt");
  40.         var dal = new DataAccessLayer(fl);
  41.         dal.addClient("Andrew");
  42.     }
  43. }
  44.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement