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(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 bool AddStrategy(Type disposableAttribute, IGarbageDisposalStrategy strategy) { if (disposableAttribute == typeof(BurnableGarbage) || disposableAttribute == typeof(RecyclableGarbage) || disposableAttribute == typeof(StorableGarbage)) { this.strategies.Add(disposableAttribute, strategy); return(true); } return(false); }
public bool AddStrategy(Type disposableAttribute, IGarbageDisposalStrategy strategy) { if (!this.strategies.ContainsKey(disposableAttribute)) { this.strategies.Add(disposableAttribute, strategy); return(true); } return(false); }
public bool AddStrategy(Type disposableAttribute, IGarbageDisposalStrategy strategy) { if (disposableAttribute == null || strategy == null) { throw new ArgumentException(); } this.strategies.Add(disposableAttribute, strategy); return(true); }
public bool AddStrategy(Type disposableAttribute, IGarbageDisposalStrategy strategy) { if (!this.strategies.ContainsKey(disposableAttribute)) { this.strategies.Add(disposableAttribute, strategy); return true; } return false; }
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 bool AddStrategy(Type disposableAttribute, IGarbageDisposalStrategy strategy) { if (disposableAttribute == null || strategy == null) { throw new ArgumentNullException(); } if (this.GetDisposalStrategies.ContainsKey(disposableAttribute)) { return(false); } this.strategies.Add(disposableAttribute, strategy); return(true); }
public bool AddStrategy(Type disposableAttribute, IGarbageDisposalStrategy strategy) { if (!disposableAttribute.IsSubclassOf(typeof(DisposableAttribute)) || disposableAttribute.IsAbstract) { throw new ArgumentException("The passed in type is not a subclass of Disposable Attribute!"); } if (this.strategies.ContainsKey(disposableAttribute)) { return(false); } this.strategies.Add(disposableAttribute, strategy); return(true); }
public bool AddStrategy(Type disposableAttribute, IGarbageDisposalStrategy strategy) { if (disposableAttribute != typeof(DisposableAttribute) && disposableAttribute.BaseType != typeof(DisposableAttribute)) { throw new ArgumentException(ConstantMessages.GarbageDoesNotImplementDisposableAttribute); } if (!this.strategies.ContainsKey(disposableAttribute)) { this.strategies.Add(disposableAttribute, strategy); return(true); } return(false); }
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)); }
// Refactored public bool AddStrategy(Type disposableAttribute, IGarbageDisposalStrategy strategy) { //this.strategies.Add(disposableAttribute, strategy); //return true; // Refactored // Validate Attribute Type if (!disposableAttribute.IsSubclassOf(typeof(DisposableAttribute)) || disposableAttribute.IsAbstract) { return(false); } // Add to collection if (!this.strategies.ContainsKey(disposableAttribute)) { this.strategies.Add(disposableAttribute, strategy); return(true); } return(false); }
private void LoadStrategies() { var garbageTypes = Assembly.GetExecutingAssembly() .GetTypes() .Where(t => t.GetCustomAttributes().Any(a => a.GetType() == typeof(GarbageAttribute))); var garbagesTypeStrategy = Assembly.GetExecutingAssembly() .GetTypes() .Where(t => t.GetCustomAttributes().Any(a => a.GetType() == typeof(GarbageDisposableStrategyAttribute))); if (garbageTypes != null && garbagesTypeStrategy != null) { foreach (var garbageType in garbageTypes) { var garbageDisposalAttribute = (DisposableAttribute)garbageType .GetCustomAttributes(true) .FirstOrDefault(a => a.GetType().IsSubclassOf(typeof(DisposableAttribute))); foreach (var garbageTypeStrategy in garbagesTypeStrategy) { var garbageStrategyDisposalAttribute = (DisposableAttribute)garbageTypeStrategy .GetCustomAttributes(true) .FirstOrDefault(a => a.GetType().IsSubclassOf(typeof(DisposableAttribute))); if (garbageDisposalAttribute.GetType() == garbageStrategyDisposalAttribute.GetType()) { IGarbageDisposalStrategy holder = (IGarbageDisposalStrategy)Activator. CreateInstance(garbageTypeStrategy); this.strategyHolder.AddStrategy( garbageStrategyDisposalAttribute.GetType(), holder); break; } } } } }
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); }
protected virtual void AddStrategyAttributePair(Type attributeType, IGarbageDisposalStrategy garbageDisposalStrategy) { this.strategyHolder.AddStrategy(attributeType, garbageDisposalStrategy); }
public void InitialTest() { this.strategy = new RecyclableDisposalStrategy(); }
public void TestInit() { ds = new BurnableGarbageDisposalStrategy(); strategies = new Dictionary <Type, IGarbageDisposalStrategy>(); }
public bool AddStrategy(Type disposableAttribute, IGarbageDisposalStrategy strategy) { this.strategies.Add(disposableAttribute, strategy); return(true); }