dimanche 25 octobre 2015

Java Relationship between interfaces/abstract classes

I am trying to build an algorithm that works in different ways depending on a traversal strategy and an update strategy. However, not every update Strategy works with every traversal strategy. Hence, I figured that an update strategy must only be instantiated with a corresponding traversal strategy. I wanted to force a constructor for that (see below). So that the subclasses would have to check if they support the strategy.

I am currently having an Interface

public interface TraversalStrategy {
...
}

And an (invalid) abstract class

public abstract class UpdateStrategy {
protected TraversalStrategy travStrategy;

public abstract UpdateStrategy(TraversalStrategy travStrategy);
}

What is the correct way to imply such a dependency? I could of course add an empty body to this constructor but that seemed wrong to me.

I am sure there has been questions asking similar things but I don't really know how to describe this correctly. Hence it is hard for me to find the right answer. I checked the following questions that didn't help me much:

Relationship between Interfaces and Abstract Data Type in Java

Abstract classes and interfaces in Java

Can an abstract class have a constructor?

Design pattern for configurable system

I have an interesting design problem that I will attempt to simplify in a toy problem below:

I wish to design a system for which the output will be student objects based on certain inputs and intermediary processing. The flow will be as follows: I have a list of classrooms as one type of input. To generate the output, the processing steps are:

  1. Filter each classroom by students under the age of X (lets say 10)
  2. Sort the filtered results by any permutation of this hierarchical order: height, weight, arm length
  3. Return the top 8 students.

Another input can simply be a list of students I already have and want included as part of the result. For example: Input 1: List of 3 students, Input 2: List of 2 classrooms for which the processing steps above will run.

What would be the best way to design such a system with inputs being input type {student list|classroom list}, filter type {age|height|etc}, sort order{any ordering of height,weight,arm length}, returnNum{how many students to return}? The system should be flexible enough to accommodate more input types and more sort order entries {ie. sort students by shoe size}. What data structure can I use to model each part of this section (ie. what is the best way to represent the sort order criteria?) Is there any design pattern that would fit these needs? Any help with the architecture design would be greatly appreciated!

Model View Controller pattern in REST HTTP client

I am a bit confused what should Model consist of in the case of REST client. Let's assume I want to have a client to communicate with server via REST. No database.

Example: I want to send a POST HTTP request with some form data to a server. Where should I put methods like construct_REST_request() or execute_REST_request()? In Controller or in Model? In model theoretically should be business logic objects. Does it mean in Model should be only data that Controller uses?

how to refactor a registry to prevent false positives from valgrind

The following registry, implemented as a singleton class, works fine for me. However, as it is not teared down on exit, each registry entry causes one error report stanza in valgrind. How to refactor the code to get rid of these false positives?

// registry.h

class SFuncRegistry {
 private:
    SFuncRegistry() {};
    SFuncRegistry(SFuncRegistry const&) = delete;  //! To prevent copying
    void operator=(SFuncRegistry const&) = delete; //! To prevent copying

    map<string,const class CFunc*> FMap;

 public:
    static SFuncRegistry& getInstance()
        {   static SFuncRegistry instance; //! Instantiated on first use.
            return instance; }

    const class CFunc* find( string nam ) const;

    void register( string nam, const class CFunc* fun );
}    

// main.cpp

{
    SFuncRegistry* GFuncRegistry = &(SFuncRegistry::getInstance());

    GFuncRegistry->register( ... )
    ...

    const class CFunc fun = GFuncRegistry->find( ... )
    ...
}

What is the real need of Model driven approach ?

Doing some research on model driven programming and found it interesting but I can’t clearly understand the real problem what it tries to solve And how can we call the current approach, is it object oriented approach? If that’s the case OOP is a part and parcel of MDA so what is the approach are we following right now I just want something to compare and see the pros and cons of MDA

Using generics to return List of various types

Trying to get my head around generics and am wanting to create a class that returns records of various types.

So what I want is something like this:

public class Job
{
    public JobType Type { get; set; }

    public Job (JobType jobType)
    {
        Type = jobType;
    }

    public IList<T> GetRecords<T>()
    {
       IList<T> records = new IList<T>();

       if (Type == JobType.Type1)
       {
           List<Type1Record> records = GetType1Records();
       }
       if (Type == JobType.Type2)
       {
            List<Type2Record> records = GetType2Records();
       }
        return records;
    }
}

I would want to call this using something like

var job = new Job(JobType.Type1);

var jobRecords = Job.GetRecords();

I think the code above conveys the intention but I am beginning to think that I am going about this in the wrong way because the above just doesn't work at all. IList cannot be converted to other types like this.

Does anybody know how I can achieve something like this.

How to print an inclined pyramid pattern in java using for loop in java?

I have been trying to print different patterns using for loop statement in java. And now I want to learn how to print the following pattern. It is basically an inclined pyramid type of pattern.

    *
    **
    ***
    ****
    ***
    **
    *