diff --git a/src/BehavioralPatterns/Program.cs b/src/BehavioralPatterns/Program.cs index c64a350..d569d06 100644 --- a/src/BehavioralPatterns/Program.cs +++ b/src/BehavioralPatterns/Program.cs @@ -13,6 +13,7 @@ using System.Diagnostics; using System.Linq; using System.Threading.Tasks; using TemplatePattern; +using VisitorPattern; namespace BehavioralPatterns { @@ -31,6 +32,7 @@ namespace BehavioralPatterns Console.Write("7: State Pattern\r\n"); Console.Write("8: Strategy Pattern\r\n"); Console.Write("9: Template method Pattern\r\n"); + Console.Write("v: Visitor Pattern\r\n"); Console.Write("0: exit\r\n>"); var key = Console.ReadKey(); @@ -67,6 +69,9 @@ namespace BehavioralPatterns case '9': TemplatePatternExamples.Run(); break; + case 'v': + VisitorPatternExamples.Run(); + break; } if (key.KeyChar == '0') break; diff --git a/src/VisitorPattern/CalculateMoney/WithVisitor/CalculateMoneyWithVisitorExample.cs b/src/VisitorPattern/CalculateMoney/WithVisitor/CalculateMoneyWithVisitorExample.cs new file mode 100644 index 0000000..614f9f6 --- /dev/null +++ b/src/VisitorPattern/CalculateMoney/WithVisitor/CalculateMoneyWithVisitorExample.cs @@ -0,0 +1,36 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace VisitorPattern.CalculateMoney.WithVisitor +{ + public class CalculateMoneyWithVisitorExample + { + public static void Run() + { + var suspect = new CorruptionSuspect(); + + suspect.MoneyBankAccounts.Add(new MoneyBankAccount { Ammount = 50000, InterestPerMonth = 0.00, Bank = "ING" }); + suspect.MoneyBankAccounts.Add(new MoneyBankAccount { Ammount = 100000, InterestPerMonth = 0.02, Bank = "Bank of Uzbezkistan" }); + suspect.MoneyBankAccounts.Add(new MoneyBankAccount { Ammount = 50000, InterestPerMonth = 0.01, Bank = "Bank of Libia" }); + + suspect.Valuables.Add(new Clock { EstimatedValue = 2500, InterestPerMonth = 0.00 }); + suspect.Valuables.Add(new RealEstate { EstimatedValue = 250000, InterestPerMonth = 0.25 }); + + suspect.Loans.Add(new Loan { MonthlyPayment = 4000, Owed = 100000 }); + + MonthlyIncomeVisitor mVisitor = new MonthlyIncomeVisitor(); + suspect.Accept(mVisitor); + Console.WriteLine("Monthly income: {0} ", mVisitor.MonthlyIncome); + + NetWorthVisitor nVisitor = new NetWorthVisitor(); + suspect.Accept(nVisitor); + Console.WriteLine("NetWorth: {0}", nVisitor.NetWorth); + + NetWorth2Visitor n2Visitor = new NetWorth2Visitor(); + suspect.Accept(n2Visitor); + Console.WriteLine("NetWorth2: {0}", n2Visitor.NetWorth); + } + } +} diff --git a/src/VisitorPattern/CalculateMoney/WithVisitor/CorruptionSuspect.cs b/src/VisitorPattern/CalculateMoney/WithVisitor/CorruptionSuspect.cs new file mode 100644 index 0000000..8189e61 --- /dev/null +++ b/src/VisitorPattern/CalculateMoney/WithVisitor/CorruptionSuspect.cs @@ -0,0 +1,112 @@ +using System; +using System.Collections.Generic; + +namespace VisitorPattern.CalculateMoney.WithVisitor +{ + public class CorruptionSuspect : IAsset + { + public CorruptionSuspect() + { + MoneyBankAccounts = new List(); + Valuables = new List(); + Loans = new List(); + Jobs = new List(); + + } + public List MoneyBankAccounts { get; set; } + + public List Valuables { get; set; } + + public List Loans { get; set; } + + public List Jobs { get; set; } + + public List Assets + { + get + { + List assets = new List(); + assets.AddRange(MoneyBankAccounts); + assets.AddRange(Valuables); + assets.AddRange(Loans); + assets.AddRange(Jobs); + return assets; + } + } + + public void Accept(IVisitor visitor) + { + foreach (var asset in Assets) + { + asset.Accept(visitor); + } + } + } + + public interface IAsset + { + void Accept(IVisitor visitor); + } + + public class Job : IAsset + { + public double Salary { get; set; } + + public string JobTitle { get; set; } + + public DateTime StartDate { get; set; } + + public void Accept(IVisitor visitor) + { + visitor.Visit(this); + } + } + + public class MoneyBankAccount : IAsset + { + public double Ammount { get; set; } + public double InterestPerMonth { get; set; } + + public string Bank { get; set; } + + public double GetNetWorth() + { + return Ammount; + } + + public void Accept(IVisitor visitor) + { + visitor.Visit(this); + } + } + + public class Valuable : IAsset + { + public double EstimatedValue { get; set; } + + public double InterestPerMonth { get; set; } + + public void Accept(IVisitor visitor) + { + visitor.Visit(this); + } + } + + public class Clock : Valuable { } + + public class RealEstate : Valuable { } + + public class Art : Valuable { } + + public class Loan : IAsset + { + public double Owed { get; set; } + + public double MonthlyPayment { get; set; } + + public void Accept(IVisitor visitor) + { + visitor.Visit(this); + } + } +} diff --git a/src/VisitorPattern/CalculateMoney/WithVisitor/SuspectVisitors.cs b/src/VisitorPattern/CalculateMoney/WithVisitor/SuspectVisitors.cs new file mode 100644 index 0000000..5cb3244 --- /dev/null +++ b/src/VisitorPattern/CalculateMoney/WithVisitor/SuspectVisitors.cs @@ -0,0 +1,87 @@ +using System; + +namespace VisitorPattern.CalculateMoney.WithVisitor +{ + public interface IVisitor + { + void Visit(MoneyBankAccount moneyBankAccount); + void Visit(Loan loan); + void Visit(Job job); + + void Visit(Valuable valuable); + } + + public class NetWorthVisitor : IVisitor + { + public double NetWorth { get; private set; } + + public void Visit(Job job) + { + + } + + public void Visit(Valuable valuable) + { + NetWorth += valuable.EstimatedValue; + } + + public void Visit(Loan loan) + { + NetWorth -= loan.Owed; + } + + public void Visit(MoneyBankAccount moneyBankAccount) + { + NetWorth += moneyBankAccount.Ammount; + } + } + + public class NetWorth2Visitor : IVisitor + { + public double NetWorth { get; private set; } + + public void Visit(Job job) + { + + } + + public void Visit(Valuable valuable) + { + NetWorth += valuable.EstimatedValue; + } + + public void Visit(Loan loan) + { + NetWorth -= loan.Owed + 215; + } + + public void Visit(MoneyBankAccount moneyBankAccount) + { + NetWorth += moneyBankAccount.Ammount + 210; + } + } + + public class MonthlyIncomeVisitor : IVisitor + { + public double MonthlyIncome { get; private set; } + public void Visit(Job job) + { + MonthlyIncome += job.Salary; + } + + public void Visit(Valuable valuable) + { + + } + + public void Visit(Loan loan) + { + MonthlyIncome -= loan.MonthlyPayment; + } + + public void Visit(MoneyBankAccount moneyBankAccount) + { + MonthlyIncome += moneyBankAccount.InterestPerMonth * moneyBankAccount.Ammount; + } + } +} diff --git a/src/VisitorPattern/CalculateMoney/WithoutVisitor/CalculateMoneyMotivationalExample.cs b/src/VisitorPattern/CalculateMoney/WithoutVisitor/CalculateMoneyMotivationalExample.cs new file mode 100644 index 0000000..e0f9fa7 --- /dev/null +++ b/src/VisitorPattern/CalculateMoney/WithoutVisitor/CalculateMoneyMotivationalExample.cs @@ -0,0 +1,29 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace VisitorPattern.CalculateMoney.WithoutVisitor +{ + public class CalculateMoneyMotivationalExample + { + public static void Run() + { + var suspect = new CorruptionSuspect(); + + suspect.MoneyBankAccounts.Add(new MoneyBankAccount { Ammount = 50000, InterestPerMonth = 0.00, Bank = "ING" }); + suspect.MoneyBankAccounts.Add(new MoneyBankAccount { Ammount = 100000, InterestPerMonth = 0.02, Bank = "Bank of Uzbezkistan" }); + suspect.MoneyBankAccounts.Add(new MoneyBankAccount { Ammount = 50000, InterestPerMonth = 0.01, Bank = "Bank of Libia" }); + + suspect.Valuables.Add(new Clock { EstimatedValue = 2500, InterestPerMonth = 0.00 }); + suspect.Valuables.Add(new RealEstate { EstimatedValue = 250000, InterestPerMonth = 0.25 }); + + suspect.Loans.Add(new Loan { MonthlyPayment = 4000, Owed = 100000 }); + + Console.WriteLine("Monthly income: {0} ", suspect.GetMonthlyIncome()); + Console.WriteLine("NetWorth: {0} ", suspect.GetNetWorth()); + Console.WriteLine("NetWorth2: {0} ", suspect.GetNetWorth2()); + + } + } +} diff --git a/src/VisitorPattern/CalculateMoney/WithoutVisitor/CorruptionSuspect.cs b/src/VisitorPattern/CalculateMoney/WithoutVisitor/CorruptionSuspect.cs new file mode 100644 index 0000000..813c0bd --- /dev/null +++ b/src/VisitorPattern/CalculateMoney/WithoutVisitor/CorruptionSuspect.cs @@ -0,0 +1,173 @@ +using System; +using System.Collections.Generic; + +namespace VisitorPattern.CalculateMoney.WithoutVisitor +{ + public class CorruptionSuspect : IAsset + { + public CorruptionSuspect() + { + MoneyBankAccounts = new List(); + Valuables = new List(); + Loans = new List(); + Jobs = new List(); + + } + public List MoneyBankAccounts { get; set; } + + public List Valuables { get; set; } + + public List Loans { get; set; } + + public List Jobs { get; set; } + + public List Assets + { + get + { + List assets = new List(); + assets.AddRange(MoneyBankAccounts); + assets.AddRange(Valuables); + assets.AddRange(Loans); + assets.AddRange(Jobs); + return assets; + } + } + + public double GetNetWorth() + { + double netWorth = 0; + foreach (var asset in Assets) + { + netWorth += asset.GetNetWorth(); + } + return netWorth; + } + + public double GetNetWorth2() + { + double netWorth = 0; + foreach (var asset in Assets) + { + netWorth += asset.GetNetWorth2(); + } + return netWorth; + } + + public double GetMonthlyIncome() + { + double monthlyIncome = 0; + foreach (var asset in Assets) + { + monthlyIncome += asset.GetMonthlyIncome(); + } + return monthlyIncome; + } + } + + public interface IAsset + { + double GetNetWorth(); + + double GetNetWorth2(); + + double GetMonthlyIncome(); + } + + public class Job : IAsset + { + public double Salary { get; set; } + + public string JobTitle { get; set; } + + public DateTime StartDate { get; set; } + + public double GetNetWorth() + { + return 0; + } + + public double GetNetWorth2() + { + return 0; + } + + public double GetMonthlyIncome() + { + return Salary; + } + } + + public class MoneyBankAccount : IAsset + { + public double Ammount { get; set; } + public double InterestPerMonth { get; set; } + + public string Bank { get; set; } + + public double GetNetWorth() + { + return Ammount; + } + + public double GetNetWorth2() + { + return Ammount + 210; + } + + public double GetMonthlyIncome() + { + return InterestPerMonth * Ammount; + } + } + + public class Valuable : IAsset + { + public double EstimatedValue { get; set; } + + public double InterestPerMonth { get; set; } + + public double GetMonthlyIncome() + { + return 0; + } + + public double GetNetWorth() + { + return EstimatedValue; + } + + public double GetNetWorth2() + { + return EstimatedValue; + } + } + + public class Clock : Valuable { } + + public class RealEstate : Valuable { } + + public class Art : Valuable { } + + public class Loan : IAsset + { + public double Owed { get; set; } + + public double MonthlyPayment { get; set; } + + public double GetNetWorth() + { + return -Owed; + } + + public double GetNetWorth2() + { + return -(Owed + 215); + } + + public double GetMonthlyIncome() + { + return -MonthlyPayment; + } + } +} diff --git a/src/VisitorPattern/VisitorPatternExamples.cs b/src/VisitorPattern/VisitorPatternExamples.cs index c460d41..2d04333 100644 --- a/src/VisitorPattern/VisitorPatternExamples.cs +++ b/src/VisitorPattern/VisitorPatternExamples.cs @@ -2,6 +2,9 @@ using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; +using VisitorPattern.CalculateMoney; +using VisitorPattern.CalculateMoney.WithoutVisitor; +using VisitorPattern.CalculateMoney.WithVisitor; namespace VisitorPattern { @@ -13,7 +16,9 @@ namespace VisitorPattern public static void Run() { + CalculateMoneyMotivationalExample.Run(); + CalculateMoneyWithVisitorExample.Run(); } public static string GetDescription()