lundi 3 septembre 2018

Child Interface with no methods

So I have an parent interface, and some other child interfaces, some have implementation, some others don't.

interface IParent {
    void A();
    void B();
}

interface IChild1 : IParent {
    void C();
}

interface IChild2 : IParent {
    // empty, just inherits
}

Is that good practice? If not, what's the better way to do it?

Cache Tolerance in Matlab Pattern Search

I have been experimenting with the following options with patternsearch in MATLAB.

'Cache', 'On'

'CacheTol', tol

'CacheSize', 1e6

From the docs I would expect a very large value of CacheTol simply to cause all the fitness evaluations to return the same result.

https://uk.mathworks.com/help/gads/use-cache.html

However, even with a value of realmax for 'CacheTol' the solver still reaches a reasonable answer.

I am using madspositivebasis2n

Is there an explanation for this behavior?

design patterns how to avoid instanceOf when using List

Imagine you have a menu with dishes each dish should be available in multiple languages (French , English , Arabic , ...). The dish class contains a list with language type objects.

     class Dish { 
           List<Language> languages
           void addLanguage(Language lg){...}
     }

     class Language {String getDescription(){}}

     class French extends Language{}

     class Menu {List<Dish> dishes }

How do i avoid using instance of when wanting a description of a specific language for that dish?

Should i define for each language a get method in the dish class:getFrench(), getArabic(),..?

Or should i keep it in list and check for instance of French by looping the list and then call getDescription() on this list object ?

Or is there a way for a more polymorphic approach?

dimanche 2 septembre 2018

clone function for prototype design pattern

One of the most important part of prototype design pattern in OOP is that we don't create new object from scratch, we just clone them using clone() function from existing object. So is clone() function deep or shallow copy? if it's deep copy, than I understand everything, but if it's shallow one, it will be mess if two different objects(one created from another using prototype pattern) watch same state objects, than it means that these object aren't different at all(they are linked as they share same state objects). can anyone clarify me this situation?

Which design pattern does this code follow or does it follow at all?

Appreciate if anyone could check the below and point out which design pattern it follows. If it does not, could suggest a better way?

Looking forward for responses. Thanks in advance.

namespace CloudStorageManager
{
     internal abstract class RemoteItemBase
     {
         internal string Name { get; set; }
         string Path { get; set; }
         string Parent { get; set; }
     }

     internal class RemoteItem : RemoteItemBase
     {
         public RemoteItem()
         {
             Folders = new List<RemoteItem>();
             Files = new List<RemoteItem>();
         }

         internal virtual ICollection<RemoteItem> Folders { get; set; }
         internal virtual ICollection<RemoteItem> Files { get; set; }
     } 
}

How can conditional code be improved ? Can inner class or lambada be used in this scenario?

I am writing an application which has lot of checks , most of the logic is based on condition something like if this and this condition is met then this else something else .,So i am trying to find an elegant way to write code , how can make such code comply with SOLID principles. I cannot give the code of my application here hence i will try to simulate problem with simpler example. Below is the code

class Person {
    public String gender;
    public int age;
    public int birthYear;
    public Benefits b;

    public Benefits getB() {
        return b;
    }

    public void setB(Benefits b) {
        this.b = b;
    }
}

class Student extends Person {
    public boolean isEngineerStudent;
    public boolean isMedicalStudent;
}

class Employed extends Person {
    public boolean privateSector;
    public boolean governmentSector;
    public int baseIncome;
}

class Benefits {
    public boolean isInsuraceAvailabe;
    public boolean isStipendAvailable;
    public boolean isPensionAvaiable;
    public boolean isHealthCheckupAvailable;
    public boolean isTransportAvaiable;
    public boolean isLabFacilityProvided;
    public boolean isComputerFacilityProvided;

}

public class EnableBenefits {

    public void enbableBenefits(Person p) {
        Benefits b = new Benefits();
        p.setB(b);
        if (p instanceof Student) {
            Student s = (Student) p;
            if (s.isEngineerStudent && s.age > 22) {
                s.b.isStipendAvailable = true;
                s.b.isComputerFacilityProvided = true;
            }
            if (s.isMedicalStudent && s.age > 20) {
                s.b.isStipendAvailable = true;
                s.b.isLabFacilityProvided = true;
            }
        }

        if (p instanceof Employed) {
            Employed e = (Employed) p;
            if (e.governmentSector == true && e.age > 40 && e.gender.equalsIgnoreCase("F") && e.birthYear <= 1960) {
                e.b.isPensionAvaiable = true;
                e.b.isInsuraceAvailabe = true;
                e.b.isTransportAvaiable = true;
            }
            if (e.governmentSector == true && e.age > 40 && e.gender.equalsIgnoreCase("M") && e.birthYear <= 1960
                    && e.baseIncome < 20000) {
                e.b.isPensionAvaiable = true;
                e.b.isInsuraceAvailabe = true;
            }
            if (e.privateSector == true & e.gender.equalsIgnoreCase("M") && e.birthYear <= 1960) {
                e.b.isInsuraceAvailabe = true;
                e.b.isTransportAvaiable = true;
            }
        }
        p.b.isHealthCheckupAvailable = true;
    }

}

I have purposefully made the access modifiers of instance variables as public so that it does not have too much code so spare me for that.

Though the code does not have too much condition and logic . Just imagine the code for student and employee logic are in hundreds of line

The question is below

1) How can i write huge if condition in better way ? can i use Lamba here ? of course the if condition is not too big but in my real application i can easily have 8 conditions.

if (e.governmentSector == true && e.age > 40 && e.gender.equalsIgnoreCase("M") && e.birthYear <= 1960
                    && e.baseIncome < 20000) {
                e.b.isPensionAvaiable = true;
                e.b.isInsuraceAvailabe = true;
            }

2) Is it better to write below code in Inner Class

if (p instanceof Student) {
            Student s = (Student) p;
            if (s.isEngineerStudent && s.age > 22) {
                s.b.isStipendAvailable = true;
                s.b.isComputerFacilityProvided = true;
            }
            if (s.isMedicalStudent && s.age > 20) {
                s.b.isStipendAvailable = true;
                s.b.isLabFacilityProvided = true;
            }
        }

3) Is there any better way i can write this code

Using props in a function which is not a component in react

So, say for example my code looks something like this:

class DoSomething extends Component{
  function1(var1, var2){
    return var1 * var2 / this.props.var3
  }

  render(){
    <div>{function1(10, 20)}</div>
  }
}

I would like to abstract function1 into its own file and import it but I would still like it to have access to this component's (or redux's) props / state.

What would be the best way of accomplishing this? Sorry if this question is really whishy, washy.