dimanche 11 décembre 2022

Creating a chat functionality using Slack API

I am having a problem understanding this problem as I have not had much - if any - interaction with Slack API. The problem is listed as follows:

We need to integrate a chat function allowing users to talk to trainers. Slack API is going to be used for this function.

Now, say - UI and Auth are both already implemented. The following is not implemented as part of the network layer yet :

1 - Send/read messages

2 - List/create groups and conversations (channels)

3 - Get a list of the users in the team

4 - Update current status from online, away, etc...

The question is:

How would you design the network layer for this service? Why did you choose such a design?

I am really not understanding the way this is worded and any assistance would be of much appreciation and cordiality here.

This is what my understanding is use Slack API pre-built-in functions:

1 - To get and post messages.

2 - To list and create new channels

3 - To get the list of the team users

4 - To update user status

The problem is this does not sound like a design as this latter would require a database and such.

Design patterns for chaining dependency objects

I have a design problem and I can't find the right way to deal with that. In my problem there are 4 classes:

  • class G
  • class T depending on the G
  • class F depending on T
  • class P depending on G, T and F

Additionally each concrete implementation of P should use exact implementation of T and when F is used then T is not needed. However F and T are different concepts (I would say F is some transformation or different interpretation of T) and it is hard to design common interface for them (otherwise I would think about decorator pattern). I was thinking about approach like below:

class PImpl extends P {
     G g;
     T t;
     F f;

     PImpl(G g) {
        this.g = g;
        this.t = new TImpl(g);
        this.f = new FImpl(t);
     }
}

But isn't it breaking the Dependency Inversion Principle from SOLID? Another idea which is an extenstion of above is additional introduction of initialization method for TImpl and FImpl. Then I would provide implementation classes in the constructor and then do exact construction in the constructor body.

class Test {
    main() {
       P p = new PImpl(new GImpl(), new FImpl());
    }
}

abstract class P {
    G g;
    T t;
    F f;

    P(G g, T t, F f) {
        this.g = g;
        this.t = t.create(g);
        this.f = f.create(t);
    }
}

class PImpl extends P {
     
     PImpl(G g, F f) {
        super(g, new TImpl(), f);
     }
}

But I'm not sure if this brings any additional value and only makes the code more complex.

Understanding the code in an Abstract factory pattern

I am trying to learn design patterns in C# and my friend has written me some code for an Abstract factory pattern (I think).

from what I am seeing the code creates a factory(Fa), this factory(Fa) then creates another factory(Fb) based on an Enum and then that factory(Fb) creates a concrete class that can be used to call an API etc.

I can create a factory(Fb) and it creates the class but when I call methods from the class that were created by the factory(fb), I do not see my methods and cant call them but can only call the class that it inherits.

What I am trying to do in a nutshell, is create a factory that creates Jane dolls (like it does) and this inherits everything from the doll class, it also has all its own properties, great, but why cant I access its own properties when I make a factory to create the Jane Factory, it only lets me use the inherited Doll methods this way, but if I created another factory to create Santa dolls it would have different methods I need to use.

**Web.Controllers **

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;


namespace Qqq.Dolls.Web.Controllers
{

    public class InventoryController : Controller
    {
        private readonly IDollFactory _dollFactory;
        private readonly IJaneDollFactory _janeDollFactory;
        private readonly IMapper _mapper;

        public InventoryController(IJaneDollFactory dollFactory, IMapper mapper, IDollFactory dollFactory1)
        {
            _janeDollFactory = dollFactory;
            _mapper = mapper;
            _dollFactory = dollFactory1;
        }

        public async Task<IActionResult> List()
        {
            var token = HttpContext.Session.GetObject<OAuthResponse>(SessionConstants.JaneToken);
            var doll = _JaneDollFactory.Create(token, JaneScopeConstants.GetAllScopes());


            var a = _DollFactory.Create(Doll.Jane, HttpContext);

            var ab = await a.LGetProductAsync("TestProduct");


            var inventory = await doll.GetInventory();

            var ret = inventory.InventoryItems.Select(
                inventoryItem => _mapper.Map<InventoryViewModel>(inventoryItem));

            return View(ret);

        }
    }

}

DollFactory

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Qqq.Dolls.Web.Infrastructure;

public class DollFactory : IDollFactory
{
    private readonly IJaneDollFactory _JaneDollFactory;

    public DollFactory(IJaneDollFactory JaneDollFactory)
    {
        _JaneDollFactory = JaneDollFactory;
    }

    public IDoll Create(Doll Doll, HttpContext httpContext)
    {
        switch (Doll)
        {
            case Doll.Jane:
                var token = httpContext.Session.GetObject<OAuthResponse>(SessionConstants.JaneToken);

                return _JaneDollFactory.Create(token, JaneScopeConstants.GetAllScopes());
            default:
                throw new NotImplementedException();
        }

    }
}

IJaneDollFactory Interface

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Qqq.Dolls.Jane;

public interface IJaneDollFactory
{
    IJaneDoll Create(OAuthResponse oAuthResponse, List<string> scopes, HttpMessageHandler httpMessageHandler = null);
}

**IJaneDoll interface **

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Qqq.Dolls.Jane;

public interface IJaneDoll : IDoll
{
    //Inventory
    Task<Inventory> GetInventory();
    Task ListInventoryItem(InventoryItem product);
    Task DeleteInventoryItem(string sku);

}

IDoll interface

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

public interface IDoll
{
    Task ListProductAsync(Product product);

    Task<Product> GetProductAsync(string productId);
}

interface IDollFactory

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Qqq.Dolls.Web.Infrastructure;

public interface IDollFactory
{
    IDoll Create(Doll doll, HttpContext httpContext);
}

JaneDollFactory

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;


namespace Qqq.Dolls.Jane;

public class JaneDollFactory : IJaneDollFactory
{
    private readonly IMapper _mapper;
    private readonly JaneApiConfiguration _JaneApiConfiguration;

    public JaneDollFactory(IOptions<JaneApiConfiguration> JaneApiConfiguration, IMapper mapper)
    {
        _mapper = mapper;
        _JaneApiConfiguration = JaneApiConfiguration.Value;
    }
    public IJaneDoll Create(OAuthResponse oAuthResponse, List<string> scopes, HttpMessageHandler httpMessageHandler = null)
    {
        return new JaneDoll(_mapper, _JaneApiConfiguration, oAuthResponse, scopes, httpMessageHandler);
    }

}

General code design: shared and alternating patterns

The situation
My question applies to all programming languages with classes and inheritance. Im sure the answer is out there somewhere already, but I could not find it as i don’t have the right terminology to use.

Lets take the classic example. We got Dogs and Cats which are both of the type Animal:

abstract class Animal {
}

class Dog extends Animal {
}

class Cat extends Animal {
}

Simple enough. Some behavior is shared in the Animal class, anything that differs between Cat and Dog is coded in their respective classes.

Now suppose we got 2 planets. Earth and Mars. Both planets have Cats and Dogs. But, behavior for all animals on Earth differs from all animals on Mars. They, for example, experience a difference in gravity which affects the way they move.

There is no difference between specific animal types between the planets. Thus, all differences between animals on Earth and Mars can be coded at the parent level, that of the Animal class.

Not only that, but some behavior is available for all Animal instances on Mars that does not exist on Earth.

Ideally, in code dealing with these animals, we deal with a MarsAnimal or EarthAnimal class that is implemented by either a Dog or Cat. The implementing code does not need to know if they are Dogs or Cats. It does already know on what planet the Animal lives though.

What I thought about already
One solution would be the following:

abstract class Animal {
}
abstract class Cat extends Animal {
}
abstract class Dog extends Animal {
}
interface MarsAnimal {
}
interface EarthAnimal {
}
class MarsCat extends Cat implements MarsAnimal {
}
class MarsDog extends Dog implements MarsAnimal {
}
class EarthCat extends Cat implements EarthAnimal {
}
class EarthDog extends Dog implements EarthAnimal {
}

Ofc, the obvious problem with this is, that any behavior specific to MarsAnimal would need to be implemented in both the MarsCat and MarsDog classes. That’s ugly code duplication and definitely not what I was looking for.

The only semi-acceptable method I could think of was the following:

abstract class Animal {

    private PlanetAnimal planetAnimal;

    public function myBehavior() {
        this.planetAnimal.myBehavior();
    }
}

class Cat extends Animal {
}

class Dog extends Animal {
}

interface PlanetAnimal {
    function myBehavior();
}

class MarsAnimal implements PlanetAnimal {
    public function myBehavior() {
        // marsAnimal- specific behavior here
    }
}

class EarthAnimal implements PlanetAnimal {
    public function myBehavior() {
        // earthAnimal- specific behavior here
    }
}

Thus, when creating a Cat or Dog instance, since we know what planet they are from at that point in the code, we give then the needed PlanetAnimal instance in their constructor (either MarsAnimal or EarthAnimal).

This is close. The only problem with this is, like I said, some behavior exists only for all animals on Mars. I’d have to still implement a method in both the Animal and PlanetAnimal classes that is used only for Mars. If this is the only solution then sure, but it feels like there should be some better method out there.

So, any ideas? I’d love to hear!

Where to put static fields used in builder pattern?

Using a builder pattern I want to have static fields used in all instances. Do I put them in the Url class or in the UrlBuilder class as static fields or do I extract them into a new class to only hold the static fields?

The expected result is the ability to create a Url instance by using the Builder class and a switch case logic in the UrlBuilder constructor.

Here is a code example:

public class Url {

  //required parameters
  private String homePage;
  private String boardSuffix;

  public String getHomePage() {
    return homePage;
  }

  public String getBoardSuffix() {
    return boardSuffix;
  }

  private Url(UrlBuilder builder) {
    this.homePage = builder.homePage;
    this.boardSuffix = builder.boardSuffix;
  }

  //Builder class
  public static class UrlBuilder {

    //required parameters
    private String homePage;
    private String boardSuffix;

    public UrlBuilder(String homePage, String boardSuffix) {
      this.homePage = homePage;
      this.boardSuffix = boardSuffix;
    }

    public Url build() {
      return new Url(this);
    }

  }

}

I've tried placing the static fields in a "Constants" enum, but got lost in the reflection of enums, while trying to prepare tests.

samedi 10 décembre 2022

how to handle server downtime for webhook requests in java

My application(A) gets file processing details from another java application(B) through rest api. App B sends the status with real-time processing details (Inprogess, Generated, Transfered). Application reads this info and displays it to the user. when the status is Transfered.. App A performs some task.

App A is completely dependent on App B for performing future task.

Looking for best practise to handle the request from App B during my app A downtime.

I found that message queue is best solution for this usecase. Unfortunately we cant have any new infrastructure for message queue.

Is there any other solution that we can implement in java without having any infra level changes. I appreciate your time for going through this query. Thankyou

vendredi 9 décembre 2022

What is best practice on handing off design with icons in containers? [closed]

Product designer here. In my DSM I use icons in containers for consistency and alignment (16x16, 20x20, 24x24) which is common. On handoff I haven't had an issue until recently a person in(dev) asked for the icons without containers.

What is the feeling here? You can see the reason and organization in example A. Is this an issue or is there a preference? This was for web but on mobile web that are adds a touch point.enter image description here

I handed of a design and expected the icon and container ot be implemented. It also help in terms of hotspot.