lundi 2 juillet 2018

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?

Haskell Pattern Matching (beginner)

I have to implement a small programm in Haskell that increments/decrements a result by what in the console line is. For example if we have -a in the console the results must be 0, if -b the result must be incremented with 6 and so on. I have to do this with pattern matching.

I haven't used Haskell until now and I find it pretty hard to understand. I have this to start with:

import System.Environment
main = getArgs >>= print . (foldr apply 0) . reverse
apply :: String -> Integer -> Integer

I don't understand what in the main is. What does it make and the reverse from end, what does it do? As I've read on the internet the getArgs function gives me the values from the console line. But how can I use them? Are there are equivalent functions like for/while in Haskell?

Also, if you have some examples or maybe could help me, I will be very thankful.

Thanks!

calling a sound file from self executable function using module pattern javascript

Im testing to play a simple sound by click my mouse on canvas html5 using module pattern and the self executable functions. it works without the self executable function, but no within. Sorry I have no idea about to upload the simple sound file here. My console saids

    "Uncaught (in promise) DOMException: The element has no supported sources.
mouseDown @ audioFuncionProtegida.js:82"

so Im afraid Im missing something to call the sound file

all the code at https://jsfiddle.net/xy2cs03p/7/

Is using dictionary good practice to keep same classes?

I need to collect some classes and provide it by request to some parts of program. I have following code:

public interface ISameClass
{
    int Value { get; set; }
    void DoStuff();
}

public class SameClass : ISameClass
{
    int Value { get; set; }
    void DoStuff()
    {
    //Do something
    }
}

public class SameClassProvider
{
        private readonly Dictionary<string, ISameClass> _sameClasses;

        public SameClassProvider(string parentDir)
        {
            _sameClasses = new Dictionary<string, ISameClass>
            {
                { "Type1", new SameClass() },
                { "Type2", new SameClass() },
                { "Type3", new SameClass() }
            };
        }

        public bool AddClass(string type, ISameClass class)
        {
            if (_sameClasses.ContainsKey(type) || class == null)
            {
                return false;
            }

            _nodes.Add(type, class);
            return true;

        }

        public ISameClass GetClass(string type)
        {
            if (_sameClasses.TryGetValue(type, out var someClass))
            {
                return someClass;
            }
        }
}

Is using classes like SameClassProvider is good practice? Or i can refactor this something or replace with correct pattern? Tnx

dimanche 1 juillet 2018

pattern matching between different multiscripts

im new to programming and i need to know 2 things 1) How to search the term 'helper' between these two scripts and display the containing script as output. Please complete the code as as whole

import re

text_to_search = ''' There will be no shortage of topics for Federal Reserve Chairman Jerome Powell’s press conference on Wednesday, analysts said.

The post-meeting press briefing “figures to be one of the most lively post-meeting press conference of any Fed chair seen to date,” said Richard Moody, chief economist at Regions Financial Corp. in Birmingham, Ala.

While on the surface, the economy is looking strong, questions about the outlook.

There has never been a stimulus at the end of a tightening cycle. The $1.5 trillion Trump tax cut package and $300 billion increase in federal spending are just starting to have an impact on the economy and estimates vary widely about the road ahead '''

Text = 'Helper is the best'

pattern = re.compile(r'helper')

Composition vs facade pattern

I found a response that made me understand a bit more class compositioning.

class Engine
{

}

class Automobile
{

}


class Car extends Automobile // car "is a" automobile //inheritance here
{ 
 Engine engine; // car "has a" engine //composition here

}

but isn't this a facade pattern? Isn't facade about making the class simpler by dividing into subclasses? What's then the difference between composition and facade?

Or maybe I get it wrong. The facade is a design pattern but the composition is more like a good practice to follow. Can we say that using facade is doing composition?

Design pattern with three.js and webpack

I'm looking for a better way to organize my code with three.js and webpack than having everything in a single file (camera, meshes, lights, postprocessing, etc).

I thought I could use "manager modules" like a LightManager class, or a PostProcessingManager class, for instance. But then, these modules should be singletons, since I would only need one instance, right? Singleton is said to be a "bad" pattern, though (or you know, "it's not that it's bad, but in many cases you probably miss a better solution").

So I'm wondering what are my options? How do you guys organize your code?