jeudi 23 août 2018

How to avoid Method Overloading in Java to avoid duplicates?

I have 2 method which performs 80% same work but differ in result processing. I am doing :

 private <T> T getResponse(final RestURI query, final Class<T> responseClass) throws IOException {
        T response = null;

        final RestResponse<Record> tempResponse = getResponseFromDataPath(query);

        if (isResponseOK(tempResponse, query)) {
            final CustomReader reader = createCustomReaderFromResponse(tempResponse);
            response = objectMapper.readValue(reader, responseClass);
        }

        return response;
    }

    private <T> T getResponse(final RestURI query, final TypeReference valueTypeRef) throws IOException {
        T response = null;

        final RestResponse<Record> tempResponse = getResponseFromDataPath(query);

        if (isResponseOK(tempResponse, query)) {
            final CustomReader reader = createCustomReaderFromResponse(tempResponse);
            response = objectMapper.readValue(reader, valueTypeRef);
        }

        return response;
    }

This looks like lot of duplicate. How do I make this such that I reduce the duplicates.

Much appreciate the advice!

Thanks

mercredi 22 août 2018

How to properly extend Clean Architecture entities?

I am trying to use Clean Architecture on my Android app project. I was still trying to grasp all the concepts but I think I got most of the basics down. However there are still specifics that I can't wrap my head around.

For instance, say I have a WeatherData entity on my domain layer. This entity has the following fields:

  • temperature
  • location
  • humidity
  • pressure
  • timezone

However, let's assume I decided to change my weather API and in the new API, I got additional fields such as:

  • precipitation probability
  • cloud cover
  • uv index
  • ozone

The latter fields are not available in my previous API but I would like to display them on my app as an additional feature. So the obvious action is that I should update my domain entity to include those additional fields. However, based on what I understood on the principles of Clean Architecture, this completely violates the dependency rule in which the domain object should not in any way be affected by the outer layers such as the data layer in where my API resides.

So is there a way where I can extend the features of my domain object while still making it framework and API agnostic?

Best way to design: A Thread Class listening for messages and modifyng attributes of a main class

Example:

class Car {

    Car[] otherCars;
    MsgHandler listeningThread;

}
class MsgHandler{
    Thread t;
}

The idea its that the MsgHandler when receiving a message, if its matches certain criteria, it has to update the otherCars attribute of the Car object who belongs to. Im considering many alternatives to design this, but im not sure which its cleaner or more elegant:

  • Observer Pattern
  • Simply passing the attribute to modify as reference
  • Put all the logic on the main class

Im looking for something that can scale to a more complex logic and more attributes involved.

Accessing the remote class method to resolve cyclic dependency

cyclic Dependency I am working with multimodule maven project, where I am facing the cyclic dependency between the driver(module A) and actors(module B)

Actors module has a common creation of actor system which should be common for all the actors across the project

driver uses ActorSystem to create an ActorRef for some client-side API calls hence there is a dependency of Module B on Module A. Also Actor A calls the method of driver class, which needs the dependency of Module A on Module B.

So it turns out to be cyclic dependency where Module A needs Module B and vice versa

I was looking for the solution where I could eliminate the dependency of Module A on B, as I want the Module B should not be dependent on any module. If there can be any way by which I could access the method in the driver class indirectly that could help in eliminating the cause of cyclic dependency.

Structure/Design Patterns for Java parsing program with multiple input combinations based on 2 parameters but one single possible output type

I am writing a tool which takes a file as an input. The input file may be one of these types: "xml", "xlsx", "csv". The file contents will be records of either type "A", "B', or "C". Regardless of input file (xml, xlsx, csv) type or type of record (A, B, C), the program will ultimately produce the same type of output file. Basically no matter what options combination, the resulting file will be a CSV with a specific structure.

I currently have an Interface with one single method "parse(File file)". Then I have 3 classes that implement such interface. Each of these 3 classes are "parser" classes that represent each of the possible file types (xml, xlsx, csv) since they each require a different file reader.

I have a simple Factory design pattern that returns the appropriate Parser class based on the file's extension. Where I am having trouble deciding, is how to deal with the type of record (A,B, or C) after the type of file was dealt with. What is the best way of organizing my classes so that I can have the cleanest structure and where I can reuse the most code possible.

Any ideas would be greatly appreciated. Thanks.

How would you go about designing a structured log pattern for systems?

My web app generate log entries on hooks such as model.save. Right now, I have a class Entry (representing a log entry) with a string field content to contain the text of the log entry.

Examples of such entries:

  1. Entry(content='Account 123-456-7890 spending changed to $500/day')
  2. Entry(content='Account 123-456-7890 paused spending')

The main problem with this approach is that it is difficult to derive semantic value from these Entry objects if content is just a string field.

For example, if I want to find out how often the spending of an account is modified on average across my team, I will have to write some custom logic to find all the entries with the text 'spending changed to' and analyze them. This is error prone because text can contain unpredictable elements such as capitalization or white spaces.

Since most of the log entries are generated programmatically, it occurred to me that I can use an enum to represent the content of the Entry class.

This means the example above can be transformed into the following:

  1. Entry(content_id=1, content_args="{ account_id: 123-456-7890, value: '500' }")
  2. Entry(content_id=2, content_args="{ account_id: 123-456-7890 }")

And the Content Enum:

class Content(Enum):  
  1: 'Account  spending changed to $/day' 
  2: 'Account  paused spending'

With structured entries, performing custom analysis on this data becomes very easy.

I am still at an extremely early stage at designing this refactor. I would not be surprised if there are existing implemention of this pattern somewhere.

Rather than re-inventing the wheel, I would like to see how others do it. Are there existing plugins/tools out there that are already implementing this pattern?

Asp core architecture

I am planning to develop an application using asp core web api2 (using entity framework).In future I should be able to consume api in mobile app also.so can anyone suggest me best design pattern or best way to set up architecture and to improve the performance, maintainability?It would be great if you provide any references. Thanks in advance.