Jump to content

  • Log In with Google      Sign In   
  • Create Account


#ActualAlpha_ProgDes

Posted 10 September 2013 - 06:19 PM

So based on the code in this thread, I've implemented a very simple Strategy pattern. Well I think I did. Basically, is it good, bad, completely off the mark?

Strategy code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace DesignPatterns
{
    class Strategy
    {
    }

    public interface IAdd
    {
        int moveXUnits(int XUnits);
    }

    public abstract class Movement : IAdd
    {
        private int startUnit;
        private int stepUnits;
        private int totalSteps;

        public Movement()
        {
            startUnit = 0;
            stepUnits = 1;
            totalSteps = 0;
        }

        public Movement(int initializeUnit, int initializeStep)
        {
            startUnit = initializeUnit;
            stepUnits = initializeStep;
            totalSteps = startUnit;
        }

        public int moveXUnits(int XUnits)
        {
            stepUnits = XUnits;
            totalSteps += stepUnits;
            return totalSteps;
        }

        private int autoMoveXUnits()
        {
            totalSteps += stepUnits;
            return totalSteps;
        }

        public virtual void Step(int XUnits)
        {
            moveXUnits(XUnits);
        }

        public virtual void Step()
        {
            autoMoveXUnits();
        }

        public virtual void displaySteps()
        {
            Console.WriteLine("I have moved " + totalSteps.ToString() + " units.");
        }
    }

    public class Walk : Movement
    {
        public Walk() : base(1,2){}
    }

    public class Skip : Movement
    {
        public Skip() : base(2,3){}
    }

    public class Run : Movement
    {
        public Run() : base(3,5){}
    }

    public class Character
    {
        private Movement movememtAction;

        public Character()
        {
            movememtAction = new Walk();
        }

        public Character(Movement initMovement)
        {
            movememtAction = initMovement;
        }

        public void setMovement(Movement movementToPerform)
        {
            movememtAction = movementToPerform;
        }

        public void Step()
        {
            movememtAction.Step();
        }

        public void Step(int newSteps)
        {
            movememtAction.Step(newSteps);
        }

        public void displaySteps()
        {
            movememtAction.displaySteps();
        }
    }
}

Main.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DesignPatterns
{
    class Program
    {
        static void Main(string[] args)
        {
            Character Mario = new Character(new Run());
            Character Link = new Character(new Walk());
            Character Zelda = new Character(new Skip());

            Mario.Step();
            Mario.displaySteps();
        }
    }
}

#1Alpha_ProgDes

Posted 10 September 2013 - 06:18 PM

So based on the code in this thread, I've implemented a very simple Strategy pattern. Well I think I did. Basically, is it good, bad, completely off the mark?

Strategy code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace DesignPatterns
{
    class Strategy
    {
    }

    public interface IAdd
    {
        int moveXUnits(int XUnits);
    }

    public abstract class Movement : IAdd
    {
        private int startUnit;
        private int stepUnits;
        private int totalSteps;

        public Movement()
        {
            startUnit = 0;
            stepUnits = 1;
            totalSteps = 0;
        }

        public Movement(int initializeUnit, int initializeStep)
        {
            startUnit = initializeUnit;
            stepUnits = initializeStep;
            totalSteps = startUnit;
        }

        public int moveXUnits(int XUnits)
        {
            stepUnits = XUnits;
            totalSteps += stepUnits;
            return totalSteps;
        }

        private int autoMoveXUnits()
        {
            totalSteps += stepUnits;
            return totalSteps;
        }

        public virtual void Step(int XUnits)
        {
            moveXUnits(XUnits);
        }

        public virtual void Step()
        {
            autoMoveXUnits();
        }

        public virtual void displaySteps()
        {
            Console.WriteLine("I have moved " + totalSteps.ToString() + " units.");
        }
    }

    public class Walk : Movement
    {
        public Walk() : base(1,2){}
    }

    public class Skip : Movement
    {
        public Skip() : base(2,3){}
    }

    public class Run : Movement
    {
        public Run() : base(3,5){}
    }

    public class Character
    {
        private Movement movememtAction;

        public Character()
        {
            movememtAction = new Walk();
        }

        public Character(Movement initMovement)
        {
            movememtAction = initMovement;
        }

        public void setMovement(Movement movementToPerform)
        {
            movememtAction = movementToPerform;
        }

        public void Step()
        {
            movememtAction.Step();
        }

        public void Step(int newSteps)
        {
            movememtAction.Step(newSteps);
        }

        public void displaySteps()
        {
            movememtAction.displaySteps();
        }
    }
}

Main.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DesignPatterns
{
    class Program
    {
        static void Main(string[] args)
        {
            Character Mario = new Character(new Run());
            Character Link = new Character(new Walk());
            Character Zelda = new Character(new Skip());

            Mario.Step();
            Mario.displaySteps();
        }
    }
}

 


PARTNERS