jeudi 23 août 2018

Choosing the correct design pattern

I'm writing an api in the form of a .net Standard library. In this library, I need to provide the caller a class which will allow me to send and receive messages from a message queue. The underlying queuing system could be anything, such as ServiceBus, NATS or RabbitMQ and therefor should be interchangeable by the caller.

Something like this:

public class QueueWrapper: IQueue {

    private QueueClient _q;

    public QueueWrapper(QueueClient q) {
       _q = q;
    }

    public void Send() { 
        q.send();
    }

    public string Receive() { 
        return q.receive();
    }
}

Called by:

IQueue msgQueue = new QueueWrapper(new RabbitMQClient());

I think the Adapter Pattern is the correct Design Pattern to choose, but wanted to get advice - does this seem like the correct pattern in this scenario? Would you recommend others?

A similar scenario would be having an emailer class that sends mails and allowing different mail providers to be switched.

Thanks in advance!

CSS Design System - How do you handle anomalies?

I'm thinking of making a CSS design system which includes classes like

.text-red {color: red}

However, I have one question I can't solve, how do you handle anomalies (one of events). A common example could be making an element darker for say one element but the rest aren't affected in anyway.

Im super confused, I love the design system approach but anomalies are a big concern.

How to provide an own instance per variable to each thread (avoid sharing the heap in multi-threading)?

My app builds a complex model based on socket input. Assume that the input comes regularly at two different time intervals, in a hourly interval and in a daily interval. The data is treated exactly the same, only that I want to build a "hourly" model and a "daily" model at the same time, in parallel. The simplest solution would be to duplicate the code with two different socket endpoints, in order to send the hourly and daily data to the different endpoints. Obiously this is not an elegant solution. So, is there an elegant way/design pattern/architecture that supports my purposes? The requirements would be as following:

1. Use the same code base to build different models at the same time, based on the type of input
2. At the same time process/cccessing the data of the models at a central place to draw conclusions/combine the models

I thought about letting the application run in two different threads, e.g. one to build the hourly and one to build the daily model.However, I don't want to share my variables between the threads. E.g. Currently my code stores the incoming data into a list, which is then further processed. So when the input is hourly and daily data, I don't want it to be mixed (otherwise I wouldn't get two separate models), but rather be treated separately (without duplicating my code or huge refactoring like making the code work for two instead of one input type). Basically I want my code to be scalable.

How to manage multiple views in a single page with JavaScript?

I am developing a single page application and trying to grasp MVC, MV* structure with vanilla JavaScript. At the moment I have a page that includes a lot of functionality and events. There are some parts of the page that needs to be dynamically rendered separately. Those parts have to interact with each other and a single Model. Creating a single View class for this page seems like it would make it very heavy.

enter image description here

My question is how do I best handle this kind of structure so that the code would be clean, clear and easy to test? Should I create a View for each Part in a page? Should I have those page parts interacting with each other or should they interact through a single controller, presenter? Or should they be working through the single View as some sort of partials?

Please provide some code examples in JavaScript if possible. Thank you..

Inheritace common test steps from base class

    When writing test cases for end-to-end test scenarios using java, selenium, java; we can keep common steps into the base class method and specific add, edit steps in the specific class.

    public abstract class XXXXBaseTest extends SeleniumTest { 

    @Test
    public void validateCalendarUi() throws IOException {
            **ExpCalendar expCalendar = openExpCalendar();**

            String calenderAvailable = expCalendar.getHeaderViewText();
            Assert.assertEquals(calenderAvailable, "Calendar View", "Failed : Calendar is not available");
    }
    }

Then, opened calendar() method is overridden in each specific class with specific steps.

public class XXXXXViewExpirationCalendarTest extends RefDataExpirationCalendarTest {

@Override
protected ExpCalendar openExpCalendar() {
        //Here write specific methods
}

}

Is this appropriate approach for test scripting?.Can we use inheritance concept to write test cases in this way ?

Stateful Strategy with Layout Mangager

I try to understand the difference between stateless and stateful.

As an example i take the Java LayoutManager. Normally i cannot use an instance for example of the BorderLayout for more than one container. I think in a stateful strategy, the Context pass itself as an argument to Strategy operation. So that the strategy can reach all data which is needed for the strategy algorithm.

I have a Code Snippet of a stateful strategy. I think here the context is "creation of the panel" for which we have different strategies.

public class LayoutComparer extends JFrame {
   private LayoutManager layout;
   private String title;

   public static void main(String[] args) {
       JFrame f = new LayoutComparer();
       f.setDefaultCloseOperation(EXIT_ON_CLOSE);
       f.pack();
       f.setVisible(true);
   }

   static int counter = 0;

   JPanel createPanel(LayoutManager layout, String title) {
      this.layout = layout;
      this.title = title;
      JPanel p = new JPanel();
      p.setLayout(layout);
      p.add(new JButton("Click " + counter++), "West");
      p.add(new JButton("Click " + counter++), "Center");
      p.add(new JButton("Click " + counter++), "East");
      p.setBorder(BorderFactory.createTitledBorder(title));
      return p;
  }

  LayoutComparer() {
      setTitle("Layout Manager Test");
      setLayout(new GridLayout(1, 2));
      LayoutManager m;
    m = new java.awt.FlowLayout();
 // m = new java.awt.BorderLayout();

    add(createPanel(m, "Left"));
//  pack();
    add(createPanel(m, "Right"));
  }
}

How to return message-completion results from a consumer back to a producer

We are building an application with a microservice architecture.

The microservice architecture will follow a message-oriented pattern, with AWS SQS.

We would like to return completion results from the consumer service back to the producer service.

This is the algorithm we are considering:

  1. Producer creates a message with a unique id
  2. Producer subscribes to a Redis channel that is named with the message id
  3. Producer places the message onto the queue
  4. Consumer removes the message from the queue and performs an operation
  5. Consumer publishes the results of the operation to the Redis channel that is named with the message id
  6. Producer recieves the completion results and resumes execution

Is this a reasonable way to pass message-completion results from a consumer back to a producer?