Here i build a simple code using class-inheritance pattern (I took it from my game made just for trainig) :
class BasicGamePiece {
constructor(param) {
this.size = param.size
}
fire(){
console.log('fire')
}
}
// first and second children . Got different methods move() and attachTo() plus different properties
class MovingWeaponPiece extends BasicGamePiece {
constructor(param) {
super(param)
this.movementPoints = param.movementPoints
}
move(){
this.movementPoints -= 1
}
}
class StaticWeaponPiece extends BasicGamePiece {
constructor(param) {
super(param)
this.owner = param.owner
}
attachTo(owner){
this.owner = owner
}
}
// now children of MovingWeaponPiece
class Squad extends MovingWeaponPiece {
constructor(param) {
super(param)
}
getUnderCommand(){
console.log('ready to action')
}
fire(){ // shadowing
super.fire()
console.log('as squad')
}
}
class Vehicle extends MovingWeaponPiece {
constructor(param) {
super(param)
}
ignition(){
console.log('at the start!')
}
}
// and the third step of inheritance :
class Tank extends Vehicle {
constructor(param) {
super(param)
this.orientation = param.orientation
}
rotateArmament(where){
this.orientation = where
}
}
For clarity i made this scheme :

Now i want to add another class Artyllery :
It MUST HAVE method attachTo() (same as class Weapon) and property this.owner (same as class Weapon)
It MUST HAVE rotateArmament() (same as class Tank) and property this.orientation (same as class Tank)
It SHOULD NOT HAVE Vehicle-like methods ignition()
So questions are :
1) How should i correctly add class Artyllery ?
and
2) What if i want to refactor all my code from class-inheritance pattern, to the Behavior Delegation design pattern (same as in the Kyle Simpson's book 'You don't know Java Script').
Is it will be easier to add Artillery in this case ?
I would be grateful if someone show me the refactored code