mardi 13 décembre 2022

One vs several objects - which is better OOP approach?

I am not sure if this question should be here or on another community on stackexchange. I have some problem regarding design. Here it is an simplified example of my code.

class Data:
store = {
    'key':[]
}
def __init__(self):
    self.arg1 = 'value'
    self.arg2 = 'value'
def add_to_store(contents):
   self.store['key'] += contents

Arguments arg1 and arg2 will always be the same while initializing object of class Data (not that important). Only store will change depending on contents of a file.

My dilemma is:

is it better to initialize an object in a for loop and each time working on the new one:

for file_content in files_contents:
   d = Data()
   d.add_to_store(file_content)

or should I create only one object and add method that will clear my dictionary?

d = Data()
for file_content in files_contents:
   d.add_to_store(file_content)

Which is better practice? or it depends and both are correct?

Template Pattern how to update each method status in Main Method c# Windows App

Template Pattern how to update each method status in Main Method c# Windows App


using System; namespace TemplateMethodDesignPattern { public abstract class HouseTemplate { // Template method defines the sequence for building a house public void BuildHouse() { BuildFoundation(); BuildPillars(); BuildWalls(); BuildWindows(); Console.WriteLine("House is built"); }

    // Methods to be implemented by subclasses
    protected abstract void BuildFoundation();
    protected abstract void BuildPillars();
    protected abstract void BuildWalls();
    protected abstract void BuildWindows();
}

}

on Main method i wanted to show each method status/output based on progress in listbox c#

tried with delegates but didn't succeeded.

What is the best way to handon the Clean Architecture for Swift iOS? [closed]

I wanted to get hands-on practice with Clean Architecture for Swift programming on the iOS platform. What I know is - Clean is NOT the same as VIPER.

lundi 12 décembre 2022

Error, does not name a type in State design pattern [closed]

Im relatively new to C++. I'm applying the State design pattern for an IoT application that simply polls a sensor every 15 secs, then if its still connected online, send the data somewhere, and if its not, then save it to an array. However I've been at this code for a few days now and I'm stuck at a part where when I added a TransitionTo in OfflineState I get errors. From my understanding it is because TransitionTo(new OnlineState), OnlineState is declared after I try to use the function, however if I change the order of declaration I'll get an error in TransitionTo(new OfflineState). Coming from C, I expected to just do something similar to adding a prototype, in this case I tried Forward declaring the states classes, but it didn't work. I would like to understand why is it wrong OOP-wise and how to fix it.

#include <iostream>
#include <typeinfo>
#include <WiFi.h>
#include <WiFiClientSecure.h>
// The base State class declares methods that all Concrete State should
// implement and also provides a backreference to the Context object, associated
// with the State. This backreference can be used by States to transition the
// Context to another State.
const char *ssid = "XXX";
const char *password = "XXX";
unsigned long lastTime = 0;
const unsigned long timerDelay = 15000;
int flag = 0;
const TickType_t connectionDelay = 1500 / portTICK_PERIOD_MS;

WiFiClientSecure client;

class State
{
  // @var Context
protected:
  Context * context_;

public:
  virtual ~ State ()
  {
  }

  void set_context (Context * context)
  {
    this->context_ = context;
  }

  virtual void Connect () = 0;
  virtual void CheckConnection () = 0;
  virtual void setMyAttribute (float x) = 0;

};

// The Context defines the interface of interest to clients. It also maintains a
// reference to an instance of a State subclass, which represents the current
// state of the Context.

class Context
{
  // @var State A reference to the current state of the Context.
private:
  State * state_;

public:
  float sensorData = 0;

    Context (State * state):state_ (nullptr)
  {
    this->TransitionTo (state);
  }
   ~Context ()
  {
    delete state_;
  }

  // The Context allows changing the State object at runtime.
  void TransitionTo (State * state)
  {
    Serial.println ("Transitioning");
    if (this->state_ != nullptr)
      delete this->state_;
    this->state_ = state;
    this->state_->set_context (this);
  }

  // The Context delegates part of its behavior to the current State object.
  void Connect ()
  {
    this->state_->Connect ();
  }
  void CheckConnection ()
  {
    this->state_->CheckConnection ();
  }

  void setMyAttribute (float x)
  {
    this->state_->setMyAttribute (x);
  }

};

// Concrete States implement various behaviors, associated with a state of the Context

class OfflineState:public State
{
public:
  virtual void Connect () override
  {
    Serial.println ("connect from offlineState called");
    WiFi.begin (ssid, password);
    Serial.print ("Connecting to wifi...");
    for (int i = 0; i < 10; i++)
      {
    if (WiFi.status () != WL_CONNECTED)
      {
        Serial.print ('.');
        vTaskDelay (connectionDelay);
      }
    else
      {
        i = 10;
        Serial.println ();
        Serial.println ("Connected !");
        this->context_->TransitionTo (new OnlineState);
      }
      }
  }

  void CheckConnection () override
  {

    Serial.println ("Check connection from offlinestate called");
  }

  void setMyAttribute (float x) override
  {
    CheckConnection ();
  }

};

class OnlineState:public State
{
public:
  void Connect () override
  {
    Serial.println ("Already connected");
  }

  void CheckConnection () override
  {
    Serial.println ("Check connection from onlineState called");
    if (WiFi.status () != WL_CONNECTED)
      {
    Serial.println ("Disconnected!");
    this->context_->TransitionTo (new OfflineState);
      }
  }

  void setMyAttribute (float x) override
  {
    CheckConnection ();

  }

};

Context *context = new Context (new OfflineState);

void
setup ()
{

  WiFi.mode (WIFI_STA);
  lastTime = millis ();
  Serial.begin (115200);
  context->Connect ();
  context->CheckConnection ();
}

void
loop ()
{
  if (millis () > (lastTime + timerDelay))
    {

      context->CheckConnection ();
      lastTime = millis ();
    }
}

I've tried making Connect() virtual void so it's declared after both states.

void OnlineState::Connect() {
{
std::cout << "OnlineState: Already connected.\n";
}
}

But I still get errors since in the class declaration, the class appears to be abstract.

How to populate CRM Contact data to another Microservice?

Situation:

I have a Spring Boot application that stores Project Entities with associated Announcement Entities. This Announcement Entities have 1 to N Contact Entities.

The Contact table has fields like: first name, last name, e-mail, mobile, and so on.

If we create a project and an announcement, we must specify at least one contact. Previously, the contact was created in the database using text input fields.

Now it is a requirement to get the contact data from our CRM system. At the same time, it should also be possible to trace which contact is assigned to which Project in the CRM System.

Problem:

Unfortunately, I don't have much experience with software architecture. Whatever my supervisor wants me to work out a solution for this. I would be very grateful if you could help me a little and give me a direction to go.

My idea:

At first, This bidirectional relation between the CRM System and my service sounds redundant to me. I would suggest to fetch the contact data from an CRM Endpoint by company name (a company can create a project) and provide the found contacts as dropdown or autocomplete textfield. If a contact not exist, a new contact will be created about another CRM Endpoint. Next, I would persist the project-announcement-contact relationship in my service. In the CRM system I would only create a new contact if needed. For the CRM system I would create an endpoint that returns projects/announcements by contacts. this is a bit weird, but that's how I understand the requirement.

Second Problem:

What if a contact changes in the CRM system. I have to reflect that somehow in my service. Should i use event driven design here and a message broker?

I would be very grateful if someone with experience could give me some hints and tips.

dimanche 11 décembre 2022

Creating a chat functionality using Slack API

I am having a problem understanding this problem as I have not had much - if any - interaction with Slack API. The problem is listed as follows:

We need to integrate a chat function allowing users to talk to trainers. Slack API is going to be used for this function.

Now, say - UI and Auth are both already implemented. The following is not implemented as part of the network layer yet :

1 - Send/read messages

2 - List/create groups and conversations (channels)

3 - Get a list of the users in the team

4 - Update current status from online, away, etc...

The question is:

How would you design the network layer for this service? Why did you choose such a design?

I am really not understanding the way this is worded and any assistance would be of much appreciation and cordiality here.

This is what my understanding is use Slack API pre-built-in functions:

1 - To get and post messages.

2 - To list and create new channels

3 - To get the list of the team users

4 - To update user status

The problem is this does not sound like a design as this latter would require a database and such.

Design patterns for chaining dependency objects

I have a design problem and I can't find the right way to deal with that. In my problem there are 4 classes:

  • class G
  • class T depending on the G
  • class F depending on T
  • class P depending on G, T and F

Additionally each concrete implementation of P should use exact implementation of T and when F is used then T is not needed. However F and T are different concepts (I would say F is some transformation or different interpretation of T) and it is hard to design common interface for them (otherwise I would think about decorator pattern). I was thinking about approach like below:

class PImpl extends P {
     G g;
     T t;
     F f;

     PImpl(G g) {
        this.g = g;
        this.t = new TImpl(g);
        this.f = new FImpl(t);
     }
}

But isn't it breaking the Dependency Inversion Principle from SOLID? Another idea which is an extenstion of above is additional introduction of initialization method for TImpl and FImpl. Then I would provide implementation classes in the constructor and then do exact construction in the constructor body.

class Test {
    main() {
       P p = new PImpl(new GImpl(), new FImpl());
    }
}

abstract class P {
    G g;
    T t;
    F f;

    P(G g, T t, F f) {
        this.g = g;
        this.t = t.create(g);
        this.f = f.create(t);
    }
}

class PImpl extends P {
     
     PImpl(G g, F f) {
        super(g, new TImpl(), f);
     }
}

But I'm not sure if this brings any additional value and only makes the code more complex.