Sajmon

Composite Pattern

Apr 9th, 2012
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.95 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace Composite.Pattern
  7. {
  8.     public interface BankAccount
  9.     {
  10.         void Add(BankAccount account);
  11.         void Remove(BankAccount account);
  12.         BankAccount Get(BankAccount account);
  13.         int GetMoney();
  14.     }
  15. }
  16.  
  17.  
  18.  
  19. using System;
  20. using System.Collections.Generic;
  21. using System.Linq;
  22. using System.Text;
  23.  
  24. namespace Composite.Pattern
  25. {
  26.     public class Bank : BankAccount {
  27.  
  28.         private List<BankAccount> listOfAccounts = new List<BankAccount>();
  29.  
  30.         public void Add(BankAccount account)
  31.         {
  32.             this.listOfAccounts.Add(account);
  33.         }
  34.  
  35.         public void Remove(BankAccount account)
  36.         {
  37.             this.listOfAccounts.Remove(account);
  38.         }
  39.  
  40.         public BankAccount Get(BankAccount account) {
  41.             return this.listOfAccounts.Find(delegate(BankAccount acc) { return acc == account; });
  42.         }
  43.  
  44.         public int GetMoney()
  45.         {
  46.             int sum = 0;
  47.             foreach (BankAccount acc in listOfAccounts) {
  48.                 sum += acc.GetMoney();
  49.             }
  50.             return sum;
  51.         }
  52.     }
  53. }
  54.  
  55.  
  56.  
  57. using System;
  58. using System.Collections.Generic;
  59. using System.Linq;
  60. using System.Text;
  61.  
  62. namespace Composite.Pattern
  63. {
  64.     public class SpecifyAccount : BankAccount {
  65.  
  66.         private int cash;
  67.  
  68.         public void InsertMoney(int cash)
  69.         {
  70.             this.cash += cash;
  71.         }
  72.  
  73.         public int GetMoney()
  74.         {
  75.             return this.cash;
  76.         }
  77.        
  78.  
  79.         public void Add(BankAccount account)
  80.         {
  81.             throw new NotImplementedException();
  82.         }
  83.  
  84.         public void Remove(BankAccount account)
  85.         {
  86.             throw new NotImplementedException();
  87.         }
  88.  
  89.         public BankAccount Get(BankAccount account)
  90.         {
  91.             throw new NotImplementedException();
  92.         }
  93.     }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment