Skip to content

Instantly share code, notes, and snippets.

@tuannguyenssu tuannguyenssu/DIPAfter.cs Secret

Created Jul 7, 2019
Embed
What would you like to do?
// 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
You can’t perform that action at this time.