How many variables are too many for the class?

I want to see if anyone has a better design for the class (class, like in OOP ) I write. We have a script that puts public folder statistics in a CSV file . I read this and put it in the Share class.

My boss wants to know information like:

  • Total number of files
  • Total file size
  • Number of Office Files
  • Office File Size
  • Number of Exe Files
  • Exe File Size
  • etc.

I have a class with variables like $ numOfficeFiles, $ sizeOfficeFiles, etc. with tons of get / set methods. Isn't there a better way to do this? What is the general rule if you have a class with many variables / properties?

I think of it as an agnostic language issue, but if that matters, I use PHP.

+3
11

, 5 6 , , antsy.

, , Outlaw Programmer. , -.

: , , setter getter, DATA, - - .

, (), , , ( OO, ).

: , , ; , .

GUI , , ; - ( ).

+8

:

files = {
   'total':  { count: 200, size: 3492834 },
   'office': { count: 25, size: 2344 },
   'exe':    { count: 30, size: 342344 },
   ...
}
+8

" "

, , .

.

100-, , .

get/set, "".

EDIT:

, Count Size. , . FileInfo count class, frist FileInfo.

, . "", "Exe"., File Info. FileInfo.

, .

+3

, " , ".

, , .

, , .

+1

"max variables" , . X , , . , , - "", . .

+1

, . # . , :

public class FileStats
{
    public FileStats(String extension)
    {
        // logic to discover files goes here
    }

    public int getSize() { }
    public int getNumFiles() { }
}

, , :

public class Statistics
{
    private static final String[] TYPES = { "exe", "doc", "png" };
    private Collection<FileStats> stats = new HashSet<FileStats>();

    public static void collectStats()
    {            
        stats.clear();
        for(String type : TYPES)
            stats.add(new FileStats(type));
    }
}

API, getter:

public int getNumFiles(String type)
{
    return stats.get(type).getNumFiles();
}
+1

. OO coupling cohesion. , , /, .

0

, :

, " ".

0

, , , csv (?). csv.

0

.

. get/set.

- :

class Test()
{
    private $DATA = array();

    function set($what,$data) {
       $DATA[$what] = $data;
    }

    function get($what) {
        return $this->DATA[$what];
    }
}
0

, " " " ", . "" .

In this case, it seems that you can start grouping things together, for example, you repeat the actions of number and size many times. Why not create a superclass inherited by other classes, for example:

class NameOfSuperClass {
    public $type;
    function __construct($type) {
        $this->type = $type;
        $this->getNumber();
        $this->getSize();
    }
    public function getNumber() {
        // do something with the type and the number
    }
    public function getSize() {
        // do something with the type and the size
    }
}

Class OfficeFiles extends NameOfSuperClass {
    function __construct() {
        $this->_super("office");
    }
}

I'm not sure if this is correct in PHP, but you understand. Things will start to look a lot cleaner and better managed.

0
source

All Articles