/// <summary> /// Problem: We need to notify our members, and mark them as notified. This is implemented via their implementations of the NotifyMember() method on the IMemberNotification interface. However, the user of type PayingUser also get an SMS. Solve the problem by implementing a template-method /// </summary> /// <remarks> /// Start by implementing an abstract base-class, that holds the combined functionality from the two member-notification implementations. /// </remarks> public static void Main() { Member bruceFreeMember = Member.CreateMember("Bruce Willis", "1-800-diehard", MemberType.FreeUser); FreeMemberNotification freeMemberNotificator = new FreeMemberNotification(bruceFreeMember); freeMemberNotificator.NotifyMember(); Member clintPayingUser = Member.CreateMember("Clint Eastwood", "1-800-mkemydy", MemberType.PayingUser); PayingMemberNotification payingMemberNotificator = new PayingMemberNotification(clintPayingUser); payingMemberNotificator.NotifyMember(); }
/// <summary> /// By introducing an abstract class with a template method that defines a default skeleton for notifying members, and allows the implementing classes to override certain steps, we have a more maintainable way of handling these things. /// </summary> private static void Main() { Member bruceFreeMember = Member.CreateMember("Bruce Willis", "1-800-diehard", MemberType.FreeUser); FreeMemberNotification freeMemberNotificator = new FreeMemberNotification(bruceFreeMember); freeMemberNotificator.NotifyMember(); Member clintPayingUser = Member.CreateMember("Clint Eastwood", "1-800-mkemydy", MemberType.PayingUser); PayingMemberNotification payingMemberNotificator = new PayingMemberNotification(clintPayingUser); payingMemberNotificator.NotifyMember(); // What did we achieve? We re-factored our code and made a case for maximum code re-use and flexibility - and later maintenance-benefits. }
public static void Main() { Member bruceFreeMember = Member.CreateMember("Bruce Willis", "1-800-diehard", MemberType.FreeUser); FreeMemberNotification freeMemberNotificator = new FreeMemberNotification(bruceFreeMember); freeMemberNotificator.NotifyMember(); Member clintPayingUser = Member.CreateMember("Clint Eastwood", "1-800-mkemydy", MemberType.PayingUser); PayingMemberNotification payingMemberNotificator = new PayingMemberNotification(clintPayingUser); payingMemberNotificator.NotifyMember(); // This works, but... Feels much like we should be able to re-use some code. // The template pattern helps us in this. // Lab-challenge: rewrite to utilize the Template Pattern. }