Add CalculateMoney with visitor pattern
This commit is contained in:
@@ -13,6 +13,7 @@ using System.Diagnostics;
|
|||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using TemplatePattern;
|
using TemplatePattern;
|
||||||
|
using VisitorPattern;
|
||||||
|
|
||||||
namespace BehavioralPatterns
|
namespace BehavioralPatterns
|
||||||
{
|
{
|
||||||
@@ -31,6 +32,7 @@ namespace BehavioralPatterns
|
|||||||
Console.Write("7: State Pattern\r\n");
|
Console.Write("7: State Pattern\r\n");
|
||||||
Console.Write("8: Strategy Pattern\r\n");
|
Console.Write("8: Strategy Pattern\r\n");
|
||||||
Console.Write("9: Template method Pattern\r\n");
|
Console.Write("9: Template method Pattern\r\n");
|
||||||
|
Console.Write("v: Visitor Pattern\r\n");
|
||||||
Console.Write("0: exit\r\n>");
|
Console.Write("0: exit\r\n>");
|
||||||
var key = Console.ReadKey();
|
var key = Console.ReadKey();
|
||||||
|
|
||||||
@@ -67,6 +69,9 @@ namespace BehavioralPatterns
|
|||||||
case '9':
|
case '9':
|
||||||
TemplatePatternExamples.Run();
|
TemplatePatternExamples.Run();
|
||||||
break;
|
break;
|
||||||
|
case 'v':
|
||||||
|
VisitorPatternExamples.Run();
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
if (key.KeyChar == '0')
|
if (key.KeyChar == '0')
|
||||||
break;
|
break;
|
||||||
|
|||||||
@@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,112 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
|
||||||
|
namespace VisitorPattern.CalculateMoney.WithVisitor
|
||||||
|
{
|
||||||
|
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)
|
||||||
|
{
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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());
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,173 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
|
||||||
|
namespace VisitorPattern.CalculateMoney.WithoutVisitor
|
||||||
|
{
|
||||||
|
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 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -2,6 +2,9 @@
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
using VisitorPattern.CalculateMoney;
|
||||||
|
using VisitorPattern.CalculateMoney.WithoutVisitor;
|
||||||
|
using VisitorPattern.CalculateMoney.WithVisitor;
|
||||||
|
|
||||||
namespace VisitorPattern
|
namespace VisitorPattern
|
||||||
{
|
{
|
||||||
@@ -13,7 +16,9 @@ namespace VisitorPattern
|
|||||||
|
|
||||||
public static void Run()
|
public static void Run()
|
||||||
{
|
{
|
||||||
|
CalculateMoneyMotivationalExample.Run();
|
||||||
|
|
||||||
|
CalculateMoneyWithVisitorExample.Run();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static string GetDescription()
|
public static string GetDescription()
|
||||||
|
|||||||
Reference in New Issue
Block a user