Beispiel #1
0
        static void Main(string[] args)
        {
            // Represent an operation to be performed on the elements of an object structure.
            // Visitor lets you define a new operation without changing the classes of the elements on which it operates.

            var person = new Person();

            person.Assets.Add(new BankAccount {
                Amount = 1000, MonthlyInterest = 0.01
            });
            person.Assets.Add(new BankAccount {
                Amount = 2000, MonthlyInterest = 0.02
            });
            person.Assets.Add(new RealEstate {
                EstimatedValue = 79000, MonthlyRent = 500
            });
            person.Assets.Add(new Loan {
                Owed = 40000, MonthlyPayment = 40
            });

            var networth = new NetworthVisitor();

            person.Accept(networth);
            Console.WriteLine($"Networth: ${networth.Total}");

            var monthlyIncome = new MonthlyIncomeVisitor();

            person.Accept(monthlyIncome);
            Console.WriteLine($"Monthly income: ${monthlyIncome.Total}");

            Console.ReadKey();
        }
Beispiel #2
0
        public void VisitorPatter_TestUseOfVisitor()
        {
            var person = new Person();

            person.Assets.Add(new BankAccount {
                Amount = 1000, MonthlyInterest = 0.01
            });
            person.Assets.Add(new BankAccount {
                Amount = 2000, MonthlyInterest = 0.02
            });
            person.Assets.Add(new RealEstate {
                EstimatedValue = 79000, MonthlyRent = 500
            });
            person.Assets.Add(new Loan {
                Owed = 40000, MonthlyPayment = 40
            });

            var netWorthVisitor = new NetWorthVisitor();
            var incomeVisitor   = new IncomeVisitor();

            person.Accept(netWorthVisitor);
            person.Accept(incomeVisitor);

            Console.WriteLine(netWorthVisitor.Total);
            Console.WriteLine(incomeVisitor.Amount);
        }
Beispiel #3
0
        public int CalcNetWorth()
        {
            var networthVisitor = new NetWorthVisior();

            _person.Accept(networthVisitor);

            return(networthVisitor.Total);
        }
            public Beer ServeAnyBeerTo(Person person)
            {
                Beer beer = beerInventory.Last();

                person.Accept(beer);
                return(beer);
            }
        private static void NetWorthDemo()
        {
            var person = new Person();
            person.Assets.Add(new BankAccount { Amount = 10000, MonthlyInterest = 0.005M });
            person.Assets.Add(new BankAccount { Amount = 1000, MonthlyInterest = 0.01M });
            person.Assets.Add(new RealEstate { EstimatedValue = 100000, MonthlyRent = 500 });
            person.Assets.Add(new Loan { Owed = 30000, MonthlyPayment = 300 });

            var netWorthVisitor = new NetWorthVisitor();
            person.Accept(netWorthVisitor);
            Console.WriteLine("Total value of assets: {0}", netWorthVisitor.Total);

            var monthlyIncomeVisitor = new MonthlyIncomeVisitor();
            person.Accept(monthlyIncomeVisitor);
            Console.WriteLine("Total monthly income: {0}", monthlyIncomeVisitor.Total);
        }
Beispiel #6
0
        public override void Run()
        {
            var bank = new Bank();

            var person1  = new Person("Person_1", 20);
            var company1 = new Company("Company_1", "123456", 1990);
            var person2  = new Person("Person_2", 30);

            var xmlVisitor  = new XmlVisitor();
            var htmlVisitor = new HtmlVisitor();

            bank.AddAccount(person1);
            bank.AddAccount(company1);

            bank.Accept(xmlVisitor);
            bank.Accept(htmlVisitor);

            person2.Accept(xmlVisitor);
            person2.Accept(htmlVisitor);
        }
Beispiel #7
0
        public void Calculate_Monthly_Income()
        {
            var person = new Person();

            person.Assets.Add(new BankAccount {
                Amount = 1000, InterestRate = 0.05m
            });
            person.Assets.Add(new BankAccount {
                Amount = 2000, InterestRate = 0.1m
            });
            person.Assets.Add(new RealEstate {
                EstimatedValue = 79000, MonthlyRent = 2000
            });
            person.Assets.Add(new Loan {
                Owed = 40000, MonthlyPayment = 100
            });

            var incomeVisitor = new MonthlyIncomeVisitor();

            person.Accept(incomeVisitor);

            Assert.Equal(1920.84m, incomeVisitor.Amount);
        }
        public void Calculate_Net_Worth()
        {
            var person = new Person();

            person.Assets.Add(new BankAccount {
                Amount = 1000, InterestRate = 0.05m
            });
            person.Assets.Add(new BankAccount {
                Amount = 2000, InterestRate = 0.1m
            });
            person.Assets.Add(new RealEstate {
                EstimatedValue = 79000, MonthlyRent = 2000
            });
            person.Assets.Add(new Loan {
                Owed = 40000, MonthlyPayment = 100
            });

            var netWorthVisitor = new NetWorthVisitor();

            person.Accept(netWorthVisitor);

            Assert.Equal(42000, netWorthVisitor.Total);
        }
 public Beer ServeBeerTo(Beer beer, Person person)
 {
     person.Accept(beer);
     return(beer);
 }
 public TVal GetValue(Person p)
 {
     _val = default(TVal);
     p.Accept(this);
     return(_val);
 }