lundi 5 juillet 2021

Pattern or Type to Handle a "Smart Grid"

enter image description here

Hello. i am designing a configurator that is suppose to handle a Skeleton Grid. witch now i am handling the task with a "Manager" and a class with a lot of dictionary in witch i host all the data (one dict for the node,one dict for the connection, one dict for the face in between the connection) but i feel that it is more and more "Static". is there some design pattern that can i use to have a better result? I saw the Linked List. is there any solution for a "Linked Grid"? Thanks in advance Stefano

dimanche 4 juillet 2021

Design replication of spring boot API consumer app

As title said I'm trying to design replication of API consumer app. Imagine that in system exists server with some sort of subscribe API. So client have to register and after that client will receive data. In one moment only 1 APP is registered and receive data from that server. So if that APP is down I want second APP to subscribe and receive data until first APP is back up and running.

link to image.

If first APP is back up and running, second app will unsubscribe.

I hope I explained it clearly.

Is "throwing stack-less exceptions" a known/common pattern?

Is this a known/common pattern? (example in Java)

class BadInput extends Exception {
    BadInput INSTANCE = new BadInput();
    private BadInput() { }
}

Whenever INSTANCE is thrown, it won't have a sensible stack trace but that's OK as it doesn't signify a coding error/resource issue/security issue/etc. (at a low level) but an input error (at a high level). Also, it won't explain why some input is bad. That's OK as well if no specifics are needed.

Here's a use case about (exact) integer division (a bit silly, but it becomes more practical if it deals with e.g. rationals or polynomials)

class ExactIntegerDivision extends BinaryOperator {
    int compute(int arg1, int arg2) throws BadInput
    {
        if (arg2 == 0 || arg1 % arg2 != 0) throw BadInput.INSTANCE;
        return arg1 / arg2;
    }
}

If the exception is thrown, no cost is incurred in building a stack trace. All intermediate levels don't have the burden of having to check the validity of intermediate results since the exception simply propagates upwards (no checks as in if (!equalsErrorSentinel(subresult)) … are needed, which avoids both performance-penalties as well as coding errors (such checks are bound to be forgotten sometimes).

It seems to be a great pattern (only to be used when the two OK's above are really OK of course). Why have I never come across it?

How to reference new collection in a Model, after the initial design was designed to only refer to one?

Right now, I have a collection called article, with all my articles. Each has a unique id, for example: ABC. Then I have a collection called paragraph which stores the article id, so i know which article each paragraph belongs to. Each paragraph also holds a numeric field called position, so I know which order they appear in in the article (1,2,3..). The article itself doesn't store any information about what paragraphs are inside it.

Now I've recently realised that I also want to also have Images between my paragraphs. So an article might be:

Paragraph
Paragraph
Image
Paragraph
...

But I am not sure how to now make this work!

One solution I came up with was to change my paragraph collection to also include images, so I could rename the collection to items and then store everything in there. But this doesn't seem very neat?

PS: For other reasons / requirements that would be too cumbersome to get into here, the solution can't be that I just move my paragraphs & images inside article.

How to bind to specific elements of a collection?

For example, I have a collection where data is stored like this:

class MyVector3
{
   double X { get; set; }
   double Y { get; set; }
   double Z { get; set; }
}
ObservableCollection<MyVector3> Positions { get; } = new ObservableCollection<MyVector3>();
Positions.Add(new MyVector3() { X = 0.0, Y = 0.0, Z = 0.0 });
Positions.Add(new MyVector3() { X = 1.0, Y = 1.0, Z = 1.0 });
Positions.Add(new MyVector3() { X = 2.0, Y = 2.0, Z = 2.0 });
...

Represent in a image, it is as follows.

Image1

And I want to bind the value of a specific element in the Positions property.

class ModelData
{
   List<MyVector3> ModelPositions { get; set; }
   ...
}
SomeCollection<ModelData> ModelDatas { get; } = new SomeCollection<ModelData>();
ModelDatas.Add(new ModelData() // ModelData 1
{
   ModelPositions = new List<MyVector3>()
   {
      Positions[0],
      Positions[3],
      Positions[9],
      ...
   };
});
ModelDatas.Add(new ModelData() // ModelData 2
{
   ModelPositions = new List<MyVector3>()
   {
      Positions[4],
      Positions[3],
      Positions[7],
      Positions[10],
      ...
   };
});
...

The state of the Positions property is always changing. Elements can be added or deleted, and the element's data can also change. And all these changes should be reflected to the ModelData object.

In the sample code above, the ModelData 1 and ModelData 2 share the value of index 3 of the Postions property. If the value of the index 3 element of the Positions property is changed, how to notify this change only to the elements of ModelData 1 and ModelData 2 that added to the ModelDatas property?

samedi 3 juillet 2021

Should a background process be part of Django project?

I'm trying to build a web app which provide an interface to do some queries upon data extracted from another public API server.

To complete the queries in real time, I would have to prefetch the data from the public API server.

So I think it is reasonable to separate the app deal with query input(there is still some logic here, so only javascript wouldn't be enough) from the app which runs in background and builds the database which could possibly answer the query in real time.

Then what comes to my mind is does this background app really have to be a part of Django project? It runs almost without interacting with any Django component. Except for the database which is also accessible by a Django app and some signals maybe(when to start/stop collecting data, but this could probably also decided internally).

What would a be good design choice for my situation?

Design pattern to inherit derived members from non-modifiable base classes?

I have the following chicken/egg inheritance problem:

Here, base classes I can derive from, but they're on a framework, thus, I can't modify them:

class Editor
{

}

class ScriptedImporterEditor : Editor
{
}

Here, are classes in my project:

An editor with a preview, this works as expected, Cylinder and Torus have DrawPreview:

class EditorWithPreview : Editor
{
    public void DrawPreview(){}
}
   
class Cylinder : EditorWithPreview
{
    // DrawPreview is available
}

class Torus : EditorWithPreview
{
    // DrawPreview is available
}

But now I need a scripted importer editor that can also preview:

class ScriptedImporterEditorWithPreview : ScriptedImporterEditor
{
    // cannot inherit EditorWithPreview as it's not a ScriptedImporterEditor
}

class Cube : ScriptedImporterEditorWithPreview 
{
    // unable to use DrawPreview
}

class Sphere : ScriptedImporterEditorWithPreview 
{
    // unable to use DrawPreview
}

So basically,

  • I can't change neither Editor nor ScriptedImporterEditor as I don't own them
  • I therefore cannot import the logic of EditorWithPreview to ScriptedImporterEditor
  • Cube and Sphere can't inherit and use DrawPreview