Which software design pattern is best for the following scenario (C #)

I have a gps device that records data, for example. datetime, latitude, longitude

I have sdk that reads data from a device. The way to read data:

A command packet (basically a combination of int values ​​in the structure) is sent to the device. The device responds with data in blocks of a fixed size, for example. 64bytes

Depending on the issued command, I will return to different data structures, for example, sending command 1 to the device returns a structure of type

struct x
{
 id int,
 name char[20]
}

command 2 returns a collection of the following structures (basically it comes down to an array of structures - y [12])

struct y
{
 date datetime,
 lat decimal,
 lon decimal
}

Then I would like to convert the structure to a class and store the data in a database.

, ?

+3
5

, .

- , , , .

factory , (, /)

ctor .

/, , ( )

, , , , rs232, .

0

factory.

factory, . , . factory .

. , , -, . factory .

Decorator. , , . , Decorator .

(DAO) CRUD. , , JDO.

+1
0

@

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

Btw Tim , .

0

, :

// CALLER
public class Program
{
    static void Main(string[] args)
    {
        Device<Command1, S1> cmd1 = new Device<Command1, S1>();
        S1 s1 = cmd1.ExecuteCommand(new Command1());

        Device<Command2, S2> cmd2 = new Device<Command2, S2>();
        S2 s2 = cmd2.ExecuteCommand(new Command2());
    }
}

// SDK
public interface ICommand<T>
{
    T Execute();
}

public struct S1
{
    public int id;
}

public struct S2
{
    public string name;
}

public class Command1 : ICommand<S1>
{
    public S1 Execute()
    { return new S1() { id = 1 }; }
}

public class Command2 : ICommand<S2>
{
    public S2 Execute()
    { return new S2() { name = "name" }; }
}

// DEVICE
public class Device<T, U> where T : ICommand<U>
{
    public U ExecuteCommand(T cmdObject)
    {
        return cmdObject.Execute();
    }
} 
0

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


All Articles