jeudi 3 octobre 2019

Spring: How to decorate some incoming requests with extra logic

I don't know how to ask this without getting downvoted 10 times, and being clear but here it goes. I have a rest controller class with many endpoints doing certain operations on books depending on action.

My question is a design question, how do I best get the all action specific business logic out from the abstract controller class(handleRequest method)? One of options I considered is GOF Template pattern which can fit here nicely. Problem with that is I dont want to create a different implementation class for each action type. Maybe %80 of the incoming action types have a generic flow. I only want to create an exception for certain action types and add some extra logic to them. Another option is Intercepting Filter pattern, still don't fit well to my situation.

mainController extends AbstractController{
   ....

  @PostMapping(value = "/getBooks/")
   public getBooks( @PathVariable ActionType action){
       handleGenericBookRequests(action)
  }

  ....

}

abstract class AbstractController{

 handleRequest(action){
  //do mostly generic stuff
  //do generic stuff
 if (action1){
  //do some action1 specific stuff  using action 1 related services
 }
 if(action2){
  //do some action2 specific stuff  using action 2 related services
 }

 //continue
 ..
 }
}

What is the best design pattern in iOS swift with firebase project (Firestore and realtime database)?

I integrated firebase into my iOS swift project. I used Firestore as a database. but I'm not sure what is a good design pattern for implementation this project.

Own CRM to get used by other application

We built our own CRM for our web application which has several other functions. Now we got the request from the management that also another web application should use our CRM. Our current web application is a monolithic one, which means that everything is in one big web application developed with Grails.

The first idea from one DEV was to create a REST API so that they can use our CRM functionality and customers are created in our application/DB and will always get synced back to their application.

I think this is not the best idea and much better would be to screw out our CRM from the monolithic web application and make a stand-alone version of it (microservice). This stand-alone version should then be configurable for things like DB, file storage, entities, workflows, ...

What do you guys think about it and maybe does one has another good idea for this specific use case?

mercredi 2 octobre 2019

override value of instance variable based on user string

I am building a java based web app with a class (being created per request) that does some computation based on configured member variables. I want to make it possible for the user to experiment by changing the value of any of these instance variables by sending it as a GET param.

For eg:

class Multiply {
    public int factor = 5;

    public doMul(int n) {
        return n * factor;
    }
}

Suppose this is my class. I create an object of this class for every request and call the doMul function with the use supplied value of n. I want to build a generic framework where the user can send a GET param to override the value of this factor for a particular request (example format being "?n=10&factor=2").

What would be a good design for this use case? On option i can think of is to read values of pre-defined params like 'factor' and then use if-else to set the instance variable. Is there something more generic to be used here?

which design pattern to use to get the wanted functionality

I want to make a pure virtual class that other classes inherit from it and each class implements the same function but with different implementation: let say I implement class Server

class Server
{
   Server();
   ~Server()=0;
   virtual void send(const std::string& string);
   virtual std::string resive();
};

class TcpServer:public Server
{
   TcpServer();
   ~TcpServer();
   void send(const std::string& string)
   {...}
   std::string resive(){...}
};

class UdpServer : public Server
{
    UdpServer ();
    ~UdpServer ();
    void send(const std::string& string)
    {...}
    std::string resive(){...}
};



main()
{
    Server* server = new TcpServer()/UdpServer;
}

How does this Design Pattern call? because I don't understand if it Adapter or Composite or Facade and if you have a different idea of how to implement this behavior.

Architecture - Should an object implement its behavior or we (other class) should control the object's behavior?

Dear Development Masters and Software Ninjas!

Recently, I started to feel the insuperable desire to make the architecture of my code better, therefore, I started to read books and articles about it. It turned out that it's an infinitely complex and large topic with an infinite number of approaches and architectural patterns to different types of problems, so I even feel a little lost now. Today I'd like to ask for at least some sharing of your understanding and experience.

The question is should an object implements its own behavior (I mean inside its class) or we should state only the data in the object's class and process the required behavior in a separate class (like in the ECS pattern)?

Let me give an example to avoid misunderstanding. Imagine we have some objects on the screen (no matter which ones — game objects, UI elements, etc). When the mouse cursor is over an object it does some action (a game object can be rendered with the outline, a UI element can be highlighted, etc). The question is should an object itself process the required behavior (a game object adds/removes the outline itself, a UI element adds/removes highlighting itself) or we should have some other class (like System in ECS) which, for example, serves as the event manager which controls the mouse events and processes these events (e.g. mouse cursor entered the area over object 1 -> do something, mouse cursor left the area over object 1 -> do something, etc)

The problem of my understanding is both of these approaches (and an infinite number of others) are applicable and have their own pros and cons. However, how to peak the right choice and what is better — I cannot understand. For example, in the former approach in the example above, everything is in one place and the class itself responsible for its behavior. In the latter example, the object holds only data and doesn't depend on the implementation of particular actions? which means that we can change the processing logic without the need to change the object.

I will be infinitely thankful for any advice. And thank you for your time.

Regards, Ivan.

P.S. ECS pattern here serves only as an example since it demonstrates the differences among approaches quite well.

Constructor not in Scope / Pattern matching parse error

I have the following datatype:

data Tree a = Node [Tree a]

and I want to count how many nodes are in such a tree so I defined the following:

count:: Tree a -> Integer
count [] = 0
count Node label [childrenTree a] = 1 + count a

which gives me an error saying "Parse error in pattern: true". If I change childrenTree a to Tree a it says that the data constructor is not in scope.

How do I solve this?