统一检查具有相似项目的不同列表
Uniform checks for different lists with similar items
我的代码炸了,也许你能帮帮我。我的 class Painter
有一些对象的列表:
public class Painter
{
private readonly MyCar[] cars = new MyCar[5];
private readonly MyHouse[] houses = new MyHouse[6];
private readonly MyPaper[] papers = new MyPaper[7];
}
public class MyCar
{
public void Paint(System.Drawing.Color color) { /* ... */ }
}
public class MyHouse
{
public void Paint(System.Drawing.Color color) { /* ... */ }
}
public class MyPaper
{
public void Paint(System.Drawing.Color color) { /* ... */ }
}
我添加了一种允许绘制其中一个对象的方法:
public class Painter
{
private readonly MyCar[] cars = new MyCar[5];
private readonly MyHouse[] houses = new MyHouse[5];
private readonly MyPaper[] papers = new MyPaper[5];
public void Paint(ObjectType type, int index, System.Drawing.Color color)
{
switch (type)
{
case ObjectType.Car: this.cars[index].Paint(color); break;
case ObjectType.House: this.houses[index].Paint(color); break;
case ObjectType.Paper: this.papers[index].Paint(color); break;
}
}
public enum ObjectType
{
Car,
House,
Paper
}
}
到目前为止一切都合理,但我想检查索引是否超过了相应数组的长度。
public bool Paint(ObjectType type, int index, System.Drawing.Color color)
{
switch (type)
{
case ObjectType.Car:
if (index < this.cars.Length)
{
this.cars[index].Paint(color);
return true;
}
break;
case ObjectType.House:
if (index < this.houses.Length)
{
this.houses[index].Paint(color);
return true;
}
break;
case ObjectType.Paper:
if (index < this.papers.Length)
{
this.papers[index].Paint(color);
return true;
}
break;
}
return false;
}
此外,我想改变索引的含义。如果为零,则绘制该类型的所有对象,否则绘制列表[index - 1].
public bool Paint(ObjectType type, int index, System.Drawing.Color color)
{
switch (type)
{
case ObjectType.Car:
if (index <= this.cars.Length)
{
if (index != 0)
{
this.cars[index - 1].Paint(color);
}
else
{
foreach (var car in this.cars)
{
car.Paint(color);
}
}
return true;
}
break;
case ObjectType.House:
if (index <= this.houses.Length)
{
if (index != 0)
{
this.houses[index - 1].Paint(color);
}
else
{
foreach (var house in this.houses)
{
house.Paint(color);
}
}
return true;
}
break;
case ObjectType.Paper:
if (index <= this.papers.Length)
{
if (index != 0)
{
this.papers[index - 1].Paint(color);
}
else
{
foreach (var paper in this.papers)
{
paper.Paint(color);
}
}
return true;
}
break;
}
return false;
}
你知道这是怎么回事。我实际上有更多的类型和更多的方法。有没有办法不使用 IPaintable[][]
来封装对不同数组的处理?
你只需要共同实现一个接口。说 IPaintable
.
public interface IPaintable
{
void Paint(System.Drawing.Color color);
}
在所有这些 类 中实施它。
public class MyCar : IPaintable
{
public void Paint(System.Drawing.Color color) { /* ... */ }
}
public class MyHouse : IPaintable
{
public void Paint(Color color) { /* ... */ }
}
...
然后你可以改变Painter的Paint方法如下
public bool Paint(IList<IPaintable> paintables, int index, Color color)
{
if (index < paintables.Count)
{
paintables[index].Paint(color);
return true;
}
return false;
}
不用说要使用哪个数组,只需传递数组本身即可。
如果您发现自己在很多地方都找不到要使用的数组,您可以介绍一种方法来为您做到这一点。
private IList<IPaintable> GetPaintables(ObjectType type)
{
switch (type)
{
case ObjectType.Car:
return this.cars;
case ObjectType.House:
return this.houses;
case ObjectType.Paper:
return this.papers;
default:
throw new ArgumentOutOfRangeException();
}
}
然后称它为
Paint(GetPaintables(ObjectType.House), index, color);
请记住,应用程序中每个枚举只有一个 switch 语句不是问题。问题是一次又一次地重复使用 switch 语句。所以 GetPaintables
本身不是问题,如果它恰好是你切换 ObjectType
的唯一地方。
我的代码炸了,也许你能帮帮我。我的 class Painter
有一些对象的列表:
public class Painter
{
private readonly MyCar[] cars = new MyCar[5];
private readonly MyHouse[] houses = new MyHouse[6];
private readonly MyPaper[] papers = new MyPaper[7];
}
public class MyCar
{
public void Paint(System.Drawing.Color color) { /* ... */ }
}
public class MyHouse
{
public void Paint(System.Drawing.Color color) { /* ... */ }
}
public class MyPaper
{
public void Paint(System.Drawing.Color color) { /* ... */ }
}
我添加了一种允许绘制其中一个对象的方法:
public class Painter
{
private readonly MyCar[] cars = new MyCar[5];
private readonly MyHouse[] houses = new MyHouse[5];
private readonly MyPaper[] papers = new MyPaper[5];
public void Paint(ObjectType type, int index, System.Drawing.Color color)
{
switch (type)
{
case ObjectType.Car: this.cars[index].Paint(color); break;
case ObjectType.House: this.houses[index].Paint(color); break;
case ObjectType.Paper: this.papers[index].Paint(color); break;
}
}
public enum ObjectType
{
Car,
House,
Paper
}
}
到目前为止一切都合理,但我想检查索引是否超过了相应数组的长度。
public bool Paint(ObjectType type, int index, System.Drawing.Color color)
{
switch (type)
{
case ObjectType.Car:
if (index < this.cars.Length)
{
this.cars[index].Paint(color);
return true;
}
break;
case ObjectType.House:
if (index < this.houses.Length)
{
this.houses[index].Paint(color);
return true;
}
break;
case ObjectType.Paper:
if (index < this.papers.Length)
{
this.papers[index].Paint(color);
return true;
}
break;
}
return false;
}
此外,我想改变索引的含义。如果为零,则绘制该类型的所有对象,否则绘制列表[index - 1].
public bool Paint(ObjectType type, int index, System.Drawing.Color color)
{
switch (type)
{
case ObjectType.Car:
if (index <= this.cars.Length)
{
if (index != 0)
{
this.cars[index - 1].Paint(color);
}
else
{
foreach (var car in this.cars)
{
car.Paint(color);
}
}
return true;
}
break;
case ObjectType.House:
if (index <= this.houses.Length)
{
if (index != 0)
{
this.houses[index - 1].Paint(color);
}
else
{
foreach (var house in this.houses)
{
house.Paint(color);
}
}
return true;
}
break;
case ObjectType.Paper:
if (index <= this.papers.Length)
{
if (index != 0)
{
this.papers[index - 1].Paint(color);
}
else
{
foreach (var paper in this.papers)
{
paper.Paint(color);
}
}
return true;
}
break;
}
return false;
}
你知道这是怎么回事。我实际上有更多的类型和更多的方法。有没有办法不使用 IPaintable[][]
来封装对不同数组的处理?
你只需要共同实现一个接口。说 IPaintable
.
public interface IPaintable
{
void Paint(System.Drawing.Color color);
}
在所有这些 类 中实施它。
public class MyCar : IPaintable
{
public void Paint(System.Drawing.Color color) { /* ... */ }
}
public class MyHouse : IPaintable
{
public void Paint(Color color) { /* ... */ }
}
...
然后你可以改变Painter的Paint方法如下
public bool Paint(IList<IPaintable> paintables, int index, Color color)
{
if (index < paintables.Count)
{
paintables[index].Paint(color);
return true;
}
return false;
}
不用说要使用哪个数组,只需传递数组本身即可。
如果您发现自己在很多地方都找不到要使用的数组,您可以介绍一种方法来为您做到这一点。
private IList<IPaintable> GetPaintables(ObjectType type)
{
switch (type)
{
case ObjectType.Car:
return this.cars;
case ObjectType.House:
return this.houses;
case ObjectType.Paper:
return this.papers;
default:
throw new ArgumentOutOfRangeException();
}
}
然后称它为
Paint(GetPaintables(ObjectType.House), index, color);
请记住,应用程序中每个枚举只有一个 switch 语句不是问题。问题是一次又一次地重复使用 switch 语句。所以 GetPaintables
本身不是问题,如果它恰好是你切换 ObjectType
的唯一地方。