From 67790912056e57eccacd743a38543397f08bef57 Mon Sep 17 00:00:00 2001 From: Petrutiu Mihai Date: Wed, 6 Jul 2016 15:08:23 +0300 Subject: [PATCH] Add dynamic visitor example --- ...CalculateMoneyWithDynamicVisitorExample.cs | 36 +++++++ .../DynamicVisitor/CorruptionSuspect.cs | 91 ++++++++++++++++ .../DynamicVisitor/SuspectVisitors.cs | 100 ++++++++++++++++++ src/VisitorPattern/VisitorPatternExamples.cs | 3 + src/VisitorPattern/project.json | 3 +- 5 files changed, 232 insertions(+), 1 deletion(-) create mode 100644 src/VisitorPattern/CalculateMoney/DynamicVisitor/CalculateMoneyWithDynamicVisitorExample.cs create mode 100644 src/VisitorPattern/CalculateMoney/DynamicVisitor/CorruptionSuspect.cs create mode 100644 src/VisitorPattern/CalculateMoney/DynamicVisitor/SuspectVisitors.cs diff --git a/src/VisitorPattern/CalculateMoney/DynamicVisitor/CalculateMoneyWithDynamicVisitorExample.cs b/src/VisitorPattern/CalculateMoney/DynamicVisitor/CalculateMoneyWithDynamicVisitorExample.cs new file mode 100644 index 0000000..c9975b1 --- /dev/null +++ b/src/VisitorPattern/CalculateMoney/DynamicVisitor/CalculateMoneyWithDynamicVisitorExample.cs @@ -0,0 +1,36 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace VisitorPattern.CalculateMoney.DynamicVisitor +{ + public class CalculateMoneyWithDynamicVisitorExample + { + 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/DynamicVisitor/CorruptionSuspect.cs b/src/VisitorPattern/CalculateMoney/DynamicVisitor/CorruptionSuspect.cs new file mode 100644 index 0000000..5f8c7a7 --- /dev/null +++ b/src/VisitorPattern/CalculateMoney/DynamicVisitor/CorruptionSuspect.cs @@ -0,0 +1,91 @@ +using System; +using System.Collections.Generic; + +namespace VisitorPattern.CalculateMoney.DynamicVisitor +{ + 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) + { + visitor.Visit((dynamic)asset); + } + } + } + + public interface IAsset + { + } + + public class Job : IAsset + { + public double Salary { get; set; } + + public string JobTitle { get; set; } + + public DateTime StartDate { get; set; } + } + + 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 class Valuable : IAsset + { + public double EstimatedValue { get; set; } + + public double InterestPerMonth { get; set; } + } + + 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; } + } +} diff --git a/src/VisitorPattern/CalculateMoney/DynamicVisitor/SuspectVisitors.cs b/src/VisitorPattern/CalculateMoney/DynamicVisitor/SuspectVisitors.cs new file mode 100644 index 0000000..1bd6d3c --- /dev/null +++ b/src/VisitorPattern/CalculateMoney/DynamicVisitor/SuspectVisitors.cs @@ -0,0 +1,100 @@ +using System; + +namespace VisitorPattern.CalculateMoney.DynamicVisitor +{ + public interface IVisitor + { + void Visit(MoneyBankAccount moneyBankAccount); + void Visit(Loan loan); + void Visit(Job job); + + void Visit(Valuable valuable); + void Visit(IAsset asset); + } + + public class NetWorthVisitor : IVisitor + { + public double NetWorth { get; private set; } + + public void Visit(Job job) + { + + } + + public void Visit(IAsset asset) + { + } + + 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(IAsset asset) + { + } + + 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(IAsset asset) + { + } + + 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/VisitorPatternExamples.cs b/src/VisitorPattern/VisitorPatternExamples.cs index 2d04333..8b02dbb 100644 --- a/src/VisitorPattern/VisitorPatternExamples.cs +++ b/src/VisitorPattern/VisitorPatternExamples.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using VisitorPattern.CalculateMoney; +using VisitorPattern.CalculateMoney.DynamicVisitor; using VisitorPattern.CalculateMoney.WithoutVisitor; using VisitorPattern.CalculateMoney.WithVisitor; @@ -19,6 +20,8 @@ namespace VisitorPattern CalculateMoneyMotivationalExample.Run(); CalculateMoneyWithVisitorExample.Run(); + + CalculateMoneyWithDynamicVisitorExample.Run(); } public static string GetDescription() diff --git a/src/VisitorPattern/project.json b/src/VisitorPattern/project.json index 864b9a5..1fb2f0d 100644 --- a/src/VisitorPattern/project.json +++ b/src/VisitorPattern/project.json @@ -1,7 +1,8 @@ -{ +{ "version": "1.0.0-*", "dependencies": { + "Microsoft.CSharp": "4.0.1", "NETStandard.Library": "1.6.0" },