Add dynamic visitor example

This commit is contained in:
Petrutiu Mihai
2016-07-06 15:08:23 +03:00
parent 3c1aa09ec0
commit 6779091205
5 changed files with 232 additions and 1 deletions

View File

@@ -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);
}
}
}

View File

@@ -0,0 +1,91 @@
using System;
using System.Collections.Generic;
namespace VisitorPattern.CalculateMoney.DynamicVisitor
{
public class CorruptionSuspect : IAsset
{
public CorruptionSuspect()
{
MoneyBankAccounts = new List<MoneyBankAccount>();
Valuables = new List<Valuable>();
Loans = new List<Loan>();
Jobs = new List<Job>();
}
public List<MoneyBankAccount> MoneyBankAccounts { get; set; }
public List<Valuable> Valuables { get; set; }
public List<Loan> Loans { get; set; }
public List<Job> Jobs { get; set; }
public List<IAsset> Assets
{
get
{
List<IAsset> assets = new List<IAsset>();
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; }
}
}

View File

@@ -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;
}
}
}

View File

@@ -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()

View File

@@ -1,7 +1,8 @@
{
{
"version": "1.0.0-*",
"dependencies": {
"Microsoft.CSharp": "4.0.1",
"NETStandard.Library": "1.6.0"
},