static void Main(string[] args) { /* * More info: https://en.wikipedia.org/wiki/Mediator_pattern * What problems can the Mediator design pattern solve? * Tight coupling between a set of interacting objects should be avoided. * It should be possible to change the interaction between a set of objects independently. * * Defining a set of interacting objects by accessing and updating each other directly is inflexible because it tightly couples the objects to each other and makes * it impossible to change the interaction independently from (without having to change) the objects. * * And it stops the objects from being reusable and makes them hard to test. * Tightly coupled objects are hard to implement, change, test, and reuse because they refer to and know about many different objects. */ IChatMediator mediator = new ChatMediator(); IUser user1 = new User(mediator, "Hamid"); IUser user2 = new User(mediator, "Lisa"); IUser user3 = new User(mediator, "Saurabh"); IUser user4 = new User(mediator, "David"); IUser vipUser = new VipUser(mediator, "Hans"); mediator.AddUser(user1); mediator.AddUser(user2); mediator.AddUser(user3); mediator.AddUser(user4); mediator.AddUser(vipUser); user1.Send("Hi All"); }
private static void Main(string[] args) { /* * In this example we have a simple decorator which extends StringBuilder * type of .Net and add it the functionality of string concatenation by += operator * and implicit conversion from string to it's type. */ var assignableSb = new AssignableStringBuilder(); assignableSb = "Hello"; assignableSb += " World"; WriteLine(assignableSb); /* * Decorator pattern is used to add or extend functionality of an * existing type. In this example we decorate the basic User class * to have a last name when it becomes a VipUser. */ var user = new User("John", 24); var vipUser = new VipUser(user, "Doe"); WriteLine(user); WriteLine(vipUser); /* * Demonstration of C# extension methods to apply decorator pattern. */ var user2 = new User("Jane", 17); var ageInfo = user2.IsAdult() ? "an adult" : "not an adult"; WriteLine($"{user2} is {ageInfo}"); var vipUser2 = user2.ConvertToVip("Doe"); WriteLine($"{vipUser2} is a VIP user now."); /* * Course exercise */ var dragon = new Dragon() { Age = 0 }; WriteLine(dragon.Fly()); WriteLine(dragon.Crawl()); }
/// <summary> /// Get Users on VipList. /// </summary> /// <param name="online">If online.</param> /// <returns></returns> private List<VipUser> GetUsers(bool? online) { List<VipUser> users = new List<VipUser>(); uint begin = Memory.Addresses.VipList.VipBegin; uint distc = Memory.Addresses.VipList.VipDist; for (uint i = 0; i < Memory.Addresses.VipList.VipMaxD; i++) { VipUser user = new VipUser(Memory, begin + (i * distc)); if (user.Id > 0) { if (online.HasValue) { if (online.Value && user.IsOnline) { users.Add(user); } else if (!online.Value && !user.IsOnline) { users.Add(user); } } else { users.Add(user); } } else { break; } } return users; }