示例#1
0
 /// <summary>
 /// Конструктор
 /// </summary>
 public AquariumObject(BaseParameters parameters)
 {
     X = parameters.X;
     Y = parameters.Y;
     SizeX = parameters.SizeX;
     SizeY = parameters.SizeY;
 }
        public IAquariumObject Create(AquariumObjectType aquariumObjectType, BaseParameters parameter)
        {
            AquariumTypeDescription typeDescription = GetTypeDescription(aquariumObjectType);

            Type type = typeDescription.ObjectType;
            IAquariumObjectRenderer renderer = _rendererSelector.Get(aquariumObjectType, parameter);
            IAquariumObject aquariumObject = (IAquariumObject)Activator.CreateInstance(type, parameter, renderer);
            IAquariumObject wrappedAquariumObject = _aquariumObjectWrapper.Wrap(aquariumObject);
            return wrappedAquariumObject;
        }
        /// <summary>
        /// Создать рендерер
        /// </summary>
        /// <param name="aquariumObjectType"></param>
        /// <param name="parameters"></param>
        /// <returns></returns>
        private ImageRenderer CreateRenderer(AquariumObjectType aquariumObjectType, BaseParameters parameters)
        {
            ImageRenderer renderer = null;

            Image image = GetImage(aquariumObjectType, parameters);
            if (image != null)
            {
                renderer = new ImageRenderer(image);
            }

            return renderer;
        }
        /// <summary>
        /// Получить рендерер
        /// </summary>
        public virtual IAquariumObjectRenderer Get(AquariumObjectType aquariumObjectType, BaseParameters parameters)
        {
            string key = GetKey(aquariumObjectType, parameters);

            ImageRenderer renderer;
            if (!_renderers.TryGetValue(key, out renderer))
            {
                renderer = CreateRenderer(aquariumObjectType, parameters);
                _renderers.Add(key, renderer);
            }

            return renderer;
        }
        /// <summary>
        /// Получить изображение рыбки
        /// </summary>
        private Image GetFishImage(AquariumObjectType aquariumObjectType, BaseParameters parameters)
        {
            FishParameters fishParameters = parameters as FishParameters;
            if (fishParameters != null)
            {
                Type movementStategyType = fishParameters.MovementStrategy.GetType();
                if (movementStategyType == typeof(SinMovementStrategyAdapter))
                {
                    return Resources.FishRed;
                }
            }

            return Resources.Fish;
        }
        /// <summary>
        /// Получить изображение объекта
        /// </summary>
        private Image GetImage(AquariumObjectType aquariumObjectType, BaseParameters parameters)
        {
            Image image = null;

            switch (aquariumObjectType)
            {
                case AquariumObjectType.Fish:
                    image = GetFishImage(aquariumObjectType, parameters);
                    break;
                case AquariumObjectType.Seaweed:
                    image = Resources.Seaweed;
                    break;
            }

            return image;
        }
        /// <summary>
        /// Получить ключ рендерера
        /// </summary>
        private string GetKey(AquariumObjectType aquariumObjectType, BaseParameters parameters)
        {
            string key = ((int)aquariumObjectType).ToString();

            // Здесь получаются слишком большие строки
            FishParameters fishParameters = parameters as FishParameters;
            if (fishParameters != null && fishParameters.MovementStrategy != null)
            {
                Type movementStrategyType = fishParameters.MovementStrategy.GetType();
                key += movementStrategyType.GUID;
            }

            return key;
        }