lundi 2 janvier 2023

What Design Pattern is suitable for Solving Dependencies at Deeper Level

In one of the project I am working on, I am facing a problem in terms of creating an object with a dependency that sits at a deeper level in the class composition. Following diagram shows the class composition.

Class Diagram

The problem I am facing is as follows:

  1. The IFrameGrid object contains 'IBracings' object at a deeper level in the class composition.
  2. Creation of the object 'IBracings' requires 'INodeEnvelope' which is a struct consisting of 4 Node (a geometric Point) objects.
  3. IFrameGrid requires the IBracings to be instantiated as soon as the UI is displayed to the user so that the user can change IBracings type based on the need. However, for IBracings to be instantiated the IFrameGrid should have been instantiated already since the INodeEnvelope is dependent on the root level user input.

UI looks something like this:

Typical UI

I have a feeling that, this is a design problem and I could not figure out as to what changes should be made to resolve this issue and honestly I am not a software developer.

I hope the description and the images above gives a fair understanding to convey my issue.

Can someone help me in identifying and resolving this and is much appreciated if it is elaborated instead of using some specific software terms which are known to the people working in a professional development team(s).

I tried to refactor this by introducing some AbstractFactories but no success as the ConcreteFactory implementation for creating the IBracings has a dependency on INodeEnvelope which can be acquired only after IFrameGrid object is available which itself has an indirect dependency on IBracings.

I also tried to find a solution in the web. Unfortunately I could not find anything relevant.

Looking forward for:

  1. If AbstractFactory is the solution for this kind of problem, can anyone explain may be by an example or by a diagram similar to the one attached herewith?
  2. If solution is by some other means, what would that be? An elaborated example can save a lot of brain storming.

PS: The title may not be apt for the question I am posting and I am not sure about what other title could be apt.

Is passing context class attributes as parameters to a strategy method (when the strategy is itself a part of the context) correct?

I am implementing the Snakes and Ladders game. Where Game is one class which has Board, Dice, Players and 2 strategies - Winning Strategy and EndGameStrategy. Different implementations of EndGameStrategy can be -

  1. If one player wins, the game ends - OnePlayerWonGameEndStrategy
  2. If only 1 player remains at last, the game ends - OnlyOnePlayerLeftGameEndStrategy

The Game model looks as below:

@Builder
@Getter
public class Game {
    private Board board;
    private Queue<Player> players;
    private List<Dice> dices;
    private final int MAX_BOARD_SIZE = 1000;
    private final int MAX_NUMBER_OF_PLAYERS = 10;
    private final int MAX_NUMBER_OF_DICE = 5;
    private int totalPlayers;
    private GameEndStrategy gameEndStrategy;
    private WinningStrategy winningStrategy;
    private Queue<Player>leaderBoard;
}

The Client initializes strategies using builder.

Game game = Game.Builder()
                .setBoard(board)
                .setPlayers(playerList)
                .setDices(diceList)
                .setGameEndStrategy(new OnlyOnePlayerLeftGameEndStrategy())
                .build();

One of the implementation of GameEndStrategy is OnlyOneplayerLeftGameEndStrategy

public class OnlyOnePlayerLeftGameEndStrategy implements GameEndStrategy{
    @Override
    public Boolean isGameEnd(int activePlayers, int totalPlayers) {
        return activePlayers == 1;
    }
}

Other implementation is when the first player wins, the game ends

public class OnePlayerWonGameEndStrategy implements GameEndStrategy{
    @Override
    public Boolean isGameEnd(int activePlayers, int totalPlayers) {
        return totalPlayers - activePlayers == 1;
    }
}

The client uses the strategy as follows:

while(!game.getGameEndStrategy().isGameEnd(game.getPlayers().size(), game.getTotalPlayers())) {
    \\Game logic (Player throws dice, new position is checked
}

Here as parameters of isGameEnd method, should I pass the context class i.e the Game instance as the param? because, OnlyOnePlayerLeftGameEndStrategy would only need the activePlayers number to check if one one player is left. But, OnePlayerWonGameEndStartegy needs both totalPlayers and activePlayers as params to check if one player has won. Also there could be another strategy in future which would need more params. So how should we handle changing params? How should we implement the strategy pattern here in this Snakes and Ladders usecase?

Design pattern for creating a class with multiple methods which should be executed in an order

I am trying to create a template class for testing Room migrations. The steps which should be followed for testing a Room migration are as follows.

  1. Create a database in the previous version.
  2. user migration helper to migrate to the next version.
  3. insert some data.
  4. verify the integrity of that data.

Now I have created a class MigrationHelper which implements all this methods accordingly.

class MigrationHelper {

 fun createDatabase(version : Int) : MigrationHelper{
       initialDatabase = migrationTestHelper.createDatabase(TEST_DB_NAME , version)
       return this
 }

 fun insertData(db : SupportSQLiteDatabase){
   ... 
 }

}

Now for this class I am currently using the builder method so that the for writing tests developers can call methods in a clean and understandable way.

    @Test
    fun runMigration_78_79(){
        migrationHelper.setUp("test_db")
            .addTablesInvolved("packs")
            .createDatabaseWithData(78)
            .addMigrations(MIGRATION_77_78,MIGRATION_78_79)
            .runMigration(79)
            .cleanOut()

    }

But since builder pattern , any method can be called in any order . This is not good for my implementation , since some methods are neccessary to be called in an order . for eg : Can call the 'runMigration' method before the 'addMigration' method, Since for running the migration you need to add an array of all the migrations involved.

What would be the best design pattern for this kind of situation ? I understand I might be over complicating , I could just create class with a bunch of methods and call them individually . but I would like to have a clean approach. Thank you.

dimanche 1 janvier 2023

c program about hollow number pattern

Write a C program to print hollow number pattern series using for loop

Example

Input

5

OUTPUT :

123456

2 5

3 4

4 3

5 2

654321

#include<stdio.h>
int main()
{
int i,j,m,n,a;
scanf("%d",&a);
for(i=1;i<=a+1;a++)
{
    for (j=1;j<=a+1;j++)
    {
        for(m=1;m<=a+1;m++)
        {
            for(n=1;n<=a+1;n++)
            {
               printf("%d",n);
            }
          printf(" ");  
        }
    }printf("\n");
printf("\n");
}
}

How to get specific strings based on complex pattern with, regular expression

path<-c("C:/Users/SEYDOU GORO/Dropbox/PC (2)/Documents/BETCLIC/DONNEES/argentina-primera-division-matches-2022-to-2022-stats.csv",
"C:/Users/SEYDOU GORO/Dropbox/PC (2)/Documents/BETCLIC/DONNEES/argentina-primera-division-matches-2022-to-2022-stats.csv",
"C:/Users/SEYDOU GORO/Dropbox/PC (2)/Documents/BETCLIC/DONNEES/france-ligue-2-matches-2021-to-2022-stats.csv",
"C:/Users/SEYDOU GORO/Dropbox/PC (2)/Documents/BETCLIC/DONNEES/germany-2-bundesliga-matches-2021-to-2022-stats.csv")

mydata<-data.frame(path=path)

I want to create new variables out of variable path

Country the pattern is: DONNEES/country name -`

League the pattern is: DONNEES/ a word - League name -matches

Year the pattern is: - The year -to

This should the resulting dataset:

                                                                                                                     path   Country           League Year
1 C:/Users/SEYDOU GORO/Dropbox/PC (2)/Documents/BETCLIC/DONNEES/argentina-primera-division-matches-2022-to-2022-stats.csv argentina primera-division 2022
2 C:/Users/SEYDOU GORO/Dropbox/PC (2)/Documents/BETCLIC/DONNEES/argentina-primera-division-matches-2022-to-2022-stats.csv argentina primera-division  202
3             C:/Users/SEYDOU GORO/Dropbox/PC (2)/Documents/BETCLIC/DONNEES/france-ligue-2-matches-2021-to-2022-stats.csv    france          ligue-2 2021
4       C:/Users/SEYDOU GORO/Dropbox/PC (2)/Documents/BETCLIC/DONNEES/germany-2-bundesliga-matches-2021-to-2022-stats.csv   germany     2-bundesliga 2021

Properly handling CRUD for local and remote files in a drag and drop file upload component

I'm trying to build a CRUD interface for a drag and drop file upload component that allows users to manage remote files, and also upload new ones. However, I'm having difficulty because local and remote files are fundamentally different and I'm struggling to find a clean solution. For example acceptedFiles are meaningless for files that are already to remote.

enter image description here

Is this a common issue when building file upload components and is there a standard way to handle it?

For example, when I try to add a local file to the group, this is the result [Screenshot 2].

enter image description here

HOW Enum Singletons are SIngleton Instances?

How can ENUM singleton a true singleton?

1. `public ENUM SingletonInstance{
   INSTANCE;
   doStuff(){
      ___________
   }
}`
and we access it using SingletonInstance using SingletonInstance.INSTANCE.doStuff().
What about 

2. `public ENUM SingletonInstance{
   INSTANCE1, INSTANCE2;
   doStuff(){
      ___________
   }
}`

This voilates the single Pattern as we have 2 instances here.

When using class based singleton pattern,

3. `public class SingletonInstance{
   doStuff(){
        -----------
    }
   PSVM(){
        SingletonInstance si = new SingletonInstance();
        si.doStuff()
    }
}

`

4. `public class SingletonInstance{
    doStuff(){
     ------------
    }
    PSVM(){
       SingletonInstance s1 = new Singleton();
       s1.doStuff();
       SingletonInstance s2 = new Singleton();
       s2.doStuff();
    }
}`

Now from the above 4 points, 1 and 3, if 1 is considered as Singleton, than point 3 has to be singleton. if point 3 is not a singleton, point 1 also cannot be singleton.. Point 2 and 4 are obivously not singleton..


`private SingletonInstance getInstance(){
     if(instance == null){
         instance = new SingletonInstance()
     }
     return instance;
}`

prevents 2nd instance fron bring created. hence this can be seen as truest form of Singleton pattern. Irresoective of any number of times we call getInstance() it is going to return me singleInstance.

Similarly, Irrespective of number of Constants, there should be only 1 instance of ENUM to be called as singleton..correct?

what about ENUM? is there any new way of prevent 2nd constant for ENUM? if yes, than that can be converted to purest form of singleton. But not, ENUMs cannot be considered singleton.

What's your say on this? Please help me understand ENUMs and how and why are they singletons if they don't prevent 2nd instance to be created.. ?

Expecting to understand ENUMs and how are the ENUMs singleton's without any enforcement.