Skip to content

Instantly share code, notes, and snippets.

Avatar
🏠
Working from home

Sukhpinder Singh ssukhpinder

🏠
Working from home
View GitHub Profile
View useCORpattern.cs
ClaimApprover junior = new Manager();
ClaimApprover sukhpinder = new Manager();
ClaimApprover singh = new SeniorManager();
junior.SetHierarchy(sukhpinder);
sukhpinder.SetHierarchy(singh);
Claim c1 = new Claim() { amount = 999, Id = 1001 };
Claim c2 = new Claim() { amount = 10001, Id = 1002 };
junior.ApproveRequest(c1);
View seniormanager.cs
public class SeniorManager : ClaimApprover
{
public override void ApproveRequest(Claim claim)
{
if (claim.amount > 1000 && claim.amount <= 10000)
{
System.Console.WriteLine($"Claim reference {claim.Id} with amount {claim.amount} is approved by Senior Manager");
}
else
{
View manager.cs
public class Manager : ClaimApprover
{
public override void ApproveRequest(Claim claim)
{
if (claim.amount >= 100 && claim.amount <= 1000)
{
System.Console.WriteLine($"Claim reference {claim.Id} with amount {claim.amount} is approved by Manager");
}
else if (claimApprover != null)
{
View junior.cs
public class Junior : ClaimApprover
{
public override void ApproveRequest(Claim claim)
{
System.Console.WriteLine("Cannot approve");
}
}
View corabstract.cs
public abstract class ClaimApprover
{
protected ClaimApprover claimApprover;
public void SetHierarchy(ClaimApprover claimApprover)
{
this.claimApprover = claimApprover;
}
public abstract void ApproveRequest(Claim claim);
}
View mediator-main.cs
static void Main(string[] args)
{
Chatroom chatroom = new Chatroom();
User Jennifer = new UserPersona(Username.Jennifer.ToString());
User Ashley = new UserPersona(Username.Ashley.ToString());
User David = new UserPersona(Username.David.ToString());
User Scott = new UserPersona(Username.Scott.ToString());
chatroom.Register(Jennifer);
chatroom.Register(Ashley);
View user-persona.cs
class UserPersona : User
{
public UserPersona(string name) : base(name) { }
public override void DM(string from, string message) => base.DM(from, message);
}
View user-mediator.cs
public class User
{
private Chatroom _chatroom;
private string _name;
public User(string name) => this._name = name;
public string Name => _name;
public Chatroom Chatroom
{
View abstract-clatroom.cs
public class Chatroom : AChatroom
{
private Dictionary<string, User> _users = new Dictionary<string, User>();
public override void Post(string fromUser, string toUser, string msg)
{
User participant = _users[toUser];
if (participant != null)
{
participant.DM(fromUser, msg);
View chatroom.cs
public abstract class AChatroom
{
public abstract void Register(User user);
public abstract void Post(string fromUser, string toUser, string msg);
}