Reputation: 33
I am working on an app with a designer, in which can resize some shape items. To resize different shapes in only one 'ResizeThumb' type, I have this two programs:
switch(Mode)
{
case Mode.CircleCenter:
if (item is Circle circle1)
{
// ...
}
break;
case Mode.CircleRadius:
if (item is Circle circle2)
{
// ...
}
break;
case Mode.RectTopLeft:
if (item is MyRect rect1)
{
// ...
}
break;
// many cases...
default: break;
}
or
if (item is Circle circle)
{
switch (Mode) // just 2 cases
{
case Mode.CircleCenter:
// ...
break;
case Mode.CircleRadius:
// ...
break;
default: break;
}
}
else if (item is MyRect rect)
{
switch (Mode)
{
case Mode.RectTopLeft:
// ...
break;
// 8 cases of every border and corner
default: break;
}
}
else if (item is MyEllipse ellipse)
{
switch (Mode) { /* 8 cases of every border and corner */ }
}
else if (item is Line ellipse)
{
switch (Mode) { /* 2 cases of every endpoint */ }
}
else
{
// No Code
}
When I have many shapes to resize, which will be faster and more stable? It seems that the first one will be faster, but I'm not sure. And the second one will be simple and convenient (no need use rect1, rect2 , ... ,rect8
). Which should be used in my app?
Upvotes: 0
Views: 151
Reputation: 112632
Note that you can use the type pattern in the switch as well. So you don't actually need an if-else.
switch (item)
{
case Circle circle:
...
case MyRect rect:
...
case MyEllipse ellipse:
...
case Line line:
...
}
So you can nest switch statements. Whether the item or the mode is nested does not matter.
But another question arises here. Couldn't you make these actions virtual methods of the shapes? Let's assume that you have a base object
public abstract class Shape
{
public abstract void ResizeThumb(Mode mode);
}
Then you would derive all the shapes from this base Shape
public class Circle : Shape
{
public override void ResizeThumb(Mode mode)
{
switch(Mode)
{
case Mode.CircleCenter:
//TODO: do circle things here
break;
case Mode.CircleRadius:
//TODO: do circle things here
break;
case Mode.RectTopLeft:
//TODO: do circle things here
break;
default:
break;
}
}
}
Then you can call this method without caring about the type of the item
item.ResizeThumb(mode);
Also, I'm not sure, why resizing thumbs has to be done in a different way for different shapes. By applying a coordinate transformation, you can resize every shape the same way.
Upvotes: 2
Reputation: 1845
My suspicion is that you should not be using a switch and you should not be testing the object type. This is a classic illustration of polymorphism in an object-oriented language. Your code should look like this.
namespace ConsoleApplication1
{
abstract class Shape
{
abstract public void ResizeThumb();
}
class Circle : Shape
{
public override void ResizeThumb() {
// Your code to resize a circular thumbnail goes here
}
}
class Ellipse : Shape
{
public override void ResizeThumb() {
// Your code resize an elliptical thumbnail goes here
}
}
class Rectangle : Shape
{
public override void ResizeThumb() {
// Your code to resize a rectangular thumbnail goes here
}
}
class Program
{
public void Main() {
// Create a collection of shapes.
Shape[] shapes = new Shape[] {
new Circle(),
new Ellipse(),
new Rectangle(),
};
// Resize each shape's thumbnail.
foreach(Shape shape in shapes) {
shape.ResizeThumb();
}
}
}
}
Upvotes: 1