mercredi 1 décembre 2021

PHP design pattern for activities

Let's say I have multiple activities with same events and these events having different parameters in each activity. The question is "Is there any design pattern that particularly cover this matter?"

If I use interfaces, then I have to have same parameters in event functions. What I need is just insuring of having all event functions with any parameters in each activity.

Example:

class FirstCampaign{
    public function onFirstPurchase($arg1,$arg2, User $user){
        //
    }
    public function onFirstTrade($price,$something, User $user,Model $model){
        //
    }
}

class SecondCampaign{
    public function onFirstPurchase(User $user){
        //
    }
    public function onFirstTrade(Model $model){
        //
    }
}

class ThirdCampaign{
    public function onFirstPurchase(User $user, Model $model, int $abc){
        //
    }
    public function onFirstTrade(){
        //
    }
}

PS. I have tried onFirstPurchase(...$arguments) but it's not exactly what I need and it's just too complicated to work with.

PS. I call the events in different part of my application and they are not running under same circumstances.

What type of architecture would be Spring Boot + Flutter

I have a mental doubt for quite some time, I am looking for diagrams my applications, but I find myself in a dilemma about Spring Boot, for example, I usually work like this with Spring boot.

Spring boot image structure

And with flutter like this.

Flutterimage structure

My doubt is what kind of architecture am I applying? I am too confused if it is a service oriented (SOA) , client - server or N - Layers.

I thought it was SOA, because in my projects I usually have a controller for certain entities, example. ClientControler. InvoiceControler. ProductControler. And each one is a service, customer service, invoice service and product service, and my app the consumer of the services.

Refactoring existing code with some design pattern

I have the following code, like a git command line system with various supported parameters and commands. But as can be seen, it involves a lot of if-else conditions which
It doesn't seem that clean and is error-prone. Could there be a way to restructure it using some design pattern

public static void main(String... args) {
            String firstParam;
            try {
                firstParam = args[0];
            } catch (ArrayIndexOutOfBoundsException e) {
                System.out.println("Please enter a command.");
                return;
            }
            String secondParam = null; String thirdParam = null; String fourthParam = null;
            if (args.length > 1) {
                secondParam = args[1];
            }
            if (args.length > 2) {
                thirdParam = args[2];
            }
            if (args.length > 3) {
                fourthParam = args[3];
            }
            git git = new git();
            if (firstParam.equals("init")) {
                git.init();
            } else if (!git.getWorkingdirectory().exists()) {
                System.out.println("not initialized");
            } else if (firstParam.equals("status")) {
                git.status();
            } else if (firstParam.equals("log")) {
                git.log();
            } else if (firstParam.equals("global-log")) {
                git.globalLog();
            }
            else if (secondParam == null) {
                System.out.println("Incorreerands.");
            } else if (firstParam.equals("add")) {
                git.add(secondParam);
            } else if (firstParam.equals("rm")) {

Reasoning:
The first thing that comes to my mind is making a class out of each command. That's probably called command pattern. So there will be a class corresponding to - add, rm, status, log, etc.. all the supported arguments to the git command. I am planning to include around 13 such git commands to be specified as a command-line option. That will be like 13 classes.
Not sure if that is a good approach, or could there be any other approach?

DDD Questions about creation and upgrade of aggregate root with child entitities and its persistence

In the application I am working on, the user can start an Order without Order Lines, but the order cannot be persisted without order lines and other properties where other aggregates are referenced (which in the database are foreign keys which do not accept null values, this is why). This order is kept in cache until the user confirms it, that is the moment to persist it.

I think creating an aggregate root via its constructor leaving it in an inconsistent state regarding its persistence should not be possible. Is there something wrong with my thinking? because if this is possible, how do I handle the rules for its persistence? In the application service? In your repository? via a function (maybe "IsValidToPersist") in the aggregate root?

I see sense in the fact that the Order can be created without all the properties it needs for its persistence, in this case. I usually check for these needs in the aggregate root's constructors, throwing exceptions if the aggregate root is not ready to persist, among other business rules. I guess if this can be so, then there must be application services like "CreateOrderService", "AddOrderLineService", and "SaveOrderService" / "AddOrderService" where it would persist. And not just an "AddOrderService" service where it is created and persisted at the same time.

To update an Order Line, should I create an "UpdateOrderLineService" service that will find the Order, update its order line through its functions? Or should it be all that it takes to update an Oder in a single service "UpdateOrderService"?. I think this second option complicates some things. For example, it is difficult for the service to know which order lines to add, update and delete. Although that could be solved with a function in Order like "ChangeOrderLines" and simply replace them. In the first option, should the Order be persisted with each update (or even addition or deletion) of order lines? I can think of other options such as: return the updated Order or return the updated (or added) Order Lines (although this is not such a good idea).

My question, really is: is it convenient that exists services to handle the child entities of the aggregate root?

How do I have a method return one of almost the same classes case to case?

I am integrating a new payment way into our service project. I coded everything. Now I want it to be convenient to Solid principles. However, I am stuck how to overcome a case. Our service already have a method that called GetPaymentProfile and returns a class named PaymentProfile. After Integration I had to add a new PaymentProfile and method, because this new class has a new property.

Because of this, I obtained two class and two methods. Every step coded twice needlessly. I just want you to advise how I can do to overcome this.

preexisting method a class:

public class PaymentProfile
{
    public String Property1 = "";
    public String Property2 = "";
    public String Property3 = "";
    public String Property4 = "";
}

public PaymentProfile GetPaymentProfile(long aTicketType) {..}

class and method obtained after integration

public class PayCorePaymentProfile
{
    public String Property1 = "";
    public String Property2 = "";
    public String Property3 = "";
    public String Property4 = "";
    public String Property5 = "";
}

enter code here public PayCorePaymentProfile GetPayCorePaymentProfile(long aTicketType) {..}

What I thought

Creating a base class and then binding all sub-class with this base class, but I think it is not suitable for this case. Because I would return this base class without new property.

If I put everything in this base class, the old model has a new property needlessly.

*sorry for grammar rules and mistakes

Python -- extracting specific substring motifs/patterns from a string [closed]

I want to extract these specific substrings from a string. These are a few couple examples below, but ultimately I have many input strings that are similar. I have tried a substring "frameshift" method but the issue is that I get many undesirable output strings ex. ...(((((.....)))))... --> (((((.....))))), ((((.....)))), (((.....))), ((.....)), and (.....) (this is not desirable). As you can see in the patterns below, I need an equal number of open/close parenthesis and no trailing periods in the substrings.

Example 1

Input String: ..(((...(((((.(((.....((.................))..))).))))).)))..

Desired Extracted Output Substring(s): (((...(((((.(((.....((.................))..))).))))).)))

Example 2

Input String: (((((.((.((..............))..)).)))))(((((.....)))))........

Desired Extracted Output Substring(s): (((((.((.((..............))..)).)))))(((((.....))))) AND (((((.((.((..............))..)).))))) AND (((((.....)))))

Example 3

Input String: ........(((((.......................((((((...))))))...))))).

Desired Extracted Output Substring(s): (((((.......................((((((...))))))...)))))

Angular design for multiple forms

Very new to Angular and trying to get my head around an application already in development - we have 6-7 categories each with list/add/edit screens, which looking at the code are using a single list and edit component and programmatically creating each category's list/add/edit screen based on data from the API layer.

I'm finding this really difficult to work out what is doing what so my question is whether this is a correct use of Angular (could very easily be my inexperience) or whether you'd expect forms to be in the HTML/have a screen per function ie. category 1 list, category 2 list etc (each category has different fields)

Thanks :)