mercredi 2 décembre 2020

Shared table between microservices?

I work on a microservices architecture and I have a question about a sharing table between two micorservices

Example :

I have two microservices, one is for publish Ads and the other one is for searching Ads, both of these services have a seperate database

I want to know in which microservice's database i should put my table ADS in order to reduce dependency and calls between these two micorservices and what is the best way to update the table ADS in search_service database after adding a new Ads ? (If the Publish service is down, i want to be able to call the Search service)

Thanks for your advice

Java: 2 Decorators on 1 object with same method overrided

I'm working with decorator pattern in java and I encountered a problem. I have 2 decorators which override same method and I want them on the same object to do 2 implementations at once.

The first thing that comes to my mind is just combining two decorators to new one, but it is probably pretty bad idea in the long run.

What is the proper way of solving this issue? Do I need to go further more abstract or just reject decorators and use something else? (like strategy?)

Code example to visualize the problem:

public class Attacker {

//variables, constructor...


     public void attack(Object defender) {
       defender.hp -= 10;
    }

//other methods
}
public class HealAfterAttackDecorator extends Attacker {

//variables, consturctor...

     @Override
     public void attack(Object defender) {
        defender -= 10;
        this.heal();
     }
//other overrided methods
}
public class StunDefenderAfterAttackDecorator extends Attacker {
//variables, consturctor...

@Override
public void attack(Object defender) {
defender -= 10;
attack.stun(defender);

}
//other overrided methods
}

How to combine stun and healing into one?

Software design patterns for class library implementation in .net application [closed]

My requirement is, I want to create a class libraries for authentication, user management etc. and data needs to saved via class libraries to DB.

So that client applications can pass the data alone to my class libraries, all business logic will happen in libraries and data will gets saved in DB. so multiplatform applications can use my class library.

Please suggest what design pattern should I follow?

mardi 1 décembre 2020

Subclass attribute in Subclass of superclass attribute in Superclass

I have a problem creating two classes that share heavy amount of code, with an attribute of the subclass meant to be a subclass of the attribute in the superclass. Let me provide a dummy example of what the root of the problem is:

public class A {
}

public class B extends A{
}

public class X{
 A value;
}
public class Y extends X{
 B value;
}

The shared code makes use of either A or B, depending on the situation. Converting value to protected and ignoring the problem did not work, during runtime in debugging of operations of class B, while in a shared method, I could see that Y.value was filled but X.value was not, eventually raising a NullPointerException. To face that issue I thought about adding a generic type T to the design thus:

public class A {
}

public class B extends A{
}

public class X<T extends A>{
 T value;
}
public class Y extends X<B>{
}

which seems to work. Unfortunately however, there is a part of the code that tries to initialize the A or B, with a default constructor, which forces me to increase the complexity of the code with commands, like try catch handlders, that would otherwise not be needed. Another way of solving this situation was by introducing a parent class P:


public class A {


public class B extends A{
}

/**The abstract class has abstract the method that creates the T object
*/
public abstract class P<T extends A> {

}

public class X extends P<A>{
}
public class Y extends P<B>{
}

or creating an interface in the position, which would cause the requirement of the creation of a lot static methods that would need to be explicitly called in the implenting classes

Conclusively, I would like to ask whether there is a better solution than the ones I suggested, or which one of the above patterns seems the best to follow. FYI, I come from a Python background, where I had not faced such situations due to , well, duck typing.

The delegate pattern does not fire in the class where it is implemented

There is a TopMenuBar class. This is a menu bar (collectionView) for a tableViewCell in the main class (named - MainVC).

class TopMenuBar: UITableViewCell {
     public var delegate: TopMenuBarDelegate?
}

and protocol:

protocol TopMenuBarDelegate {
     func didTapTopMenuCell(named: String)
}

In the didSelectItemAt() method with delegate, I call this protocol:

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    
     ///Relay to delegate about menu item selection
    let selectedItem = menuItems[indexPath.row + 1].description
    delegate?.didTapTopMenuCell(named: selectedItem)
}

Further, in the main class - MainVC, I subscribed to the protocol - TopMenuBarDelegate, added a protocol method - didTapTopMenuCell(), created an instance of the TopMenuBar class and in the viewDidLoad method I assigned a delegate to this protocol - the MainVC class.

class MainVC: UIViewController, TopMenuBarDelegate {

   let topMenuBar = TopMenuBar()

   func didTapTopMenuCell(named: String) {
       print(named)
   }
   override func viewDidLoad() {
       super.viewDidLoad()
   topMenuBar.delegate = self
   }
}

To test the functionality, I only want to print the name of the cell that I clicked on. As a result, nothing appears in the console.

Using Repository Pattern for two tables without relation

How I can using Repository Pattern in C# to get data from two tables without relation between them in DB For example I have table for students and table for details look

    public class Student{
public int Id{get;set;}
public string Name{get;set;}
public int GenderId{get;set;}
}

    public class DetailsLookup{
public int Id{get;set;}
public string Name{get;set;}
}

I set data for GenderId from (DetailsLookup.Id) but I doesn't create relation between them in DB .

I'm using Repository Pattern in my code and using Unit of work I have idea for code by return StudentViewModel

    public class StudentViewModel{
public int Id{get;set;}
public string Name{get;set;}
public int GenderId{get;set;}
public int GenderName{get;set;}
}

and write code in Repository class to get data and return ViewModel

but I read in https://programmingwithmosh.com/net/common-mistakes-with-the-repository-pattern/ We can't return view models/DTOs in Repositories

Help me how I can do it

lundi 30 novembre 2020

Unable to call matrix matrix from main function in c++? [duplicate]

Don't know how to declare matrix using pointers and passing into the function and also not getting any useful resource from internet.

void wavePrint (int* arr[],int m)
{

    for (int i = 0; i < m; ++i){

        if(i%2==0) for(int j = 0; j < m; ++j)cout<<*(arr[j])<<" ";
        
        else for (int j = m-1; j >= 0; --j)cout<<*(arr[j])<<" ";
    }
}    

int main(int argc, char const *argv[])
{

    int* arr[5]={
        {1, 2, 3, 4, 5},
        {6, 7, 8, 9, 10},
        {11, 12, 13, 14, 15 },
        {16, 17, 18, 19, 20 },
        {21, 22, 23, 24, 25 }
    };

    wavePrint(*arr,5);
}