static void ObserverTester() { #region sample 1 // Configure Observer pattern var s = new ConcreteSubject(); s.Attach(new ConcreteObserver(s, "X")); s.Attach(new ConcreteObserver(s, "Y")); s.Attach(new ConcreteObserver(s, "Z")); // Change subject and notify observers s.SubjectState = "ABC"; s.Notify(); #endregion #region sample 2 // Create IBM stock and attach investors var ibm = new IBM("IBM", 120.00); ibm.Attach(new Investor("Sorros")); ibm.Attach(new Investor("Berkshire")); // Fluctuating prices will notify investors ibm.Price = 120.10; ibm.Price = 121.00; ibm.Price = 120.50; ibm.Price = 120.75; #endregion }
public void AttachObserversTest() { ibm.Attach(sorros); ibm.Attach(berkshire); Assert.AreEqual(ibm.investors[0], sorros); Assert.AreEqual(ibm.investors[1], berkshire); }
public static void Main() { var ibm = new IBM("IBM", 120.00); ibm.Attach(new Investor("Sorros")); ibm.Attach(new Investor("New investor")); ibm.Price = 121.90; ibm.Price = 200.00; }
public void When_Attach_investors_Shoud_Notify_AllOfthem_Change_Price() { foreach (var investor in _investors) { _ibm.Attach(investor.Object); } Assert.AreEqual(0, _notifiedInvestors); _ibm.Price = 120.10; Assert.AreEqual(3, _notifiedInvestors); }
static void Main() { IBM ibm = new IBM("IBM", 120.00); ibm.Attach(new Investor("Sorros")); ibm.Attach(new Investor("Berkshire")); ibm.Price = 120.10; ibm.Price = 121.00; ibm.Price = 120.50; ibm.Price = 120.75; }
public ObserverPattern() { IBM ibm = new IBM("IBM", 120.00); ibm.Attach(new Observer("Daddy yanky")); ibm.Attach(new Observer("Sean Paul")); ibm.Attach(new Observer("Nikhilesh shinde")); ibm.Price = 120.11; ibm.Price = 120.01; ibm.Price = 120.61; }
public static void Execucao() { var ibm = new IBM("IBM", 120.00); ibm.Attach(new Investor("João")); ibm.Attach(new Investor("Maria")); ibm.Price = 120.10; ibm.Price = 121.00; ibm.Price = 120.50; ibm.Price = 120.75; }
static void RunObserverRealWorld() { IBM ibm = new IBM("IBM", 120.00); ibm.Attach(new Investor("Sorros")); ibm.Attach(new Investor("Berkshire")); // Fluctuating prices will notify investors ibm.Price = 120.10; ibm.Price = 121.00; ibm.Price = 120.50; ibm.Price = 120.75; }
internal static void Main() { // Create IBM stock and attach investors var ibm = new IBM("IBM", 120.00); ibm.Attach(new Investor("Sorros")); ibm.Attach(new Investor("Berkshire")); // Fluctuating prices will notify investors ibm.Price = 120.10; ibm.Price = 121.00; ibm.Price = 120.50; ibm.Price = 120.75; }
public static void Test() { Investor s = new Investor("张三"); Investor b = new Investor("李四"); IBM ibm = new IBM("IBM", 120.00); ibm.Attach(s); ibm.Attach(b); ibm.Price = 120.10; ibm.Price = 121.00; ibm.Price = 120.50; ibm.Price = 120.75; }
static void Main(string[] args) { IBM ibm = new IBM("IBM", 120.00); ibm.Attach(new Investor("Sorros")); ibm.Attach(new Investor("Berkshire")); // Fluctuating prices will notify investors ibm.Price = 120.10; ibm.Price = 121.00; ibm.Price = 120.50; ibm.Price = 120.75; }
public static void Main() { Stock ibm = new IBM(nameof(IBM), 120.2); var firstInvestor = new Investor(Constants.DisplayInvestor + 1); ibm.Attach(firstInvestor); ibm.Attach(new Investor(Constants.DisplayInvestor + 2)); ibm.Price = 120.4; ibm.Detach(firstInvestor); ibm.Price = 125.58; ibm.Attach(new Investor(Constants.DisplayInvestor + 3)); ibm.Price = 120.99; }
static void Main(string[] args) { Stock ibm = new IBM("IBM", 120.2); var firstInvestor = new Investor("Investor #1"); ibm.Attach(firstInvestor); ibm.Attach(new Investor("Investor #2")); ibm.Price = 120.4; ibm.Detach(firstInvestor); ibm.Price = 125.58; ibm.Attach(new Investor("Investor #3")); ibm.Price = 120.99; }
public static void ObserverRealWorld() { // Create IBM stock and attach investors IBM ibm = new IBM("IBM", 120.00); ibm.Attach(new Investor("Sorros")); ibm.Attach(new Investor("Berkshire")); // Fluctuating prices will notify investors ibm.Price = 120.10; ibm.Price = 121.00; ibm.Price = 120.50; ibm.Price = 120.75; }
public void TestObserverPrice() { IBM ibm = new IBM("IBM", 120.24); var i1 = new Investor("I1"); var i2 = new Investor("I2"); ibm.Attach(i1); ibm.Attach(i2); ibm.Price = 113.24; Assert.AreEqual(i1.Stock.Price, 113.24); Assert.AreEqual(i2.Stock.Price, 113.24); }
public void Main() { // Create IBM stock and attach investors IBM ibm = new IBM(120.00); ibm.Attach(new Investor("Sorros")); ibm.Attach(new Investor("Berkshire")); // Fluctuating prices will notify investors ibm.Price = 120.10; ibm.Price = 121.00; ibm.Price = 120.50; ibm.Price = 120.75; }
static void Main(string[] args) { { IBM ibm = new IBM("IBM", 120.00); ibm.Attach(new Investor("Sorros")); ibm.Attach(new Investor("Berkshire")); ibm.Price = 120.10; ibm.Price = 121.00; ibm.Price = 120.50; ibm.Price = 120.75; Console.ReadKey(); } }
// Entry point into console application. static void Main() { // Create IBM stock and attach investors IBM ibm = new IBM("IBM", 120.00); ibm.Attach(new Investor("Sorros")); ibm.Attach(new Investor("Berkshire")); // Fluctuating prices will notify investors ibm.Price = 120.10; ibm.Price = 121.00; ibm.Price = 120.50; ibm.Price = 120.75; // Wait for user Console.ReadKey(); }
/// <summary> /// Entry point into console application. /// </summary> static void Main() { // Create IBM stock and attach investors IBM ibm = new IBM("IBM", 120.00); ibm.Attach(new Investor("Sorros")); ibm.Attach(new Investor("Berkshire")); // Fluctuating prices will notify investors ibm.Price = 120.10; ibm.Price = 121.00; ibm.Price = 120.50; ibm.Price = 120.75; // Wait for user Console.ReadKey(); }
public static void Run() { var ibm = new IBM("Microsoft", 100); var investor = new Investor("Igor"); ibm.Attach(investor); ibm.Price = 300; ibm.Price = 200; }
public void Run() { // Skapa ibm-aktien IBM ibm = new IBM("IBM", 120.00); // En extra investor var myInvestor = new Investor("happybits"); // Vi lägger till investerare ibm.Attach(new Investor("Sorros")); ibm.Attach(new Investor("Berkshire")); ibm.Attach(myInvestor); // När priset ändras så blir investorerna meddelade ibm.Price = 120.10; ibm.Price = 121.00; // Nu tar vi bort en investor och fortsätter ändra priset ibm.Detach(myInvestor); ibm.Price = 120.50; ibm.Price = 120.75; }
private static void Main(string[] args) { Console.ForegroundColor = ConsoleColor.Cyan; Console.WriteLine("Gang of Four Design Pattern - Observer"); Console.ForegroundColor = ConsoleColor.White; // Create IBM stock and attach investors var ibm = new IBM("International Business Machines", "IBM", 120.00m); ibm.Attach(new StockInvestor { Name = "Tim" }); ibm.Attach(new StockInvestor { Name = "John" }); // Fluctuating prices will notify investors ibm.Price = 120.10m; ibm.Price = 121.00m; ibm.Price = 120.50m; ibm.Price = 120.75m; }
public static void TestInvestors() { var ibm = new IBM("IBM", 120.00); ibm.Attach(new Investor("Sorros")); ibm.Attach(new Investor("Berkshire")); using (var consoleOutput = new TestHelper.ConsoleOutput()) { ibm.Price = 120.10; ibm.Price = 121.00; ibm.Price = 120.50; ibm.Price = 120.75; var consoleLogLines = consoleOutput.GetOuput().Split(Environment.NewLine.ToCharArray()).ToList(); consoleLogLines.RemoveAll(String.IsNullOrEmpty); foreach (var line in consoleLogLines) { Assert.True(line.Contains("IBM") && (line.Contains("Sorros") || line.Contains("Berkshire")) && (line.Contains("120.10") || line.Contains("121.00") || line.Contains("120.50") || line.Contains("120.75"))); } consoleOutput.Dispose(); } ibm.Attach(new Investor("Another")); using (var consoleOutput = new TestHelper.ConsoleOutput()) { ibm.Price = 99.99; var consoleLogLines = consoleOutput.GetOuput().Split(Environment.NewLine.ToCharArray()).ToList(); consoleLogLines.RemoveAll(String.IsNullOrEmpty); var lastLine = consoleLogLines.LastOrDefault(); if (lastLine == null) { Assert.Fail(); } Assert.True(lastLine.Contains("IBM") && lastLine.Contains("99.99") && lastLine.Contains("Another")); } }
public static void Main( string[] args ) { // Create investors Investor s = new Investor( "Sorros" ); Investor b = new Investor( "Berkshire" ); // Create IBM stock and attach investors IBM ibm = new IBM( "IBM", 120.00 ); ibm.Attach( s ); ibm.Attach( b ); // Change price, which notifies investors ibm.Price = 120.10; ibm.Price = 121.00; ibm.Price = 120.50; ibm.Price = 120.75; }
static void Main(string[] args) { Console.WriteLine("******Singleton******\n"); LoadBalancer L1 = LoadBalancer.GetLoadBalancer(); LoadBalancer L2 = LoadBalancer.GetLoadBalancer(); LoadBalancer L3 = LoadBalancer.GetLoadBalancer(); LoadBalancer L4 = LoadBalancer.GetLoadBalancer(); if (L1 == L2 && L2 == L3 && L3 == L4) { Console.WriteLine("Same Instance"); } LoadBalancer load = LoadBalancer.GetLoadBalancer(); for (int i = 0; i < 15; i++) { Console.WriteLine(load.Server); } Console.WriteLine("\n******AbstractFactory******\n"); ContinentFactory africa = new AfricaFactory(); AnimalWorld earth = new AnimalWorld(africa); earth.RunFoodChain(); ContinentFactory america = new AmericaFactory(); earth = new AnimalWorld(america); earth.RunFoodChain(); Console.WriteLine("\n******Factory******\n"); Document[] document = new Document[2]; document[0] = new Report(); document[1] = new Resume(); foreach (Document doc in document) { Console.WriteLine(doc.GetType().Name + " contains:"); foreach (Page page in doc.Pages) { Console.WriteLine("\t" + page.GetType().Name); } } Console.WriteLine("\n******Facade******\n"); Mortgage mort = new Mortgage(); Customer customer = new Customer("Chad"); int amount = 100000; bool elligable = mort.IsElligable(customer, amount); Console.WriteLine(customer.Name + " is " + (elligable ? "elligable" : "not elligable")); Console.WriteLine("\n******Decorator******\n"); Book book = new Book("George RR Martin", "A song of ice and fire", 22); book.Display(); Video video = new Video("Jaws", "Spielberg", 92, 18); video.Display(); Borrowable borrowableVideo = new Borrowable(video); borrowableVideo.BorrowItem("Sky"); borrowableVideo.BorrowItem("Inga"); borrowableVideo.Display(); Console.WriteLine("\n******Prototype******\n"); ColorManager colorManager = new ColorManager(); colorManager["red"] = new Color(255, 0, 0); colorManager["green"] = new Color(0, 255, 0); colorManager["blue"] = new Color(0, 0, 255); colorManager["angry"] = new Color(255, 54, 0); colorManager["peace"] = new Color(128, 211, 128); colorManager["flame"] = new Color(211, 34, 20); Color color1 = colorManager["red"].Clone() as Color; Color color2 = colorManager["peace"].Clone() as Color; Color color3 = colorManager["flame"].Clone() as Color; Console.WriteLine("\n******Builder******\n"); VehicleBuilder builder; Shop shop = new Shop(); builder = new ScooterBuilder(); shop.Construct(builder); builder.Vehicle.Show(); builder = new CarBuilder(); shop.Construct(builder); builder.Vehicle.Show(); builder = new MotorcycleBuilder(); shop.Construct(builder); builder.Vehicle.Show(); Console.WriteLine("\n******Adapter******\n"); Compound unknown = new RichCompound("Unknown"); unknown.Display(); Compound water = new RichCompound("Water"); water.Display(); Compound benzene = new RichCompound("Benzene"); benzene.Display(); Compound ethanol = new RichCompound("Ethanol"); ethanol.Display(); Console.WriteLine("\n******Bridge******\n"); Customers customers = new Customers("Chicago"); customers.Data = new CustomersData(); customers.Show(); customers.Next(); customers.Show(); customers.Next(); customers.Show(); customers.Add("Henry Velasquez"); customers.ShowAll(); Console.WriteLine("\n******Composite******\n"); CompositeElement root = new CompositeElement("Picture"); root.Add(new PrimitiveElement("Red Line")); root.Add(new PrimitiveElement("Blue Circle")); root.Add(new PrimitiveElement("Green Box")); CompositeElement comp = new CompositeElement("Two Circles"); comp.Add(new PrimitiveElement("Black Circle")); comp.Add(new PrimitiveElement("White Circle")); root.Add(comp); PrimitiveElement pe = new PrimitiveElement("Yellow Line"); root.Add(pe); root.Remove(pe); root.Display(1); Console.WriteLine("\n******Observer******\n"); IBM ibm = new IBM("IBM", 120.00); ibm.Attach(new Investor("Sorros")); ibm.Attach(new Investor("Berkshire")); ibm.Price = 120.10; ibm.Price = 121.00; ibm.Price = 120.50; ibm.Price = 120.75; Console.WriteLine("\n******Strategy******\n"); SortedList studentRecords = new SortedList(); studentRecords.Add("Samuel"); studentRecords.Add("Jimmy"); studentRecords.Add("Sandra"); studentRecords.Add("Vivek"); studentRecords.Add("Anna"); studentRecords.SetSortStrategy(new QuickSort()); studentRecords.Sort(); studentRecords.SetSortStrategy(new ShellSort()); studentRecords.Sort(); studentRecords.SetSortStrategy(new MergeSort()); studentRecords.Sort(); /* * Console.WriteLine("\n******Template******\n"); * DataAccessObject daoCategories = new Categories(); * daoCategories.Run(); * * DataAccessObject daoProducts = new Products(); * daoProducts.Run(); */ Console.WriteLine("\n***Chain of Responsibility***\n"); Approver larry = new Director(); Approver sam = new VicePresident(); Approver tammy = new President(); larry.SetSuccessor(sam); sam.SetSuccessor(tammy); Purchase p = new Purchase(2034, 350.00, "Assets"); larry.ProcessRequest(p); p = new Purchase(2035, 32590.10, "Project X"); larry.ProcessRequest(p); p = new Purchase(2036, 122100.00, "Project Y"); larry.ProcessRequest(p); Console.WriteLine("\n******Command******\n"); User user = new User(); user.Compute('+', 100); user.Compute('-', 50); user.Compute('*', 10); user.Compute('/', 2); user.Undo(4); user.Redo(3); Console.ReadKey(); }