Skip to content

Instantly share code, notes, and snippets.

@matt-goldman
Last active April 15, 2021 10:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matt-goldman/bb904f5894456531fae7201a2e2bf03f to your computer and use it in GitHub Desktop.
Save matt-goldman/bb904f5894456531fae7201a2e2bf03f to your computer and use it in GitHub Desktop.
Interfaces
public class EmailNotifier : INotifier
{
public void Send(string subject, string body, Owner recipient)
{
// logic that sends email
}
}
public class FeedingTime
{
public void FeedPet(IEats pet)
{
var food = new Food();
pet.eat(food);
}
}
public class FeedingTime
{
private EmailService _myEmailService;
public FeedingTime()
{
_myEmailSerice = new EmailService(); // we probably have to pass in some SMTP details or something...
}
public void FeedPet(IEats pet)
{
var food = new Food();
pet.eat(food);
// assuming we have set up pet and owner properties for out pet...
_myEmailService.Send("Your pet has been fed", $"Your pet {pet.name} has been fed. Kind Regards, FeedingTime", pet.Owner.EmailAddress);
}
}
public class FeedingTime
{
private INotifier _notifier;
public FeedingTime(INotifier notifier)
{
_notifier = notifier;
}
public void FeedPet(IEats pet)
{
var food = new Food();
pet.eat(food);
// assuming we have set up pet and owner properties for out pet...
_notifier.Send("Your pet has been fed", $"Your pet {pet.name} has been fed. Kind Regards, FeedingTime", pet.Owner);
}
}
public interface ISwims
{
void swim();
}
public class Fish : ISwims, IEats, ISleeps
{
public void sleep()
{
// logic for when your fish is sleeping...
}
public void eat(Food food)
{
// logic for when your fish eats...
}
public void swim()
{
// logic for when your fish is swimming...
}
}
public class EmailNotifier : INotifier
{
private IGoldieEmail _goldieMail;
public EmailNotifier(IGoldieEmail goldieMail)
{
_goldieMail = goldieMail;
}
public void Send(string subject, string body, Owner recipient)
{
_goldieMail.SendEmail(body, subjcet, new string[] { recipient.EmailAddress });
}
}
public class Startup
{
// ommitted for clarity...
public void ConfigureServices(IServiceCollection services)
{
services.AddGoldieMail("senderAddress");
}
// ommitted for clarity...
}
public interface IEats
{
void eat(Food food);
}
public interface IGoldieEmail
{
void SendEmail(string body, string subject, string[] recipients);
}
public interface INotifier
{
void Send(string subject, string body, Owner recipient);
}
public interface ISleeps
{
void sleep();
}
public class Cat: IEats, ISleeps
{
public void sleep()
{
// logic for when your cat is sleeping...
}
public void eat(Food food)
{
// logic for when your cat eats...
}
}
public class Dog: IEats, ISleeps
{
public void sleep()
{
// logic for when your dog is sleeping...
}
public void eat(Food food)
{
// logic for when your dog eats...
}
}
public class Startup
{
// ommitted for clarity...
public void ConfigureServices(IServiceCollection services)
{
services.AddSingleton<INotifier, EmailNotifier>();
}
// ommitted for clarity...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment