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));
        }
Exemple #2
0
        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));
        }
Exemple #3
0
        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(true)
                                                    .FirstOrDefault(a => a.GetType().IsSubclassOf(typeof(DisposableAttribute)));

            IGarbageDisposalStrategy currentStrategy = this.StrategyHolder
                                                       .GetDisposalStrategy(disposalAttribute.GetType());

            return(currentStrategy.ProcessGarbage(garbage));
        }
Exemple #5
0
        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(true)
                                                    .FirstOrDefault(x => x.GetType() == 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));
        }
Exemple #7
0
        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));
        }
Exemple #8
0
        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));
        }
Exemple #9
0
        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));
        }