mardi 3 juillet 2018

Design pattern to filter data according to certain rules

I need to filter data on the basis of certain rules. For example, I have a list of records, and on the other hand I have a set of rules. Each rule will filter out this list. I am also maintaining a configuration file for these rules. These rules could be turn on/off through a configuration file.

For example, consider my configuration file..

[
  {
    "rule": "rule-1",
    "class": "org.company.RuleOneFilter.class",
    "isEnabled": true
  },
  {
    "rule": "rule-2",
    "class": "org.company.RuleTwoFilter.class",
    "isEnabled": false
  },
  {
    "rule": "rule-3",
    "class": "org.company.RuleThreeFilter.class",
    "isEnabled": true
  }
]

Using this file, rules would be plugged-in or unplugged easily. I have checked the specification of Chain Of Responsibility but still confused whether to go for it or not! Can someone suggest me an appropriate design pattern to implement this? Or if there's any framework to fulfill this need?

real world example of Abstract Factory Design Pattern in C#

I am writing this question after reading many posts on Abstract Factory Design Pattern in C#.I really cannot make a real world use case sense out of the examples that all those posts provide. All I could see was some basic example of cars/computers/phones etc.I understand that they are important to provide a simple explanation. But I am really unable to map it to anything real world, because if I would really follow such examples, my code would be changing every other week when I would want to bring new objects.

The below is the sample code for it

namespace ClassLibrary1
{

    public interface AbstractProductA { }
    public class ProductA1 : AbstractProductA { }
    public class ProductA2 : AbstractProductA { }


    public interface AbstractProductB { }
    public class ProductB1 : AbstractProductB { }
    public class ProductB2 : AbstractProductB { }


    public interface AbstractFactory
    {
        AbstractProductA CreateProductA();
        AbstractProductB CreateProductB();
    }


    public class ConcreteFactoryA : AbstractFactory
    {
        public AbstractProductA CreateProductA()
        {
            return new ProductA1();
        }

        public AbstractProductB CreateProductB()
        {
            return new ProductB1();
        }
    }

    public class ConcreteFactoryB : AbstractFactory
    {
        public AbstractProductA CreateProductA()
        {
            return new ProductA2();
        }

        public AbstractProductB CreateProductB()
        {
            return new ProductB2();
        }
    }

    public class Client
    {
        private AbstractProductA _productA;
        private AbstractProductB _productB;

        public Client(AbstractFactory factory)
        {
            _productA = factory.CreateProductA();
            _productB = factory.CreateProductB();
        }
    }

}

Now, how to make sense out of it. I can understand how it is working, but it looks so naive. One thing is that maybe the kind of example everybody on tutorial sites are choosing is not the right one.

Also, what benefit is it really giving me. I can do it in the usual non abstract factory kind of way using interfaces and the entire thing would still work just fine (design wise).

Detailed explanation with example is what I am looking for

relationship that "inherit" another (1:N) relationship

I want a data-structure that supports these specific 1:N relations :-
1#. Human raise 0-N Human
2#. Human has 0-N Dog
3#. Human cultivate 0-N Tree
4#. Dog is a house of 0-N Parasites.

enter image description here

Note:
- State in these relations are all temporary e.g. Human1 may raise Human2, but after a year, Human1 may abandon Human2.
- All objects are inherited from BaseObject and has unique int ID.

In all of the above relation, I want to be able to support these features :-
F1. add relation e.g. human_dog->addRelation(Human* a,Dog* b)
F2. remove relation e.g. human_dog->removeRelation(Human* a,Dog* b)
F3. query all children e.g. human_dog->getAllChildren(Human*)
F4. query all parent e.g. human_dog->getAllParents(Dog*)
F5. check whether a parent has >=1 child
F6. check whether a child has >=1 parent
F7. remove all children for a parent
F8. remove all parent for a child

This can be implemented by std::unordered_map or something more customized quite easily.

Here comes the hard part

I want to mark relation 1#,2#,3# (i.e. all solid lines) as Feed.
It has to support feature F3-F8 in an aggregating style.

For example :-

  • feed->getAllChildren(BaseObject* b) :
    If b is human, it must return all children of raise,has and cultivate of the b.
  • feed->removeAllParent(BaseObject* b) :
    If b is a dog, it will effect like cultivate->removeAllParent(b).

In my real case, there are 8-10 relations and 3-4 levels of such inherit/indirection.
I want to be able to easily inject such aggregation.
For example, it is useful to call :-

void BaseObject::declareForFreedom(){
    feed->removeAllParent(this);
}

Question

What is a data-structure/design-pattern that suitable for this case?

I currently create a custom 1:N relation for 1#-4#, and hard-code every feed's function. It is tedious.
I have banged by head for a few months, but not found any implementation that look elegant.

lundi 2 juillet 2018

change function to a class with method for setting input variables of function

I am writing an API for my library. I have a function that takes 5 classes as inputs :

result func(A a,B b,C c,D d,E e);

I want to change it to a class that takes the inputs one by one and then the class can be execute when all inputs set :

class func
{
void setA(A a);
void setB(B b);
.......
result execut();
}

i want to know this is a bad idea ?

Count the repeat pattern in SAS or R

I have a dataset which has all the clicks done on the website in 1 column. I want to find the pattern which gets repeated in the whole data and the data contains more than 1 Million rows and has 17000 different pattern. I also want to know the average time spend on each click for each pattern. I have written a code in SAS which groups each pattern and also finds the time difference between each click but I am not getting the output how I want. For example, according to my code I am getting this output:

Clicks Group Time(Seconds)

A 1 6 B 1 2 C 1 0 D 2 12 E 2 5 F 2 0 A 3 9 B 3 6
C 3 0 H 4 8 I 4 9 J 4 0

Output expected: Clicks Average Time Count ABC A-7.5,B-4,C-0 2 DEF D-12,E-5,F-0 1 HIJ H-8,I-9,J-0 1

real world example of Abstract Factory Design Pattern in C#

I am writing this question after reading many posts on Abstract Factory Design Pattern in C#.I really cannot make a real world use case sense out of the examples that all those posts provide. All I could see was some basic example of cars/computers/phones etc.I understand that they are important to provide a simple explanation. But I am really unable to map it to anything real world, because if I would really follow such examples, my code would be changing every other week when I would want to bring new objects.

Please help me with any example/link/post that would really implement it in a way that makes real world sense.

The links i have gone through are:

-https://www.dotnettricks.com/learn/designpatterns/abstract-factory-design-pattern-dotnet

-http://www.dofactory.com/net/abstract-factory-design-pattern

-https://www.codeproject.com/Articles/328373/Understanding-and-Implementing-Abstract-Factory-Pa

-https://www.c-sharpcorner.com/UploadFile/bd5be5/design-patterns-in-net/?utm_source=tuicool

Persisting a composition interface in Hibernate

I have the following class structure:

public abstract class Creature{
   private String name;
   //strategy pattern composition
   private SkillInterface skill;
}

public interface SkillInterface {
   void attack();
}

public class NoSkill implements SkillInterface {
   @Override
   public void attack() {
       System.out.println("No skills");
   }
}

Subclasses of SkillInterface are without any fields. As they determine the behaviour, I would like to save these strategies as a column to some already existing table. I tried to implement it with @Converter annotation, using AttributeConverter class to convert SkillInterface to String and save, but always had mapping exceptions. I want to be able to save it as String and retrieve as SkillInterface reference.

But how can I implement it with Hibernate? Or do I have a design mistake?