-2

i have problem about design a module of my project.

Lets say, you have two classes which both of them almost doing the same thing. You can think like DeviceManager.

Both of them connect/disconnect/parse exactly the same way. After parse the message from device they need to do different things like one of them print a message, the other one pass it to somewhere else.

I need to design this structure best as oop allows maybe with inheritance,interface,abstract etc. but i am not sure what is the best way to approach this problem.

Note: I dont want to violate any SOLID principle.

Thanks.

Example:

// lines exactly same code
** line different code

class DeviceMessageNavigator
{
    //connect 
    //wait message
    //parse message
    **//NAVIGATE MESSAGE(Pass it to somewhere else)**
    //disconnect
}
class DeviceMessagePrinter
{
    //connect
    //wait message
    //parse message
    **//PRINT MESSAGE**
    //disconnect
}
Vincent
  • 4,876
  • 3
  • 44
  • 55
Hasan Durukan
  • 83
  • 1
  • 7
  • 1
    You can create a (abstract) root class to refactor common code and data. [OOP](https://www.c-sharpcorner.com/UploadFile/mkagrahari/introduction-to-object-oriented-programming-concepts-in-C-Sharp) | [Classes](https://www.c-sharpcorner.com/UploadFile/84c85b/object-oriented-programming-using-C-Sharp-net) | [Abstraction](https://stackoverflow.com/questions/58765776/#58766026) | [Encapsulation](https://stackoverflow.com/questions/58257849/#58258056) | [Polymorphism](https://stackoverflow.com/questions/1031273/#58197730) | [Interface & Class](https://stackoverflow.com/questions/10914802/#58174007) –  Jun 11 '21 at 11:16

3 Answers3

1

Your own answer is pretty pseudo code'ish, but goes into the right direction. Please post compilable code, it's easier to copy/paste and editable. Therfore easier to help you.

public abstract class DeviceMessageBase
{
    public void Connect()
    {
        // do connect things
    }

    public void WaitMessage()
    {
        // do wait message things
    }

    public void ParseMessage()
    {
        // do parse message things
    }

    public abstract void ProcessMessage();

}

public class DeviceMessageNavigator : DeviceMessageBase
{
    public override void ProcessMessage()
    {
        //**//NAVIGATE MESSAGE(Pass it to somewhere else)**
    }
}

public class DeviceMessagePrinter : DeviceMessageBase
{
    public override void ProcessMessage()
    {
        //**//PRINT MESSAGE**
    }
}
nilsK
  • 4,323
  • 2
  • 26
  • 40
1

You have a couple of options actually:

  • You could create an abstract class with the points in common between the two and then extend it adding the methods you need.
  • You could write the first and then extend it to the second adding what you need
  • You could write a DeviceManager class with a print method and a navigate method (if they really do the same things apart from one this might be your answer)

I think that your view on OOP might not be the proper one, you might want to look more in detail about it. For example it looks to me that you are looking at those classes more like functions: every instruction you commented inside should be a method on it's own. See classes like blueprints of an actual real life object: you can build the object and use it as blueprints intended or you change them to have a different object with different properties and functions. Let's take a car's blueprint, it has some properties like the car's shape, number of doors, etc.. and it has some functions you can use when interacting with it like turning it on, changing radio's volume, accellerating, etc... Following this example your question would be: i need two identical toyota yaris blueprints, one has an incorporated coffee machine and the other has wifi, how can i design them? And the answer would be: take the blueprints for a toyota yaris, make a copy and then add the coffee machine in one and the wifi router in the other.

Fabio R.
  • 393
  • 3
  • 15
  • Also the coffee machine could be a aggregate or a composite as a list of options. [Association, Composition and Aggregation in C#](https://www.tutorialspoint.com/Association-Composition-and-Aggregation-in-Chash) | [Understanding the Aggregation, Association, Composition](https://stackoverflow.com/questions/37915501/understanding-the-aggregation-association-composition) | [What is the difference between association, aggregation and composition?](https://stackoverflow.com/questions/885937/what-is-the-difference-between-association-aggregation-and-composition) –  Jun 11 '21 at 11:56
0

From Olivier's answer i drafted this one, i am not sure this is the best approach.

abstract class DeviceMessage
{
    protected string message;
    //connect
    //wait message
    //parse message
    message=recievedMessage;
    abstract void ProcessMessage();
    //disconnect
}
class DeviceMessageNavigator:DeviceMessage
{
    //connect 
    //wait message
    //parse message
    **//NAVIGATE MESSAGE(Pass it to somewhere else)**
    //disconnect
}
class DeviceMessagePrinter:DeviceMessage
{
    //connect
    //wait message
    //parse message
    **//PRINT MESSAGE**
    //disconnect
}
Hasan Durukan
  • 83
  • 1
  • 7