private IGarbageDisposalStrategy GetStrategy(DisposableAttribute garbageAttribute) { var attributeType = garbageAttribute.GetType(); var strategy = this.StrategyHolder.GetDisposalStrategies[attributeType]; return(strategy); }
public IProcessingData ProcessWaste(IWaste garbage) { Type type = garbage.GetType(); DisposableAttribute disposalAttribute = (DisposableAttribute)type .GetCustomAttributes(typeof(DisposableAttribute), true).FirstOrDefault(); if (disposalAttribute == null) { throw new ArgumentException( "The passed in garbage does not implement a supported Disposable Strategy Attribute."); } Type typeOfAttribute = disposalAttribute.GetType(); if (!this.StrategyHolder.GetDisposalStrategies.ContainsKey(typeOfAttribute)) { Type typeOfCorrespondingStrategy = disposalAttribute.CorrespondingStrategyType; IGarbageDisposalStrategy activatedDisposalStrategy = (IGarbageDisposalStrategy)Activator.CreateInstance(typeOfCorrespondingStrategy); this.StrategyHolder.AddStrategy(typeOfAttribute, activatedDisposalStrategy); } IGarbageDisposalStrategy currentStrategy = this.StrategyHolder.GetDisposalStrategies[typeOfAttribute]; return(currentStrategy.ProcessGarbage(garbage)); }
public IProcessingData ProcessWaste(IWaste garbage) { Type type = garbage.GetType(); //take garbage type. //Take attribute (if there is one) for current strategy. DisposableAttribute disposalAttribute = (DisposableAttribute)type.GetCustomAttributes(typeof(DisposableAttribute), true).FirstOrDefault(); if (disposalAttribute == null) //If there isn't throw exeption { throw new ArgumentException( "The passed in garbage does not implement a supported Disposable Strategy Attribute."); } Type typeOfAttribute = disposalAttribute.GetType(); //take from disposableAttribute his type. if (!this.StrategyHolder.GetDisposalStrategies.ContainsKey(typeOfAttribute)) //Check if in strategies have key with this current starategy. If there isn't key I make key to have this key in future. { Type typeOfCorespondingStrategy = disposalAttribute.CorespondingStrategyType; //Take type of current strategy to be used. IGarbageDisposalStrategy activatedStrategy = (IGarbageDisposalStrategy)Activator.CreateInstance(typeOfCorespondingStrategy); //Make instance for current strategy. this.StrategyHolder.AddStrategy(typeOfAttribute, activatedStrategy); // Add key to dict with strategies. } IGarbageDisposalStrategy currentStrategy = this.StrategyHolder.GetDisposalStrategies[typeOfAttribute]; //Take maked strategy. return(currentStrategy.ProcessGarbage(garbage)); }
public void RemoveStrategy_ShouldReturnTrue_WhenRecieveValidParameters() { // Arrange var strategy = new BurnableDisposalStrategy(); var type = new DisposableAttribute(); var holder = new StrategyHolder(); // Act and Assert Assert.IsTrue(holder.RemoveStrategy(type)); }
public IProcessingData ProcessWaste(IWaste garbage) { Type type = garbage.GetType(); DisposableAttribute disposalAttribute = (DisposableAttribute)type.GetCustomAttributes(true) .FirstOrDefault(a => a.GetType().IsSubclassOf(typeof(DisposableAttribute))); IGarbageDisposalStrategy currentStrategy = this.StrategyHolder .GetDisposalStrategy(disposalAttribute.GetType()); return(currentStrategy.ProcessGarbage(garbage)); }
public IProcessingData ProcessWaste(IWaste garbage) { Type type = garbage.GetType(); DisposableAttribute disposalAttribute = (DisposableAttribute)type.GetCustomAttributes(true).FirstOrDefault(); IGarbageDisposalStrategy currentStrategy; if (disposalAttribute == null || !this.StrategyHolder.GetDisposalStrategies.TryGetValue(disposalAttribute.GetType(), out currentStrategy)) { return(null); } return(currentStrategy.ProcessGarbage(garbage)); }
public IProcessingData ProcessWaste(IWaste garbage) { Type type = garbage.GetType(); DisposableAttribute disposalAttribute = (DisposableAttribute)type.GetCustomAttributes(false).FirstOrDefault(x => x.GetType().IsSubclassOf(typeof(DisposableAttribute))); IGarbageDisposalStrategy currentStrategy; if (disposalAttribute == null || !this.StrategyHolder.GetDisposalStrategies.TryGetValue(disposalAttribute.GetType(), out currentStrategy)) { throw new ArgumentException( "The passed in garbage does not implement a supported Disposable Strategy Attribute."); } return(currentStrategy.ProcessGarbage(garbage)); }
private void CreateStrategyIfNotExist(DisposableAttribute garbageAttribute) { var attributeType = garbageAttribute.GetType(); var doesContainStrategy = this.StrategyHolder.GetDisposalStrategies.ContainsKey(attributeType); if (!doesContainStrategy) { var strategyType = garbageAttribute.CorrespondingStrategyType; var activatedStrategy = Activator.CreateInstance(strategyType) as IGarbageDisposalStrategy; this.StrategyHolder.AddStrategy(attributeType, activatedStrategy); } }
public IProcessingData ProcessWaste(IWaste garbage) { Type type = garbage.GetType(); DisposableAttribute disposalAttribute = (DisposableAttribute) type.GetCustomAttributes(typeof(DisposableAttribute), true) .FirstOrDefault(x => Attribute.IsDefined(type, typeof(DisposableAttribute))); IGarbageDisposalStrategy currentStrategy; if (disposalAttribute == null || !this.StrategyHolder.GetDisposalStrategies.TryGetValue(disposalAttribute.GetType(), out currentStrategy)) { throw new ArgumentException(ConstantMessages.GarbageDoesNotImplementDisposableAttribute); } return(currentStrategy.ProcessGarbage(garbage)); }
public void ProcessWaste_ShouldReturnValidCurrentStrategy() { // Arrange var garbage = new StorableWaste("glass", 10.5, 1.2); var strategyHolder = new StrategyHolder(); var garbageProcessor = new GarbageProcessor(strategyHolder); Type type = garbage.GetType(); DisposableAttribute disposalAttribute = (DisposableAttribute)type.GetCustomAttributes(true).FirstOrDefault(x => x.GetType() == typeof(DisposableAttribute)); IGarbageDisposalStrategy currentStrategy = new BurnableDisposalStrategy(); var expectedResult = currentStrategy.ProcessGarbage(garbage); // Act var actualResult = garbageProcessor.ProcessWaste(garbage); // Assert Assert.AreEqual(expectedResult, actualResult); }
public IProcessingData ProcessWaste(IWaste garbage) { Type type = garbage.GetType(); DisposableAttribute disposalAttribute = type.GetCustomAttributes(typeof(DisposableAttribute), true).FirstOrDefault() as DisposableAttribute; if (disposalAttribute == null) { throw new ArgumentException("The passed in garbage does not implement a supported Disposable Strategy Attribute."); } IGarbageDisposalStrategy currentStrategy = Activator.CreateInstance(disposalAttribute.GarbageDisposalStrategyType, new object[0]) as IGarbageDisposalStrategy; if (currentStrategy == null) { throw new ArgumentException("The passed in garbage's Strategy Attribute does not contain supported Disposal Strategy."); } return(currentStrategy.ProcessGarbage(garbage)); }
public string ProcessWaste(IWaste garbage) { Type type = garbage.GetType(); DisposableAttribute disposalAttribute = (DisposableAttribute)type.GetCustomAttributes(true).FirstOrDefault(); this.StrategyHolder.GetDisposalStrategies.TryGetValue(disposalAttribute.GetType(), out var currentStrategy); if (this.managementRequirement != null && this.StoppedGarbage(garbage) && this.UnsifficientBalances(garbage)) { return(string.Format(OutpuMessages.ProcessingDenied)); } var data = currentStrategy.ProcessGarbage(garbage); this.ProcessingData.IncreaseCapitalBalance(data.CapitalBalance); this.ProcessingData.IncreaseEnergyBalance(data.EnergyBalance); return(string.Format(OutpuMessages.GarbageProcessed, garbage.Weight, garbage.Name)); }
public void InitializeFields() { this.strategyHolder = new StrategyHolder(); this.attribute = new DisposableAttribute(); }