Ejemplos Dependency Inversion
public class ConsoleLogger { public void LogMessage(string message) { Console.WriteLine(message); } } public class NotificationService { private ConsoleLogger _logger = new ConsoleLogger(); public void Notify(string message) { // ... some notification logic ... _logger.LogMessage(message); } }
//Interface for logging public interface ILogger { void LogMessage(string message); } //Concrete Loggers public class ConsoleLogger : ILogger { public void LogMessage(string message) { Console.WriteLine(message); } } public class FileLogger : ILogger { private string _filePath; public FileLogger(string filePath) { _filePath = filePath; } public void LogMessage(string message) { // Just a simple example. In a real-world scenario, proper exception handling and file IO management is needed. File.AppendAllText(_filePath, message); } } //Now, our NotificationService should depend on the abstraction public class NotificationService { private ILogger _logger; public NotificationService(ILogger logger) { _logger = logger; } public void Notify(string message) { // ... some notification logic ... _logger.LogMessage(message); } } //Testing the Dependency Inversion Principle public class Program { public static void Main() { //Now, when initializing the NotificationService, //we can decide which logger to use: var consoleLogger = new ConsoleLogger(); var notificationService1 = new NotificationService(consoleLogger); var fileLogger = new FileLogger("path_to_log_file.txt"); var notificationService2 = new NotificationService(fileLogger); Console.ReadKey(); } }
public class Database { public void Save(String data) { System.out.printf("Saving note to database: %s\n", data); } public String Retrieve(int id) { return String.format("Note %d from database", id); } } public class NoteManager { private Database db = new Database(); public void SaveNote(String note) { db.Save(note); } public String GetNote(int id) { return db.Retrieve(id); } }
public interface IDataStore { void Save(String data); String Retrieve(int id); } public class Database implements IDataStore { public void Save(String data) { System.out.printf("Saving note to database: %s\n", data); } public String Retrieve(int id) { return String.format("Note %d from database", id); } } public class CloudStorage implements IDataStore { public void Save(String data) { System.out.printf("Saving note to cloud: %s\n", data); } public String Retrieve(int id) { return String.format("Note %d from cloud", id); } } //The NoteManager now relies on an abstraction public class NoteManager { private final IDataStore dataStore; public NoteManager(IDataStore dataStore) { this.dataStore = dataStore; } public void SaveNote(String note) { dataStore.Save(note); } public String GetNote(int id) { return dataStore.Retrieve(id); } } //Testing the Dependency Inversion Principle public class Program { public static void main(String[] args) { IDataStore dbStore = new Database(); NoteManager noteManagerWithDB = new NoteManager(dbStore); noteManagerWithDB.SaveNote("My first note."); System.out.println(noteManagerWithDB.GetNote(1)); IDataStore cloudStore = new CloudStorage(); NoteManager noteManagerWithCloud = new NoteManager(cloudStore); noteManagerWithCloud.SaveNote("My cloud note."); System.out.println(noteManagerWithCloud.GetNote(2)); new java.util.Scanner(System.in).nextLine(); } }