-4

I am developing an image editor app so which approach is better for class design from mentioned below? Any one of three or any new?

First

class Image{

    string path

    rotateLeft(){};
    rotateRight(){};
    changeBrightness(){};

}

or Second

class Image{

    string path

}

Image image = new Image();

Class Editor{

    rotateLeft(Image image){}
    rotateRight(Image image){}
    changeBrightness(Image image){}
}

or Third

Interface Operation{

    rotateLeft(Image image);
    rotateRight(Image image);
    changeBrightness(Image image);

}

class Image Implements Operation{

    string path;
    rotateLeft(this){}
    rotateRight(this){}
    changeBrightness(this){}

}

please discuss pros and cons for both approach.

Kevan
  • 1,085
  • 1
  • 9
  • 15
  • Well, you would want to create the image class holding all 4 variables, the first one private. Then in your editor class, you want to create the Image objecct. – Evan Carslake Feb 06 '15 at 15:51
  • This might be better on Programming.SE. It's rather opinionated. My opinion: neither; how about a filter graph? Image operations are filters (rotate, histogram equalization, etc.). Image goes into filter, new (modified) image comes out of filter. Easier to add new filters and plugins. And filters can be composed into a larger graph (so new "operations" can be exposed to the user that are really just a combination of multiple lower-level filters). – Cornstalks Feb 06 '15 at 15:51

2 Answers2

1

In my point of view I think that you can go more further in term of Single Responsibility Principle(RSP). But I'm not really sure if it relevant to make an interface for Editor.

class Image{
  string path
}

Image image = new Image();

interface Rotatory {
  rotate(image);
}

class RightRotator implements Rotatory{
  rotate(image){
  /*Your implementation*/
  }
}

class LeftRotator implements Rotatory{
  rotate(image){
  /*Your implementation*/
  }
}

interface Editor{
  change(image);
}

class Brightness implements Editor{
  change(image){
  /*Your implementation*/
  }
}
utnas
  • 448
  • 1
  • 5
  • 10
0

It is better to separate out your data objects from your manipulation objects. This will allow you to create different implementations of your manipulation objects, and also allow for mocking which helps with unit testing.

You should create interfaces for your classes as well, and wherever possible use those in the signatures of your class methods. Classes should avoid referencing other classes directly. Instead, pass dependencies into the constructor, which take interfaces. This is known as dependency injection.

error
  • 694
  • 4
  • 14