Giter Club home page Giter Club logo

java-tetro-puzzle's Introduction

java-tetro-puzzle

java-tetro-puzzle's People

Contributors

viiviii avatar

Watchers

 avatar

java-tetro-puzzle's Issues

Cells와 Offsets 합치기

넌 지금 Offsets과 Cells를 같은 느낌으로 쓰고있어....

조짐 포인트

  • 어느 순간 TDD를 미뤘다
  • <구현이 아닌 인터페이스에 따라 프로그래밍 해라>를 지키지 않았다
  • 예시로 자동차가 엔진 구축 방법이나 factory를 알고 있는 케이스 -> 너무 많이 알고있음
    • primary object에서 object graph construction 책임을 분리하면 더 유연한 설계가 될 수 있다
  • 디미터 법칙이란 걸 생각해보지 않았다

Puzzle 리팩토링

원인

  • Immutable하려고 하다보니 안읽히는 부분이 많아짐

발생하는 문제점

  • puzzle.put(block)을 하는데 반환값이 Optional<Puzzle>인 등 어색한 부분 발생
  • inner class인 FitCells의 문제점
    • outer class인 퍼즐의 빈 칸과 서로 연계되는 값이지만 독립적임
      • Fit으로써 하는 일이 없음
    • public static하므로 밖에서도 언제든 생성 가능 -> 퍼즐과 퍼즐의 채워진 칸이 따로인가?
  • 메서드 흐름이 잘 안읽힘
    • 메서드 대칭성이 없고 중구난방임
  • 퍼즐에 블럭이 맞춰졌을 때 보드의 빈칸이 사라지나?
    • 보드의 빈칸은 변하지 않는 값이며
    • 퍼즐의 빈칸이 변하는 값이라 생각함

public final class Puzzle {
private final Board board;
private final FitCells fitCells;
private final Board.BlankCells remainingBlankCells;
private Puzzle(Board board, FitCells fitCells) {
this.board = board;
this.fitCells = fitCells;
this.remainingBlankCells = board.blanks().difference(fitCells);
}
public Puzzle(Board board) {
this(board, FitCells.EMPTY);
}
public boolean completed() {
return remainingBlankCells.size() == 0;
}
public Board.BlankCells remainingBlankCells() {
return remainingBlankCells;
}
public FitCells fitCells() {
return fitCells;
}
// todo: 메서드명이랑 Optional 리턴하는게 안어울려
public Optional<Puzzle> put(Block block, Offset boardOffset) {
final FitBlock fitBlock = new FitBlock(block, boardOffset);
final boolean canFit = remainingBlankCells.containsAll(fitBlock);
return canFit
? Optional.of(new Puzzle(this.board, fitCells.add(fitBlock)))
: Optional.empty();
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Puzzle)) return false;
Puzzle that = (Puzzle) o;
return board.equals(that.board)
&& fitCells.equals(that.fitCells)
&& remainingBlankCells.equals(that.remainingBlankCells);
}
@Override
public int hashCode() {
return Objects.hash(board, fitCells, remainingBlankCells);
}
// todo: 클래스명
public static final class FitCells extends AbstractNonBlankCells {
public static final FitCells EMPTY = new FitCells(Offsets.EMPTY, Collections.EMPTY_SET);
private final Offsets offsets;
private final Set<FitBlock> fitBlocks;
private FitCells(Offsets offsets, Set<FitBlock> fitBlocks) {
this.offsets = offsets;
this.fitBlocks = fitBlocks;
}
public FitCells add(FitBlock fitBlock) {
final Offsets unionOffsets = Offsets.union(this.offsets, fitBlock.offsets());
final Set<FitBlock> newFitBlocks = new HashSet<>(this.fitBlocks);
newFitBlocks.add(fitBlock);
return new FitCells(unionOffsets, newFitBlocks);
}
@Override
public Offsets offsets() {
return this.offsets;
}
// todo
public Set<FitBlock.State> blockStates() {
return this.fitBlocks.stream()
.map(e -> e.blockState())
.collect(Collectors.toUnmodifiableSet());
}

Puzzle.NonBlanks 클래스 mutable 표현

  • 클래스명이 mutable한게 표현됐으면 좋겠음

public final class Puzzle {
private final NonBlanks nonBlanks = new NonBlanks(); // todo: mutable 명시적이게

private final class NonBlanks extends AbstractNonBlankCells {
private final Set<FitBlock> fitBlocks = new HashSet<>();
public boolean add(FitBlock fitBlock) {
final boolean canFit = Puzzle.this.blanks().canFit(fitBlock);
return canFit && fitBlocks.add(fitBlock);
}
public boolean remove(FitBlock fitBlock) {
return fitBlocks.remove(fitBlock);
}

immutable object일 때 메서드 컨벤션

offset.difference(other); vs Offset.difference(o1, o2);

  • 프로젝트에서 일관적으로 유지하기
  • 앞의 방식으로 쓰면 비슷한 패턴으로 mutable 객체를 리턴하면 안될 것 같음
  • 위와 같은 경우엔 setDifference() 식으로 할까?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.