-
-
Save conanak99/c9296b3b5262b4b4e100fe2c5b5d599c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Interface | |
public interface IDatabase | |
{ | |
void Save(int orderId); | |
} | |
public interface ILogger | |
{ | |
void LogInfo(string info); | |
} | |
public interface IEmailSender | |
{ | |
void SendEmail(int userId); | |
} | |
// Các Module implement các Interface | |
public class Logger : ILogger | |
{ | |
public void LogInfo(string info) {} | |
} | |
public class Database : IDatabase | |
{ | |
public void Save(int orderId) {} | |
} | |
public class EmailSender : IEmailSender | |
{ | |
public void SendEmail(int userId) {} | |
} | |
// Hàm checkout mới sẽ như sau | |
public void Checkout(int orderId, int userId) | |
{ | |
// Nếu muốn thay đổi database, logger ta chỉ cần thay dòng code dưới | |
// Các Module XMLDatabase, SQLDatabase phải implement IDatabase | |
//IDatabase db = new XMLDatabase(); | |
//IDatebase db = new SQLDatabase(); | |
IDatabase db = new Database(); | |
db.Save(orderId); | |
ILogger log = new Logger(); | |
log.LogInfo("Order has been checkout"); | |
IEmailSender es = new EmailSender(); | |
es.SendEmail(userId); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment