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 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 = 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 IProcessingData ProcessWaste(IWaste garbage) { Type garbageType = garbage.GetType(); var disposalAttribute = garbageType .GetCustomAttributes(true) .FirstOrDefault(x => x.GetType().Name.Contains("DisposeAttribute")); IGarbageDisposalStrategy currentStrategy = null; var disposalStrategyExists = disposalAttribute != null && this.StrategyHolder .GetDisposalStrategies .TryGetValue(disposalAttribute.GetType(), out currentStrategy); if (disposalAttribute == null || !disposalStrategyExists) { throw new ArgumentException( "The passed in garbage does not implement a supported Disposable Strategy Attribute."); } if (this.Restriction != null) { bool notEnoughtResourses = (this.currentData.EnergyBalance < this.Restriction.EnergyLowBorder || this.currentData.CapitalBalance < this.Restriction.CapitalLowBorder) && garbageType.Name.Contains(this.Restriction.RestrictionType); if (notEnoughtResourses) { throw new InvalidOperationException("Processing Denied!"); } } var resultData = currentStrategy.ProcessGarbage(garbage); this.UpdateCurrentData(resultData); return(resultData); }