OOP project design

here is the UML class diagramenter image description here

subscribe method is used to add a herbivore object to the plant’s watchers. when a plant is told to notify, itloops through all of the herbivore object in its watchers list and tells each herbivore to notice its current state.

but here is a error:although the design allows a herbivore object to subscrive to multiple plant objects, the herbivore currently can not know which plant object called its notice method.

so how do i fix the UML diagram in which the notice method of the herbivore is passed a reference to plant that is sending the notice?


If you want a reference to the Plant object in the to-be-notified Herbivore object, change the signature of notify to include a Plant object.

notify(state: Maturity, plant: Plant) 

I would fix this by adding new class and get away from throwing too many design patterns where they are not really true in the domain.

For the plants, there is a amount of new steams, flowering steams, and fruit bearing steams. It also has a 2D point for its location. This is a property with only a get as plants don’t really move. Same with amount of flowers per steam, fruit per steam, and amount of seeds per fruit. A time passes method would update the state of those variables. Put methods to try to eat fruit and try to pollinate. They can be for take parameters for amount of fruit to eat and amount of flowers to pollinate respectively.

For the herbivore class, make it have a interact method that takes a plant. The plant is a out parameter and it is a abstract method. The herbivore has a point and a move method that takes a rectangle as the size of the habit. It has a Boolean method for is at that takes a point and checks if its point has the same value as the parameter.

I would add a habitat class. The class has a list of plants and a list of herbivores. It also has a rectangle for its bounds. It has a update method. This calls the time passes method for the plant. Then it moves the herbivores. Finally, the method checks for collisions and calls the interact method of the herbivores.

Trả lời

Email của bạn sẽ không được hiển thị công khai. Các trường bắt buộc được đánh dấu *