mardi 21 février 2017

Design pattern for Parent child relation

What is the best design pattern for Parent child relationship in Javascript?

  1. Parent / child can live on its own.
  2. Is it better to inject parent on every child and use pub/sub design pattern?
  3. Is it better to maintain all the children on the parent?

The idea here is, parent should publish an event, and child should subscribe those events. Parent publish the event, based on the event in child. It's kind of confusing here, because parent is listening on the child's event and child is listening on Parent's event.

I tried using pub/sub pattern, but I am not sure how to use it in the context above. Somehow, Parent should not worry about the children and publish the message. But that publish should happen on some state change in children.

Can someone point me to the right design pattern here?

How to organize querys from models EF?

I am something new in programming, currently I have made a context in EF. Once generated the models for example I have clients and orders, and I have the following methods in a controller, GetOrderFromClient (...), GetOrderFromDate (...). But I would like to know what would be the correct way to organize these methods or if there is any design pattern. My idea for example I have to create another model class (OrdersOperations) and I pass the context as parameter to then make the queries.

Static methods and table data gateway patter

I implement table data gateway on data access layer and transaction script on business because. Is it possible to make table data gateway with non static methods and therefore create DAL objects in BL to calls gateway methods?

Now I do

var result = myGateway.SelectAll(p1, p2, p3);

with non-static methods it would be

var MyGWObject = new myGateway();
var result = MyGWObject.SelectAll(p1,p2,p3);

in every transaction (probably organized as a class and the object would be its private variable).

Is this ok or really bad? And also is the second approach when object is created more like Table Module or just some mess?

Redux and Design Patterns

I've been using Redux for several months and have a good feel for the unidirectional data flow. However, I'm not trained in OOP and Design Patterns. After listening to a talk by Ralph E Johnson my first reaction was that the Observable Pattern is very similar to Redux/Flux flow, is that correct? Where does that analogy break down?

He talks about the Interfaces required to implement Observable Patterns - is this the sort of thinking that the authors of Redux/Flux architecture have in mind when designing these libraries/architectures?

Cyclic dependency in design pattern

I am stuck with implementing one of design since it has cyclic dependency. I need to generate a shapes report as per below code snippet and I thought of implementing this as separating as Shape classes which implements common IShape interface and Language classes which implements common ILanguage interface. This way any shape will tell its name itself but it doesn't know about language, and if language tells any shape's name then it doesn't know about shape type and also if it is a single shape or collection of shapes. But report is generated based on total number of Shapes so there seems to be circular dependency between both groups. Can someone help to solve this. Thanks in advance.

public string PrintShape(List<Shape> shapes, string language)
{
    string returnString = "";

    if (shapes.Count == 0)
    {
        returnString = (language == "EN") ? "Empty list of shapes" : "Lege lijst van vormen";
    }
    else
    {
        returnString += (language == "EN") ? "Shapes report: " : "Samenvatting vormen: ";

        int numberSquares = 0, numberCircles = 0;

        for (int i = 0; i < shapes.Count; i++)
        {
            if (shapes[i].type == "SQUARE")
            {
                numberSquares++;
            }
            if (shapes[i].type == "CIRCLE")
            {
                numberCircles++;
            }
        }

        if (language == "EN")
        {
            returnString += numberSquares + ((numberSquares == 1) ? "Square " : "Squares ");
            returnString += numberCircles + ((numberCircles == 1) ? "Circle " : "Circles ");
        }
        else
        {
            returnString += numberSquares + ((numberSquares == 1) ? "Vierkant " : "Vierkanten ");
            returnString += numberCircles + ((numberCircles == 1) ? "Cirkel " : "Cirkels ");
        }

        returnString += (language == "EN") ? "TOTAL: " : "TOTAAL: ";
        returnString += (numberCircles + numberSquares) + " " + (language == "EN" ? "shapes" : "vormen");
    }

    return returnString;
}

Is it correct to say that the design pattern for exception in Java is "Chain of Responsibility"

Is it correct to say that the exception mechanism in Java is "chain of responsibility"?

from the one hand, as far as I understand, for every exception that we have in Java, we "run over" the "catch blocks" and check which one is responsible for to handle it, which is look like "chain of responsibility"

but in the other hand, since "catch blocks" are not object, and chain of responsibility is talking about "processing objects".

so what am I missing here?

In a game programming project, how do I handle multiple items with multiple instances?

Assuming that I have a 2 player game that has multiple weapons in it. At the start of the game, the players can choose their own weapons and equip it to their characters. So if Right now, I have the following code to implement the said scenario:

public class Weapon {
    private String name;
    private int attackPoints;
    private int effectivity = 100;

    public Weapon(String name, int attackPoints) {
        this.name = name;
        this.attackPoints = attackPoints;
    }
    // ...
}

public class Character {
    private String name;
    private Weapon weapon;

    public Character(String name, Weapon weapon) {
        this.name = name;
        this.weapon = weapon;
    }
    // ...
}

public class Game {
    Scanner scanner = new Scanner(System.in);
    public void createCharacter() {
        String namePlayer = "Knight";
        String nameOpponent = "Thief";

        //Weapon Menu
        System.out.println("Select weapon");
        System.out.println("1. Dagger - Attack: 10");
        System.out.println("2. Sword - Attack 20");

        Character player;
        Character opponent;
        int choice = scanner.nextInt();

        //Weapon assignment
        switch (choice) {
        case 1:
            player = new Character(name, new Weapon("Dagger", 10));
            break;
        case 2:
            player = new Character(name, new Weapon("Sword", 20));
            break;
        }
    }
}

Rephrased the question

My concern is, what if I have to add an additional weapon, say "Ax with 15 damage" for example, then I'd have to modify the weapon menu, and at the same time add an additional case in my switch statement. Is it possible that I'd only have to modify some kind of a list and it'll take care of the list of weapons available for the player to choose and at the same time make the instances of the weapons different for the player and the opponent character?

Old question

My concern is, every time I have a new weapon, I have to modify the weapons menu and at the same time modify the weapon assignment part. Is there a better way for implement it?