1

I've recently read that calling classes **Manager* is a bad thing due to their not exact name and a possibility that they might become god objects. However, is it a good practice to provide 'Manager' as a wrapper for multiple different methods from different classes dealing with the same business object?

Let's say my callers want to interact with orders. Is it better to mix all kinds of methods into a single **Manager* class which will only delegate them to a proper class or let the caller use the proper class itself.

So

OrderManager orderManager = new OrderManager();
orderManager.cancelOrder(order); -> delegates to OrderShredder
orderManager.sendOrder(order, destination); -> delegates to OrderSender

or

new OrderShredder().cancelOrder(order);
new OrderSender().sendOrder(order, destination);

And what about classes which do a bit more than a simple delegation (they use multiple delegations, execute them in a correct order or choose a next path based on some result from a delegate). Can these type of methods (like below) be in some kind of a manager class?

public Order makeOrder(List<Product> products, Customer customer) {
    BigDecimal orderValue = this.productPriceCalculator.calculatePrice(products, customer);
    Order order = this.orderCreator.createOrder(products, customer, orderValue);
    boolean orderIsOk = this.orderValidator.validate(order);
    if (orderIsOk) {
        OrderStatistics orderStatistics = this.orderEvaluator.evaluate(order);
        boolean orderValueIsBigEnough = orderStatistics.isValueBigEnough();
        if (orderValueIsBigEnough) {
            this.orderSender.sendInformationAboutOrderSomewhere(order, orderStatistics);
        }
    }
    else {
        throw OrderNotOkException(order);
    }

    return order;
}

public void cancelOrders(Customer customer) {
    List<Order> customerOrders = this.ordersStorage.getOrders(customer);
    for (Order order : customerOrders) {
        orderShredder.cancelOrder(order);
    }
}
4

1 に答える 1

0

ビジネス ロジック オブジェクトではなく、ビジネスロジック を配置する必要があるのはなぜだと思いますか?

new OrderShredder().cancelOrder(order);

どうですか

order.cancel()

実際、この呼び出し自体がOrderShredder(本当に必要な場合) を呼び出すかどうかは気にしませんか? これはオブジェクトへのコマンドであり、オブジェクトに何をすべきかを伝えます。それがどのように物事を行うか、あなたは気にしません。

サービス/マネージャー/その他のクラスのメソッドを実行し、最初の引数の静的型を確認します。ビジネス ロジック オブジェクトに実際に動作を割り当てて、それ自体を認識させることにより、これらのクラスを排除するようにしてください。

ネーミングの側面について:ネーミングの質問に言語学を適用するイタチ語 (残念ながら自由ではないドイツ語)についての非常に優れた記事がありました。正確な詳細を思い出すことはできませんが、議論は、-erまたはで終わる-or名詞は名詞化された動詞であり、Manag-er. .

于 2012-05-11T06:17:01.237 に答える