mercredi 3 mars 2021

Cache and update regularly complex data

Lets star with background. I have an api endpoint that I have to query every 15 minutes and that returns complex data. Unfortunately this endpoint does not provide information of what exactly changed. So it requires me to compare the data that I have in db and compare everything and than execute update, add or delete. This is pretty boring...

I came to and idea that I can simply remove all data from certain tables and build everything from scratch... But it I have to also return this cached data to my clients. So there might be a situation that the db will be empty during some request from my client because it will be "refreshing/rebulding". And that cant happen because I have to return something

So I cam to and idea to

  1. Lock the certain db tables so that the client will have to wait for the "refreshing the db"

or

  1. CQRS https://martinfowler.com/bliki/CQRS.html

Do you have any suggestions how to solve the problem?

What is important for a well-designed software library?

I am currently planning on converting a huge chunk of code at my work into a separate Python library since we use that code quite often as a starting point for new projects and it grew organically which makes it sometimes a bit awkward to use. What are in your opinion important things to plan out in order to make the library as usable as possible and extensible in the future?

Inheritance or composition in object oriented design

I have seen many class diagrams on popular websites and design courses defining the relationships using composition like: Admin has a person instance Umpire has a person instance

As per me, shouldn't it be the case of inheritance as admin 'is-a' person, umpire 'is-a' person. Admin extends Person Umpire extends Person

Can you please help me understand why we are preferring composition here?

C++ - How to reserve a certain object of a class to just be instantiated by a certain class

I have a non-conventional use case. I'm defining a unique_id_generator class, which is kind of, sort of a singleton i.e there is just one instance for a given type_id. There can be many different type_ids, but for a specific type_id, there is just one instance. Now I want to make sure that type_id = 0 goes to a very specific class. Basically just that specific class can use type_id = 0 and then the rest can be used freely. I'm wondering through which design pattern can I ensure that happens? I don't want to control or govern type_ids given in general.

I can't control who instantiates a unique_id_generator first. Also based on design, I don't want to route requests for unique ids through the specific class which gets type_id = 0.

Any thoughts/advice is greatly appreciated.

How to construct TypeScript types for abstract factory pattern

I'm trying to get my head around how I can type the private factories: Record<...>, which will contain key value pairs of a aKey: aFactoryInstance. I've tried Record<string, TemplateFactory>, which has 2 issues; 1. They key is not just any string, but a specific one, and 2. TemplateFactory is the abstract class, and what I have as values are instances of derived classes from that abstract factory one.

I found this SO thread about create a factory class in typescript, which also had my second issue which is the:

Element implicitly has an 'any' type because...

But the comments in there were not applicable here, since they didn't really implement the abstract factory pattern, I got the impression of.

abstract class TemplateFactory {}
class FirstTemplateFactory extends TemplateFactory {}
class SecondTemplateFactory extends TemplateFactory {}

const AvailableTemplate = Object.freeze({
  first: FirstTemplateFactory,
  second: SecondTemplateFactory,
});

class TemplateCreator {
  private factories: Record<string, unknown>; // 👈 How to type this record? It will be an object of: { aKey: aFactoryInstance }

  constructor() {
    this.factories = {};
    Object.keys(AvailableTemplate).forEach((key) => {
      this.factories[key] = new AvailableTemplate[key](); // 👈  "Element implicitly has an 'any' type because expression of type 'string' can't be used to index"
    });
  }
}

(C++) Input specific pattern accoring to user input

I have a line of code that when inputted 3, the result will print out a series of dashes and asterisks to form a diamond:

Expected Input:

3

Expected Output:

--*--
-***-
*****
-***-
--*--

what i have so far is the triangle but I can' seem to get rid of the middle line to make it a full diamond shape. Also "-" is not printing on the right side of the bottom half

this is the code I have made

int n;
cin >> n;
for (int left_stars = 0; left_stars < n; left_stars++) {
    for (int column = 0; column < 2 * n - 1; column++) {
        int first_star = n - 1 - left_stars;
        int last_star = n - 1 + left_stars;
        if (column < first_star || column > last_star) {
            cout << "-";
        } else {
            cout << "*";
        }
    }
    cout << endl;
}

for(int i = n; i >= 1; --i) {
    for(int space = 0; space < n-i; ++space) {
        cout << "-";
    }
    for(int j = i; j <= 2*i-1; ++j) {
        cout << "*";
    }
    for(int j = 0; j < i-1; ++j) {
        cout << "*";
    }
    cout << endl;
}
return 0;

mardi 2 mars 2021

Strategy Pattern implementation in JavaScript code error

I have the Calculation.js file, CalculationStrategy.js, and CalculationStrategyB.js.

I want to implement the Strategy Pattern to these files. However, when I used Jest to test the files. I have an error that this.stragtegy.doOperation is not a function.

I did follow the tutorial to implement the Strategy Pattern by using the Calculation file as a base but I don't know why this happens.

Calculation.js

class Calculation {
constructor(a, b, op) {
    this.a = a;
    this.b = b;
    this.op = op;
    this.stragtegy = null;
}

static Create(a, b, op){
    return new Calculation(a, b, op);
}

 GetResults() {
    return this.op(this.a,this.b)
}
addOperationToName(fn){
    return function(name){
        const operation = name + ' is an operation';
        return fn(operation);
    }
}

sayOperation(name){
    return name;
}

obtainStructor(){
    return this.a +' '+ this.b;
}

set strategy(stragtegy){
    this.stragtegy = stragtegy;
}

doOperation(){
    return this.stragtegy.doOperation();
}
}
module.exports = Calculation;

CalculationStrategy.js

class CalculationStrategy{
doAction(){
    return 'This is Calculation Strategy 1';
}
}
module.exports = CalculationStrategy;

CalculationStrategyB.js

class CalculationStrategyB{
doAction(){
    return 'This is Calculation Strategy 2';
}
}
module.exports = CalculationStrategyB;

CalculationStrategy.test.js

const calculation = require('../src/models/Calculation');
const calculationStrategy = require('../src/CalculationStrategy');
const calculationStrategyB = require('../src/CalculationStrategyB');
const Product = require('../src/Operations/Product');

test('Test Strategy of Calculation', () => {
//I need to test the get results function
let op = Product;
let customCalculation = new calculation(1,2, op);
const strategy = new calculationStrategy();
const strategyB = new calculationStrategyB();
customCalculation.stragtegy = strategy;
expect(customCalculation.doOperation()).toBe('This is Calculation Strategy 1');
customCalculation.stragtegy = strategyB;
expect(customCalculation.doOperation()).toBe('This is Calculation Strategy 2');
});