Which design is better?

I have an “Enemy” object that has a lot of “pistols.” Each pistol can fire a bullet. Storage "guns" uses an array. when the gun is launched, a bullet will be created. And the enemy object will have an array to store the "bullet".

So, I am thinking of the fire method. I think I'm creating firebulletFromGun in "enemy". It must have a parameter: "gun". while this method is called. The enemy pulp will be added to the array.

Another design is the "gun" method of fire. The "enemy" uses the "gun" method of fire. And the “gun” will return the “bullet” object, and it will be added to the “enemy” array.

Both methods may work, but which way is better? or are they the same? plx drop ur ideas / suggestions. THz

+3
source share
5 answers

It depends on whether you want the bullet to remain “owned” by the enemy or gun.

, ( , " ", , ). " " , , , ( ) , . , , , . , .

, , , - , , , , . , , ( 1- , ), , , , .

, (.. , , ).

, "".

+1

, " , ". , , . , , attack(), - fire() Gun, stab() .

+4

. .

gun.fire(); // bang!

+3

, , .

.

, - , . , , .

, , , .

, Enemy Gun. Fire BulletCount BulletMovement, .

+1

, . , .

. , , - Gun. , , , gun.fire(), enemy.fire()

, . , . .

0
source

Source: https://habr.com/ru/post/1742593/


All Articles