简体   繁体   English

使用哪种设计模式(主动和被动方法)?

[英]Which design pattern to use (Active and passive methods)?

I have a player which can feed a dog or chop a tree . 我有一个可以喂狗砍树玩家

Below are the classes I have written: 以下是我写的课程:

public class Dog {

    private int health;

    public void feed(Food food){
        health = health + food.getNutritionalValue();
    }
}

public class Player{

    public void feed(Dog dog, Food food) {
        dog.feed(food);
    }

Player and Dog both have methods that are "active" . 玩家都有“活跃”的方法

Player feeds the dog and dog starts eating the food (I am not really sure if it is good to couple methods in this way). 玩家喂狗和狗开始吃食物 (我不确定以这种方式结合方法是否好)。

On the other hand, I have tree . 另一方面,我有 And player is able to chop the tree. 玩家可以砍树。

public class Player{ 公共类玩家{

public void chop(Tree tree) {
        //At this point I am not sure
    }

I am not sure if I would use getters and setters of Tree class to interact with the Tree. 我不确定是否会使用Tree类的gettersetter来Tree进行交互。

Or if I should write an own method for this because the tree gets chopped so it is nothing really active I would call. 或者,如果我应该为此编写一个自己的方法,因为树被切断,所以它不会真正活跃我会打电话。

So, in the end, there would be two or more kinds of implementations but the two I am thinking of are: 所以,最后会有两种或更多种实现,但我想到的是:

tree.setAmountofWood = x

or 要么

tree.gettingChopped(Damage int)

I think I should make an own method for this chopping-process . 我想我应该为这个斩波过程制定一个自己的方法。

Or is there any design principle I should follow? 或者我应该遵循任何设计原则?

I would start from something like this. 我会从这样的事情开始。

Tree can grow and receive damage. 树可以生长并受到伤害。

public class Tree {
    private int lumber;

    public Tree(int size) {
        this.lumber = size;
    }

    public void grow() {
        this.lumber++;
    }

    public void grow(int size) {
        this.lumber += size;
    }

    public int receiveDamage(int damage) {
        int lumber = 0;
        if (damage > this.lumber) {
            lumber = this.lumber;
            this.lumber = 0;
        } else {
            lumber = damage;
            this.lumber -= damage;
        }
        return lumber;
    }
}

Food just stores nutritional value. 食物只是储存营养价值。

public class Food {
    private int nutrition;

    public Food(int nutrition) {
        this.nutrition = nutrition;
    }

    public int getNutritionalValue() {
        return this.nutrition;
    }
} 

I'm not sure if all types of player can chop trees, so I created a class to separate responsibilities. 我不确定所有类型的玩家是否都可以砍树,所以我创建了一个分类责任的类。 You can move methods to the Player class if you like. 如果您愿意,可以将方法移动到Player类。

public class Woodcutter extends Player {
    public int chop(Tree tree) {
        // lumber amount may depend on a tool,
        // i.e. axe, chainsaw, etc.
        return tree.receiveDamage(10);
    }

    // fell down the tree
    public int fell(Tree tree) {
        int result = 0;
        int lumber = 0;
        do {
            lumber = chop(tree);
            result += lumber;
        } while (lumber > 0);
        return result;
    }
}

Somewhere in your code 代码中的某个地方

// create a tree and let it grow for a while
Tree tree = new Tree(10);
tree.grow(90);

// Start chopping
Woodcutter woodcutter = new Woodcutter();
System.out.println("Lumber received: " + woodcutter.chop(tree));
System.out.println("Lumber received: " + woodcutter.fell(tree));

Dog dog = new Dog();
Food food = new Food(5);
woodcutter.feed(dog, food);

I wouldn't dive into passive/active methods here. 我不会在这里深入探讨被动/主动方法。 An 'active tree' may indeed be a misnomer. “活跃的树”可能确实是用词不当。

I would rather consider calling an object's method as passing a message to the object. 我宁愿考虑将对象的方法调用为将消息传递给对象。 And you apparently need to send the message to the tree that it is currently being cut by someone, and let the tree decide when to eg fall() or to bend() , or to shake() . 而且你显然需要将消息发送给当前正由某人剪切的树,然后让树决定何时例如fall()bend()shake()

The tree has some internal state ( strength ? thickness of its trunk? health ?). 树有一些内部状态( strength ?树干的thicknesshealth ?)。 'Sending a message ' to the tree means to call its method, eg beingCut() , which in turn deteriorates the state of the tree. “向树发送消息 ”意味着调用其方法,例如, beingCut() ,这反过来会恶化树的状态。 After the state of the tree reaches a certain limit, other actions (=consequences of tree's bad state) may be started by the tree . 在树的状态达到一定限度之后,树可以开始其他动作(=树的坏状态的结果)。

Of course, as in every iteration of your main loop you tree has also the chance to get the message to grow() , so its state may improve a little each time, so eventually it may even recover from being only partially cut and reach its initial, perfect state back. 当然,正如在你的主循环的每次迭代中,你的树也有机会获得消息 grow() ,所以它的状态每次都会有所改善,所以最终它甚至可以从仅部分切割恢复到达它最初,完美的状态回来。

So, yes, while trees seem rather passive, they still react to messages/stimulus. 所以,是的,虽然树木似乎相当被动,但它们仍然会对信息/刺激作出反应。 :-) :-)

I see 3 principles here, 我在这里看到3个原则,

  1. SRP - It is the responsibility of the Tree to get chopped and fall down, but to cut is the responsibility of the Person! SRP - 树的责任是砍伐和摔倒,但切割是人的责任!
  2. Demeter's law - looks good from my POV. 德米特定律 - 从我的POV看起来很好。
  3. OCP - The tree must be able to do further actions when get cut. OCP - 树在切割时必须能够做进一步的操作。

So you must use 所以你必须使用

tree.gettingChopped(Damage damage)

To your code: 致你的代码:

  1. The method Dog.feed is wrong, rename it to Dog.eat because the Dog is not feeding, the dog is eating. 方法Dog.feed是错误的,将它重命名为Dog.eat因为狗没有喂食,狗正在吃。 By the way, the food must reduce its NutritionalValue . 顺便说一句,食物必须减少其营养价值
  2. The health is an integer value, this is bad because in reality there is nothing like a numeral health. 健康是一个整数值,这很糟糕,因为实际上没有数字健康。 We may have a handicapped numeral value in percent, but this is more a byte who not can be in negative value. 我们可能有百分比的残疾数字值,但这更像是一个不能负值的字节。 You should create a custom class for the Health! 您应该为Health创建一个自定义类! This way your code is open(OCP) for extensions like to be toxified or depresive. 通过这种方式,您的代码是开放的(OCP),用于扩展,例如中毒或抑郁。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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