/** * Creates a standard arrangement for the vending machine. All the * components are created and interconnected. The hardware is initially * empty. The product kind names and costs are initialized to " " * and 1 respectively. * * @param coinKinds * The values (in cents) of each kind of coin. The order of the * kinds is maintained. One coin rack is produced for each kind. * Each kind must have a unique, positive value. * @param selectionButtonCount * The number of selection buttons on the machine. Must be * positive. * @param coinRackCapacity * The maximum capacity of each coin rack in the machine. Must be * positive. * @param productRackCapacity * The maximum capacity of each product rack in the machine. Must * be positive. * @param receptacleCapacity * The maximum capacity of the coin receptacle, storage bin, and * delivery chute. Must be positive. * @throws IllegalArgumentException * If any of the arguments is null, or the size of productCosts * and productNames differ. */ public VendingMachine(Cents[] coinKinds, int selectionButtonCount, int coinRackCapacity, int productRackCapacity, int receptacleCapacity) { this.hardwareFacade = new HardwareFacade(coinKinds, selectionButtonCount, coinRackCapacity, productRackCapacity, receptacleCapacity); CommunicationFacade cf = new CommunicationFacade(this.hardwareFacade); ProductFacade prf = new ProductFacade(this.hardwareFacade); PaymentFacade paf = new PaymentFacade(this.hardwareFacade); BuisnessRule br = new BuisnessRule(cf, prf, paf); }
public BuisnessRule(CommunicationFacade cf, ProductFacade prf, PaymentFacade paf) { this.cf = cf; this.prf = prf; this.paf = paf; this.cf.SelectionMade += new EventHandler(SelectionMade); this.cf.ProductVended += new EventHandler(productVendedHandler); this.cf.OutOfStock += new EventHandler(outOfStockHandler); this.paf.FundsSufficient += new EventHandler(ReturnToDelivery); this.paf.FundsInsufficient += new EventHandler(FundsInsufficiantHandler); }
// This class talks to the facades public BusinessLogic(HardwareFacade hardwareFacade) { // Create Facades and pass the hardware facade to them so that they can subscribe to appropriate events this.payment = new PaymentFacade(hardwareFacade); this.comms = new CommunicationFacade(hardwareFacade); this.prod = new ProductFacade(hardwareFacade); // Detect if a selection was made this.comms.SelectionMade += new EventHandler <SelectionEventArgs>(initiate); // Detect errors from payment and product and pass it to the comms facade this.payment.error += new EventHandler <ErrorEventArgs>(sendError); this.prod.error += new EventHandler <ErrorEventArgs>(sendError); }
private static void CompositionRoot(int userId = 1) { var state = new State() { Screen = 1 }; if (_server == null) { _server = new Server.Server(new MemoryRepository()); } ScenarioContext.Current["state" + userId] = state; var clientServiceProxy = new ClientServiceProxy(_server.Address.OriginalString); ScenarioContext.Current["clientServiceProxy" + userId] = clientServiceProxy; var commandFactory = new CommandFactory(state, clientServiceProxy); ScenarioContext.Current["commandFactory" + userId] = commandFactory; var application = new CommunicationFacade(state, commandFactory); ScenarioContext.Current["application" + userId] = application; }
public void At131580Arrive3059() // .......MIN------MAX...P.. { UInt64 real = CommunicationFacade.ComputeRealNumber(3059, 131580, UInt16.MaxValue, (uint)Sizes.WINDOW_SIZE); Assert.AreEqual((UInt64)68595, real); //it thinks, that it overlaps }
[TestMethod] // V public void At131580Arrive2548() // .......MIN------MAX..... { UInt64 real = CommunicationFacade.ComputeRealNumber(2548, 131580, UInt16.MaxValue, (uint)Sizes.WINDOW_SIZE); Assert.AreEqual((UInt64)133620, real); }
public void At66045Arrive2804() // .......MIN------MAX...P.. { UInt64 real = CommunicationFacade.ComputeRealNumber(2805, 66045, UInt16.MaxValue, (uint)Sizes.WINDOW_SIZE); Assert.AreEqual((UInt64)2805, real); //it thinks, that it overlaps }
[TestMethod] // V public void At66045Arrive2549() // .......MIN------MAX..... { UInt64 real = CommunicationFacade.ComputeRealNumber(2549, 66045, UInt16.MaxValue, (uint)Sizes.WINDOW_SIZE); Assert.AreEqual((UInt64)68085, real); }
public void At131325Arrive508() { UInt64 real = CommunicationFacade.ComputeRealNumber(508, 131325, UInt16.MaxValue, (uint)Sizes.WINDOW_SIZE); Assert.AreEqual((UInt64)131580, real); }
public void At131070Arrive65534() { UInt64 real = CommunicationFacade.ComputeRealNumber(65534, 131070, UInt16.MaxValue, (uint)Sizes.WINDOW_SIZE); Assert.AreEqual((UInt64)131070, real); }
public void At66045Arrive509() { UInt64 real = CommunicationFacade.ComputeRealNumber(509, 66045, UInt16.MaxValue, (uint)Sizes.WINDOW_SIZE); Assert.AreEqual((UInt64)66045, real); }
public void At65280Arrive254() { UInt64 real = CommunicationFacade.ComputeRealNumber(254, 65280, UInt16.MaxValue, (uint)Sizes.WINDOW_SIZE); Assert.AreEqual((UInt64)65790, real); }
public void At98175Arrive33599() { UInt64 real = CommunicationFacade.ComputeRealNumber(33599, 98175, UInt16.MaxValue, (uint)Sizes.WINDOW_SIZE); Assert.AreEqual((UInt64)99135, real); }
static void Main(string[] args) { EmployeeSeparator employeeSeparator = new EmployeeSeparator(); Finance finance = new Finance(employeeSeparator); IT it = new IT(employeeSeparator); employeeSeparator.Separate(); DelegateDemo delegateDemo = new DelegateDemo(); delegateDemo.Print("Am from delegate"); //delegateDemo.DoLongOperation(UpdatePrintMessage); //CustomerPresentation customerPresentation = new CustomerPresentation(); //customerPresentation.Update(); /*AnonymouseMethodDemo anonymouseMethodDemo = new AnonymouseMethodDemo(); * anonymouseMethodDemo.CalculateAreaOfRect(5, 5); * * anonymouseMethodDemo.PrintNumber(delegate (int number) * { * Console.WriteLine("The updated number : " + number); * }, 100); * * anonymouseMethodDemo.saveEventHandler += delegate (object sender, EventArgs e) * { * Console.WriteLine("Update successfull"); * }; * anonymouseMethodDemo.Save(); * anonymouseMethodDemo.CalculateAreaOfRect(5, 5); * anonymouseMethodDemo.CalculateAreaOfRectLambda(5, 6); * * LambdaCollectionEx lambdaCollectionEx = new LambdaCollectionEx(); * lambdaCollectionEx.CheckStudentActivities();*/ // Polymorphism Example using Inheritance BusinessFacade businessFacade = new BusinessFacade(); //Normal implementaiton /*Car car = new Car(); * Ship ship = new Ship(); * businessFacade.StartCar(car); * businessFacade.StopCar(car); * businessFacade.StartShip(ship); * businessFacade.StopShip(ship);*/ //With Base class [Machine] - So initially base class methods got executed //Warning CS0108 'Car.Start()' hides inherited member 'Machine.Start()'. Use the new keyword if hiding was intended /*Machine car = new Car(); * Machine ship = new Ship(); * businessFacade.StartCar(car); * businessFacade.StopCar(car); * businessFacade.StartShip(ship); * businessFacade.StopShip(ship);*/ //Polymorphic way //A base class reference variable can point to derived class object //Derived class is specialization of base class which means the derived class has all the capabilities of base class //Derived class should not hold the base class object Machine car = new Car(); Machine ship = new Ship(); /*businessFacade.StartMachine(car); //Dynamically decide which machine needs to start. In this case it's car * businessFacade.StopMachine(car); * businessFacade.StartMachine(ship); //Dynamically decide which machine needs to start. In this case it's ship * businessFacade.StopMachine(ship); * * Machine airPlane = new Airplane(); * businessFacade.StartMachine(airPlane); //Dynamically decide which machine needs to start. In this case it's airPlane * businessFacade.StopMachine(airPlane); * businessFacade.StartMachine(ship); //Dynamically decide which machine needs to start. In this case it's ship * businessFacade.StopMachine(ship);*/ // Polymorphism Example using Interface // Polymorphism Example before using Interface // Whenever new communication mode introduced [Email mode], communication facede needs to be updated and then the client needs to be updated accordingly // This makes the code fragile and error prone. Also the complete testing needs to be done. // So to achieve this, we have to use Polymorphism using Interface CommunicationFacade communicationFacade = new CommunicationFacade(); /*VoiceMode voiceMode = new VoiceMode(); * TextMode textMode = new TextMode(); * EmailMode emailMode = new EmailMode(); * * CommunicationFacade communicationFacade = new CommunicationFacade(); * Console.WriteLine("Please enter the type of communication mode you want to start [1 - Voice, 2 - Text, 3 - Email]"); * var mode = int.Parse(Console.ReadLine()); * if(mode == 1) * { * Thread.Sleep(1000); * communicationFacade.StartCommunication(voiceMode); * } else if (mode == 1) * { * Thread.Sleep(1000); * communicationFacade.StartCommunication(textMode); * } else if (mode == 1) * { * Thread.Sleep(1000); * communicationFacade.StartCommunication(emailMode); * }*/ // Polymorphism Example after using Interface // Advantage of Polymorphism : It promotes extensibility - By allowing new sub classes and methods to be added to a class hierarchy without having to modify existing programs. // Code became more loosly coupled. When we add a new communication there is no change in facade and client code // Facade doesn't do what it's doing. It just know to call the communicate method depends upon the object /*Console.WriteLine("Please enter the type of communication mode you want to start [1 - Voice, 2 - Text, 3 - Email]"); * var mode = int.Parse(Console.ReadLine()); * ICommunication factory = CommunicationFactory.Create(mode); * communicationFacade.StartCommunication(factory);*/ //Polymorphic behaviour of toString() method /*LambdaCollectionEx lambdaCollectionEx = new LambdaCollectionEx(); * lambdaCollectionEx.CheckStudentActivities(); */ //Core Abstraction Demo /*FrothyCreamingCoffee frothyCreamingCoffee = new FrothyCreamingCoffee(2,2,2); * PrepareCoffee(frothyCreamingCoffee); * * IceCoffee iceCoffee = new IceCoffee(3, 3, 3, 3); * PrepareCoffee(iceCoffee);*/ //Adding the new type of coffee is Rigid and not maintainable if we follow the above approach. //So create the abstract class to achieve this in Polymorphic way /*IceCoffee iceCoffee = new IceCoffee(3, 3, 3, 3); * PrepareCoffee(iceCoffee); * FrothyCreamingCoffee frothyCreamingCoffee = new FrothyCreamingCoffee(2, 2, 2); * PrepareCoffee(frothyCreamingCoffee); * Expresso expresso = new Expresso(); * PrepareCoffee(expresso);*/ //Interface Implementation // Tightly Coupled Way /*CreditCardPayment creditCardPayment = new CreditCardPayment(); * DebitCardPayment debitCardPayment = new DebitCardPayment(); * GooglePay googlePay = new GooglePay(); * PaymentManager paymentManager = new PaymentManager(debitCardPayment, creditCardPayment, googlePay); * paymentManager.ManagePayment();*/ // Loosely Coupled Way /*IPaymentMode paymentMode = PaymentModeFactory.Create(PaymentMode.DebitCard); * PaymentManagerLC paymentManagerLC = new PaymentManagerLC(paymentMode); * paymentManagerLC.ManagePayment();*/ //Interface - Unit Testing // A good project may well have more test code than production code // 1. Bugs are found easily and early // 2. Saves Time and Money // 3. A safety net for refactoring or any any enhancements // Concrete class implementation /*var userName = "******"; * WithdrawalService withdrawalService = new WithdrawalService(); * var isEligible = withdrawalService.IsEligibleToWithDrawal(userName, "pwd", 123, 1000); * if(isEligible) * Console.WriteLine($"{userName} is eligible to withdraw"); * else * Console.WriteLine($"{userName} is not eligible to withdraw");*/ // Interface implementation /*var userName = "******"; * IAuthenticationService authenticationService = new AuthenticationService(); * IBalanceCheckService balanceCheckService = new BalanceCheckerService(); * WithdrawalService withdrawalService = new WithdrawalService(authenticationService, balanceCheckService); * var isEligible = withdrawalService.IsEligibleToWithDrawal(userName, "pwd", 123, 1000); * if (isEligible) * Console.WriteLine($"{userName} is eligible to withdraw"); * else * Console.WriteLine($"{userName} is not eligible to withdraw"); */ // Interface - Dependency Injection // We should avoid to creating concrete class instances using new keyword instead you should depend on abstraction perhaps using an interface where by letting the dependencies // to be created by someone else for you in an abstract manner and use them by injecting whenever required. // DI - Creates objects outside of a class and provides those objects to a class through injection mainly constrctor // Interfaces achieves - Maintainability, Testability, Flexibility, Extensibility // IOC Container - Framework for implementing automatic dependency injection // Dependencies chain can become nested and complex. IOC container centralizes the creation of your dependencies and manages its lifetime. // Interface + Dependency Injection + IOC Container = Almost Zero "New" Keyword //AuthenticationService : _dataAccessService = new DataAccessService(); //BalanceCheckerService : _dataAccessService = new DataAccessService(); //Program.cs[Client] : IAuthenticationService authenticationService = new AuthenticationService(); IBalanceCheckService balanceCheckService = new BalanceCheckerService(); /*var userName = "******"; * WithdrawalService withdrawalService = AppunityContainer.RegisterService(); * var isEligible = withdrawalService.IsEligibleToWithDrawal(userName, "pwd", 123, 1000); * if (isEligible) * Console.WriteLine($"{userName} is eligible to withdraw"); * else * Console.WriteLine($"{userName} is not eligible to withdraw");*/ // Enum implementation using SmartEnum NuGet package CustomerEnumDemo customerEnumDemo = new CustomerEnumDemo(); customerEnumDemo.Id = 123; customerEnumDemo.Type = new AbstractCustomerType.RegularType(); Console.WriteLine($"Discount for {customerEnumDemo.Type.Name} type is : {customerEnumDemo.CalculateDiscount(123)}"); Console.ReadKey(); }