So you needed to draw a grid … or a ruler … or another x-y-ish vector graphics and you ended up with code snippets like this everywhere:
- public void Grid(Graphics g, Size size, int step) {
- for (int i = 0; i < size.Width; i+=step)
- {
- g.DrawLine(Pens.Black,
- new Point(i, 0),
- new Point(i, size.Height)
- );
- }
- for (int i = 0; i < _size.Height; i += step)
- {
- g.DrawLine(Pens.Black,
- new Point(0, i),
- new Point(size.Width, i)
- );
- }
- }
public void Grid(Graphics g, Size size, int step) { for (int i = 0; i < size.Width; i+=step) { g.DrawLine(Pens.Black, new Point(i, 0), new Point(i, size.Height) ); } for (int i = 0; i < _size.Height; i += step) { g.DrawLine(Pens.Black, new Point(0, i), new Point(size.Width, i) ); } }You can avoid redundancy in such situations by using the strategy pattern. First create a new interface called IDirection.
- public interface IDirection
- {
- int GetMoving(Size size);
- int GetStatic(Size size);
- void AddMoving(ref int x, ref int y, int step);
- Point GetPoint(int moving, int static_);
- }
public interface IDirection { int GetMoving(Size size); int GetStatic(Size size); void AddMoving(ref int x, ref int y, int step); Point GetPoint(int moving, int static_); }Now imagine yourself free- falling into a deep hole. Remember, you are a programmer. So stop screaming and start thinking about the fall. Your moving direction is vertical. And since you are in a vertical free- fall you almost don’t move in x direction. Therefore your static direction is horizontal. Now write this in code.
- public class VerticalDirection : IDirection
- {
- public int GetMoving(Size size)
- {
- return size.Height;
- }
- public void AddMoving(ref int x, ref int y, int step)
- {
- y += step;
- }
- public Point GetPoint(int moving, int fixed_)
- {
- return new Point(fixed_, moving);
- }
- public int GetStatic(Size size)
- {
- return size.Width;
- }
- }
public class VerticalDirection : IDirection { public int GetMoving(Size size) { return size.Height; } public void AddMoving(ref int x, ref int y, int step) { y += step; } public Point GetPoint(int moving, int fixed_) { return new Point(fixed_, moving); } public int GetStatic(Size size) { return size.Width; } }And a HorizontalDirection class.
- class HorizontalDirection : IDirection
- {
- public int GetMoving(System.Drawing.Size size)
- {
- return size.Width;
- }
- public void AddMoving(ref int x, ref int y, int step)
- {
- x += step;
- }
- public Point GetPoint(int moving, int fixed_)
- {
- return new Point(moving, fixed_);
- }
- public int GetStatic(Size size)
- {
- return size.Height;
- }
- }
class HorizontalDirection : IDirection { public int GetMoving(System.Drawing.Size size) { return size.Width; } public void AddMoving(ref int x, ref int y, int step) { x += step; } public Point GetPoint(int moving, int fixed_) { return new Point(moving, fixed_); } public int GetStatic(Size size) { return size.Height; } }There we go. Static and moving direction for each concrete direction class are encapsulated and we can now write new grid drawing code.
- public void Grid(Graphics g, Size size, int step)
- {
- Grid(g, size, step, new VerticalDirection());
- Grid(g, size, step, new HorizontalDirection());
- }
- public void Grid(Graphics g, Size size, int step, IDirection direction)
- {
- for (int i = 0; i < direction.GetMoving(size); i += step)
- {
- g.DrawLine(Pens.Black,
- direction.GetPoint(i, 0),
- direction.GetPoint(i, direction.GetStatic(size))
- );
- }
- }
public void Grid(Graphics g, Size size, int step) { Grid(g, size, step, new VerticalDirection()); Grid(g, size, step, new HorizontalDirection()); } public void Grid(Graphics g, Size size, int step, IDirection direction) { for (int i = 0; i < direction.GetMoving(size); i += step) { g.DrawLine(Pens.Black, direction.GetPoint(i, 0), direction.GetPoint(i, direction.GetStatic(size)) ); } }
Subscribe to:
Post Comments
(
Atom
)
0 comment(s) :
Post a Comment