【问题标题】:Setting an instance variable using dependency使用依赖设置实例变量
【发布时间】:2021-12-29 12:49:36
【问题描述】:

我正在从事一个项目,我试图代表股票投资组合。有一个名为Position 的类。此外,还有另一个名为 Portfolio 的类,它基本上代表一组位置以及其他实例变量/方法。

单个Position 存储在一个普通的.txt 文件中。为了读取该文件,我创建了另一个类:ReaderService。在这个类中,执行了必要的 I/O 操作,最后,readPositions() 方法返回了一个 List<Position>

Portfolio 类依赖于ReaderService 以设置其positions 字段,我通过构造函数注入该依赖项,即构造函数注入。

我不能确定的是,使用 ReaderService 依赖项在 setter 方法中初始化 Portfolio 类的实例变量似乎是一种不好的做法。这种方法是否会导致任何问题,例如紧耦合或 smt else?

这里是快速总结

ReaderService:

public class ReaderService {
    // necessary fields..

    // a method to extract all positions from the each line of .txt file.
    public ArrayList<Position> readPositions(){
        ArrayList<Position> positions = new ArrayList<>();
        ...
        /* for each line in the file, a new `Position` is being created
        and being added to `positions` after necessary file operations performed. */
        ...
            // Initializing a new position and adding it to the list
            Position position = new Position(...);
            positions.add(position);

         return positions;
    }
}

Position类:

public class Position {
    private String stockCode;
    private double balance;
    ...
    // There are more than two fields. Truncated it for the sake of the question.
    public Position(String stockCode, double balance, ...) {
        this.stockCode = stockCode;
        this.balance = balance;
        ...
    }
}

还有所讨论的班级; Portfolio类:

public class Portfolio {
    private ArrayList<Position> positions;
    ...
    private final DataService dataService;
    private final ReaderService readerService;

    public Portfolio(DataService dataService, ReaderService readerService){
        this.dataService = dataService;
        this.readerService = readerService;
    }

    public ArrayList<Position> getPositions() {
        return positions;
    }

    // Check out this method. Is it a poor design?
    public void setPositions() {
        try {
            this.positions = readerService.readPositions();
        }
        catch (IOException ex){
            ex.printStackTrace();
        }
    }
}

质疑以下是否正确:

ReaderService 负责读取位置,但您也赋予它为Portfolio 类创建位置列表的责任。'

我的意思是我在这里打破了单一职责原则吗?

我也在考虑制作这些服务类static

【问题讨论】:

  • Portfolio 还做其他事情吗?或者只是创建几个职位?作为一个新手,我认为最好让其他类执行readerService.readPositions() 并创建Portfolio 实例与结果。
  • 是的,投资组合类除了创建多个职位外还有其他职责。回到你的建议,这是我最初的方法。在主课上是这样的。 portfolio.setPositions(reader.readPositions());

标签: java oop dependency-injection


【解决方案1】:

为了可读性,我在这里回答。

我想用构造函数创建而不是setPositions()。并创建另一个类(或者你可以将它放在主类中,但你可能不喜欢它)如下。

public class PortfolioService {
    private final DataService dataService;
    private final ReaderService readerService;

    public PortfolioService(DataService dataService, ReaderService readerService) {
        this.dataService = dataService;
        this.readerService = readerService;
    }

    public PortfolioService createPortfolio() {
         return new Portfolio(readerService.readPositions());
    }
}

那么,Portfolio 类将如下所示。

public class Portfolio {
    private final List<Position> positions;
    
    public Portfolio(List<Position> positions) {
         this.positions = positions;
    }
    
    // and do something else with positions
}

【讨论】:

    猜你喜欢
    • 2019-02-01
    • 1970-01-01
    • 2018-08-09
    • 2017-11-11
    • 1970-01-01
    • 2016-05-08
    • 2019-07-05
    • 2021-11-05
    • 1970-01-01
    相关资源
    最近更新 更多