vendredi 19 novembre 2021

What is the state design pattern?

Can someone please explain to me what is the state design pattern? And How to use it using a real world example? What is meant by "state" in here?

I have experiences in drawing state transition diagram.Is the same state we talking about in this state design pattern or something else?

Using a classmethod to wrap class functions, but not as an alternate constructor

I've gotten in the habit of using classmethods to wrap class functions rather than as an alternate constructor. It seems great to keep all relevant functions within the class namespace rather than defining a wrapper function in the general namespace. I've been told this is unpythonic and I haven't seen this pattern elsewhere. Why is pattern B preferable to pattern A?

Trivial examples:

Pattern A (my pattern):

class Foo():
  def __init__(self, bar):
    self.bar = bar

  def baz(self):
    print(self.bar)

  @classmethod
  def foobaz(cls, bar):
    foo = cls(bar)
    foo.baz()

Pattern B (a normal pattern):

class Foo():
  def __init__(self, bar):
    self.bar = bar

  def baz(self):
    print(self.bar)

def foobaz(bar):
    foo = Foo(bar)
    foo.baz()

What design patter or OOP design for this use case (scanning folder and conditionaly overwrite files)

I need to conceptually design a system that

  1. to scan directories looking for specific files (xml files that has a specific name)
  2. to parse these files and look for some nodes/attributes, output these values in a csv file
  3. ot overwrite some of this file that match with some criteria

how to design/structure this use case accordingly the OOP best practices or some suitable design pattern?

Difference between web application architecture and design pattern?

I need to develop a photo album web app in Django, stored on cloud services, with user login authentication, metadata and database. It must be developed according to a specific software design pattern that is different to the architectural pattern chosen. Please explain the difference between design and architectural patterns. Any industry acceptable suggestions on these patterns, suitable for a small project as explained above, would be very helpful.

Design pattern for checking multiple conditions

I'm trying to figure out if there is a design pattern for the below scenario.

I have two types of objects say List<JobApplicant> jobApplicants and List<Job> jobs, I wanted to select all jobs for which a JobApplicant is eligible, eligibility criteria can be multiple like JobApplicant and job skills are matching, experience, location, etc. it can be multiple.

After running all kinds of eligibility criteria I can get all job lists for which a JobApplicant is eligible. I tried implementing this with the simple eligibility condition, but the condition will keep increasing when there is new eligibility comes.

Are there any design patterns that can describe such a system and/or help on this to make it generic so that we can keep adding more eligibility criteria to filter desired jobs for an applicant.

How to aggregate data from SQL/MongoDb periodically?

There is a SQL table contains Bids.

When first bids is inserted to table the downcounter starts. After some time, as instance 5 minutes I must aggregate all data and find the max price across bids.

I wonder how to trigger this event and send message to the Node service that should handle this? Another directions when service asks each second DB and compares startDate, endDate and makes aggregate by sum.

Which is approach to choose?

How to inject a IEnumerable

I don't no which pattern this is, but I want to accomplish to add an IEnumerable of multiple classes which uses DI also. In my example I want to inject the IEnumerable in the BaseRepository, filled with the Custom2Repository and Custom2Repository classes which have this ICustomRepository interface and uses also injection. Can anybody help me in the right direction?

 public class BaseRepository : IBaseRepository //This is the main class
 {
        private readonly IProvider _provider;
        private readonly DbContext _dbContext;
        private readonly IEnumerable<ICustomRepository> _customRepositories;
        private string _databaseName;

        public BaseRepository(IProvider provider, DbContext dbContext, **IEnumerable<ICustomRepository> customRepositories**)
        {
            _provider = provider;
            _dbContext = dbContext;
            _customRepositories = customRepositories;
        }

        public async Task ChangeAsync(string id, CancellationToken cancellationToken)
        {
            var tracker = _provider.Get();
            var change = tracker.GetChange(id);
            foreach (var repo in **_customRepositories**)
                if (repo.EntityName == change.EntityName)
                {
                    _databaseName = repo.DatabaseName;
                    repo.Method1(id, cancellationToken)
                }
            await tracker.SaveChangesAsync(_databaseName, cancellationToken);
        }
...
 }

public class Custom1Repository : ICustomRepository //This I want to inject the base IEnumerable in the baseRepository
{
        public Custom1Repository(IProvider provider, ITracker tracker, DbContext dbContext)
        {
            _provider = provider;
            _tracker = tracker;
            _dbContext = dbContext;
        }

        public string EntityName => "EntityOne";
        public string DatabaseName { get; private set; }
        public async Task Method1Async(string id, CancellationToken cancellationToken)
...
}
public class Custom2Repository : ICustomRepository //This I want to inject the base IEnumerable in the baseRepository too
{
        public Custom2Repository(IProvider provider, ITracker tracker, DbContext dbContext)
        {
            _provider = provider;
            _tracker = tracker;
            _dbContext = dbContext;
        }

        public string EntityName => "EntityTwo";
        public string DatabaseName { get; private set; }
        public async Task Method1Async(string id, CancellationToken cancellationToken)
...
}