R
R
Ruslan2020-11-10 16:14:46
OOP
Ruslan, 2020-11-10 16:14:46

How to select classes in OOP?

Hello.

All OOP tutorials give simple and clear examples of classes like washing machine and things, the machine can wash things. Both abstractions are not related to each other, everything is clear.

In practice, I have a query that contains a list of devices and a list of commands to be executed on the devices.
[
{'device_name': 'device1', 'commands': ['sh run']},
{'device_name': 'device2', 'commands': ['sh run', 'sh ip int brie']}
]
As a class, I chose device and added the commands_to_execute property to it, where I placed the list of commands.
But in the real world, devices and commands are different entities, and then you need to create the device and command classes, but I don’t understand how to connect them beautifully, because I need to execute certain commands on certain devices.

How to be in a similar situation?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
M
montray, 2020-11-10
@Ruslan_kulm

In general, it all depends on what exactly your system does and whether it will develop.
But, if you think objectively, then a device is something that receives a certain command as input, it executes it and gives an answer. Accordingly, you need a Device implementation that, through the execute(Executable command) method, accepts an object that implements the Executable interface. And the list of commands has nothing to do with the device, so it must be stored separately.

A
Alexey P, 2020-11-10
@ruddy22

Everything depends on your imagination, as in programming textbooks.
Many classes can be distinguished here:
1. Array class, which contains a list of devices or commands
2. Device class, which displays the essence of the device and contains a list of commands
3. Command class, which displays the essence of the
command

class Device {
  name: String
  commands: Array<Command>
}
class Command {
  shell: String
}

Here you can put accessor methods for accessing data or other dregs that you want

A
Anton R., 2020-11-10
@anton_reut

Read what Composition and Aggregation are in OOP, you will understand a lot.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question