简体   繁体   中英

Factory class to handle logic of a constructor?

I have a Maze class which has a constructor:

public class Maze implements MazeInterface {

  private int width;          // Width of maze in units
  private int height;         // Height of maze in units
  private long seed;          // Seed generated for the maze
  private Cell[][] board;     // 2D representation of the maze

  public Maze(int w, int h, Cell[][] maze, long s){
      width = w;
      height = h;
      board = maze;
      seed = s;
  }

  // Bunch of other methods/stuff irrelevant

}

I was reading online about good practices and now I understand that a complex constructor is bad practice. So I thought a factory class would be a viable solution. What I want to do is generate a random seed, random w/h values for the maze (how many units wide or tall the maze is), and other logic for the Maze object. Is the factory pattern the right way to do this?

public class MazeFactory {

  public Maze createMaze(){
      long s = generateSeed();       // Generation of a seed
      int w  = generateW();          // Random w value
      int h  = generateH();          // Random h value

      return new Maze(w, h, new Cell[w][h], s);
  }

  private long generateSeed(){
      // Do stuff and return a seed
  }

  private int generateW(){
      // Do stuff
  }

  private int generateH(){
      // Do stuff
  }

}

Would this separation of logic be beneficial by putting it into a Factory class or is this the wrong way to go about this (wrong pattern use/etc) or should I just do this within the Maze class in the constructor by writing private methods and calling them in the constructor? I am trying to learn about different patterns/best practices, but I think i'm misunderstanding the way Factory design is handled or if I am just using the wrong pattern.

Your question is about when to use creational patterns https://en.wikipedia.org/wiki/Creational_pattern . This is a big a fairly complex topic with really no right and wrong answer. I'll provide my personal 'rules-of-thumb' but I'm sure there will be many contrary views.

The key advantage of a 'factory' pattern is that it separates the creation of an object from its operation. One of the triggers for this is when you don't know ahead of time which class you are creating. You can have a factory that generates a number of different classes depending on the method called. Note that this doesn't necessarily make the construtor any simpler.

The key advantage of a 'builder' pattern is that it breaks a complex constructor down into individual steps that can be varied by the user. This pattern can help when there are many variations on the creation or when you have many potential values that can be set which have sensible default values.

I would guess that a builder is more appropriate for your case. Your construction code might look something like:

Maze myMaze = mazeBuilder.makeMazeOfSize(30, 20).withBoard(values).withSeed(15).build();

This also has the advantage of documenting the meaning of the creation values.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM