问:
Constructor vs setter validations
I have the following class :

```<code>public class Project {

    private int id;
    private String name;

    public Project(int id, String name, Date creationDate, int fps, List<String> frames) {
        if(name == null ){
            throw new NullPointerException("Name can't be null");
        }

        if(id == 0 ){
            throw new IllegalArgumentException("id can't be zero");
        }

            this.name = name;
            this.id = id;

    }

    private Project(){}

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

}
</code>```

I have three questions:

Do I use the class setters instead of setting the fields directly. One of the reason that I set it directly, is that in the code the setters are not final and they could be overridden.

If the right way is to set it directly and I want to make sure that the name filed is not null always. Should I provide two checks, one in the constructor and one in the setter.

I read in effective java that I should use NullPointerException for null parameters. Should I use IllegalArgumentException for other checks, like id in the example.

答:
I believe you shouldn't get anything extra in the getters and setters.
There will be time in the future where putting extra functionalities in getters and setters cause weird side effects.

But the most compelling reason is when you use the class as data entity, those getters and setters might be used by java bean container (EJB, Spring), JPA, XML marshaller and unmarshaller.

I also prefer using IllegalArgumentException over NPE.
If you plan to catch and recover or just log the error, it's almost always better to be as specific as possible.

But as you mentioned already, for validation. Having a separate validation method is much better.

Title: Constructor vs setter validations


问:
What types of programming require practical category theory?
Category theory has applications in theoretical computer science and obviously is central to abstract mathematics.  I have heard that it also has direct practical applications in programming and software development.

What type of programming is practical category theory necessary for? What do programmers use category theory to accomplish?

Please note my use of "necessary" and "require" in this post.  I realize that in some sense most programmers will benefit from having experience in different types of theories, but I am looking for direct applications where the usage of category theory is essential, i.e. if you didn't know category theory, you probably couldn't do it.

Also, I'd like to clarify that by "what type of programming," I am hoping less for a broad answer like "functional programming," and more for specific applications like "writing bank software" or "making operating systems."

答:
The question is asking about an abstract mathematical concept (category theory) while hoping for a very practical answer (specific applications). With all due respect, I think this is unrealistic expectation.

Abstract mathematical concepts are part of the fundamentals of programming languages, not applications. For instance, data types are central to programming. Every language has some form of data types and implements a type system -- whether static or dynamic, strong or weak, explicit or implicit, etc. However, there is no standard.

Therefore, many computer scientists have attempted to use category theory to define a unified type system. See for instance Hagino's Categorical Programming Language (1987) and Charity (1996), then ML (2003) and CAML, and Haskell of course, which defines a "Haskell category" of types, and Haskell functions are morphisms on types...

This is the case because type theory is closely related to category theory. To quote JL Bell: "Categories can themselves be viewed as type theories of a certain kind... Thus type theory is much more closely related to category theory than it is to set theory... Roughly speaking, a category may be thought of as a type theory shorn of its syntax." It has been shown that, for instance, Cartesian closed categories correspond to typed λ-calculus and C-monoids correspond to untyped λ-calculus...

I don't think category theory is necessary for any type of programming, but it is a very useful tool in the design and implementation of programming languages, and esp. those which are inherently mathematical. That is why Functional Programming is often cited as a categorical programming, and all the programming languages mentioned above are FP languages.

A recommended introduction to the topic is "A taste of category theory for computer scientists" by BC Pierce (1988). This and other useful info was found on a similar discussion on mathoverflow.

OwnerDisplayName: Alexander Gruber

ContentLicense: CC BY-SA 3.0


问:
Best Practice: What can be the hashCode() method implementation if custom field used in equals() method are null?
What is the best practice to return a value for hashCode() method if custom field used in equals are null ?

I have a situation, where equals() override is implemented using custom fields. Usually it is recommented to override hashCode() using that custom fields used in equals(). But if all the custom fields used in equals() are null, then what would be the best implementation for hashCode()?

Example:

```<code>class Person {
    private String firstName;
    private String lastName;
    public String getFirstName() {
        return firstName;
    }
    public String getLastName() {
        return lastName;
    }
    @Override
    public boolean equals(Object object) {
        boolean result = false;
        if (object == null || object.getClass() != getClass()) {
            result = false;
        } else {
            Person person = (Person) object;
            if (this.firstName == person.getFirstName()
                && this.lastName == tiger.getLastName()) {
                result = true;
            }
        }
        return result;
    }
    @Override
    public int hashCode() {
        int hash = 3;
        if(this.firstName == null || this.lastName == null) {
            // <b>What is the best practice here, </b>
            // <b>is return super.hashCode() better ?</b>
        }
        hash = 7 * hash + this.firstName.hashCode();
        hash = 7 * hash + this.lastName.hashCode();
        return hash;
    }
}
</code>```

is it required to check for null in hashCode() ?

If yes, what should be returned if custom values are null ?

答:
If you can guarantee through your code that firstName and lastName are never null, there is no reason to test for it.

Its possible that these values are only set in the constructor and there is no setter for it.  In that case, they are never null.

Or it may be that you have a setter that checks for null and populates an empty string if a null is passed in

In either of those cases, you can guarantee that the values are always not null and thus don't need to test for it.

Doing this will drastically simplify the amount of thought needed to reason about the code and is probably the right way to go about implementing the code.

If you do allow for nulls in the values, you could just say that the hash of the value is 0.  Based on your previous code:

```<code>@Override
public int hashCode() {
    int hash = 3;
    int firstHash = 0;
    int lastHash = 0;
    if(this.firstName != null) {
        firstHash = this.firstName.hashCode();
    }
    if(this.lastName != null) {
        lastHash = this.lastName.hashCode();
    }

    hash = 7 * hash + firstHash;
    hash = 7 * hash + lastHash;
    return hash;
}
</code>```

hashCode isn't trying to be a cryptographically secure hash - its just trying to make it so you don't have everything colliding in a HashMap and have it degenerate to a linked list.

You may be interested in the @NotNull annotation which puts a compile time requirement on this.  This was implemented as part of JSR 308... but thats something for Java 8.

Body:
If you can guarantee through your code that firstName and lastName are never null, there is no reason to test for it.

Its possible that these values are only set in the constructor and there is no setter for it.  In that case, they are never null.

Or it may be that you have a setter that checks for null and populates an empty string if a null is passed in

In either of those cases, you can guarantee that the values are always not null and thus don't need to test for it.

Doing this will drastically simplify the amount of thought needed to reason about the code and is probably the right way to go about implementing the code.

If you do allow for nulls in the values, you could just say that the hash of the value is 0.  Based on your previous code:

@Override
public int hashCode() {
    int hash = 3;
    int firstHash = 0;
    int lastHash = 0;
    if(this.firstName != null) {
        firstHash = this.firstName.hashCode();
    }
    if(this.lastName != null) {
        lastHash = this.lastName.hashCode();
    }

    hash = 7 * hash + firstHash;
    hash = 7 * hash + lastHash;
    return hash;
}

hashCode isn't trying to be a cryptographically secure hash - its just trying to make it so you don't have everything colliding in a HashMap and have it degenerate to a linked list.

You may be interested in the @NotNull annotation which puts a compile time requirement on this.  This was implemented as part of JSR 308... but thats something for Java 8.


问:
What is the best practice for reading a large number of custom settings from a text file?
So I have been looking through some code I wrote a few years ago for an economic simulation program. Each simulation has a large number of settings that can be saved to a file and later loaded back into the program to re-run the same/similar simulation. Some of the settings are optional or depend on what is being simulated.

The code to read back the parameters is basically one very large switch statement (with a few nested switch statements). I was wondering if there is a better way to handle this situation.

One line of the settings file might look like this:

```<code>#RA:1,MT:DiscriminatoryPriceKDoubleAuction,OF:Demo Output.csv,QM:100,NT:5000,KP:0.5 //continues...
</code>```

And some of the code that would read that line:

```<code>switch( Character.toUpperCase( s.charAt(0) ) )
{
    case 'R':
         randSeed = Integer.valueOf( s.substring(3).trim() );
         break;
    case 'M':
          marketType = s.substring(3).trim();
          System.err.println("MarketType: " + marketType);
          break;
    case 'O':
          outputFileName = s.substring(3).trim() ;
          break;
    case 'Q':
          quantityOfMarkets = Integer.valueOf( s.substring(3).trim() );
          break;
    case 'N':
          maxTradesPerRound = Integer.valueOf( s.substring(3).trim() );
          break;
    case 'K':
          kParameter = Float.valueOf( s.substring(3).trim() );
          break;
 // continues...
}
</code>```

答:
Almost every decent application and framework I know uses XML to store configurations.

I would suggest that you create a configuration class if you use java. Then you can use JAXB to annotate and serialize this class. After that, reading and writing the XML file via model is a simple thing to do.


问:
How do I prove or disprove "God objects" are wrong?
Problem Summary:

Long story short, I inherited a code base and a development team I am not allowed to replace and the use of God Objects is a big issue.  Going forward, I want to have us re-factor things but I am getting push-back from the teams who want to do everything with God Objects "because its easier" and this means I would not be allowed to re-factor.  I pushed back citing my years of development experience, that I'm the new boss who was hired to know these things, etc,  and so did the third party offshore companies account sales rep, and this is now at the executive level and my meeting is tomorrow and I want to go in with a lot of technical ammunition to advocate best practices because I feel it will be cheaper in the long run (And I personally feel that is what the third party is worried about) for the company.

My issue is from a technical level, I know its good long term but I'm having trouble with the ultra short term and 6 months term, and while its something I "know" I can't prove it with references and cited resources outside of one person (Robert C. Martin, aka Uncle Bob), as that is what I am being asked to do as I have been told having data from one person and only one person (Robert C Martin) is not good enough of an argument.

Question:

What are some resources I can cite directly (Title, year published, page number, quote) by well known experts in the field that explicitly say this use of "God" Objects/Classes/Systems is bad (or good, since we are looking for the most technically valid solution)?

Research I have already done:

I have a number of books here and I have searched their indexes for the use of the words "god object" and "god class". I found that oddly its almost never used and the copy of the GoF book I have for example, never uses it (At least according to the index in front of me) but I have found it in the two books below, but I want more I can use.

I checked the Wikipedia page for "God Object" and its currently a stub with little reference links so although I personally agree with that it says, it doesn't have much I can use in an environment where personal experience is not considered valid.  The book cited is also considered too old to be valid by the people I am debating these technical points with as the argument they are making is that "it was once thought to be bad but nobody could prove it, and now modern software says "god" objects are good to use".  I personally believe that this statement is incorrect, but I want to prove the truth, whatever it is.

In Robert C Martin's "Agile Principles, Patterns, and Practices in C#" (ISBN: 0-13-185725-8, hardcover) where on page 266 it states "Everybody knows that god classes are a bad idea.  We don't want to concentrate all the intelligence of a system into a single object or a single function.  One of the goals of OOD is the partitioning and distribution of behavior into many classes and many function." -- And then goes on to say sometimes its better to use God Classes anyway sometimes (Citing micro-controllers as an example).

In Robert C Martin's "Clean Code: A Handbook of Agile Software Craftsmanship" page 136 (And only this page) talks about the "God class" and calls it out as a prime example of a violation of the "classes should be small" rule he uses to promote the Single Responsibility Principle" starting on on page 138.

The problem I have is all my references and citations come from the same person (Robert C. Martin), and am from the same single person/source.  I am being told that because he is just one view, my desire to not use "God Classes" is invalid and not accepted as a standard best practice in the software industry.  Is this true?  Am I doing things wrong from a technical perspective by trying to keep to the teaching of Uncle Bob?

God Objects and Object Oriented Programming and Design:

The more I think of this the more I think this is more something you learn when you study OOP and it's never explicitly called out; Its implicit to good design is my thinking (Feel free to correct me, please, as I want to learn), the problem is I "know" this, but but not everybody does, so in this case its not considered a valid argument because I am effectively calling it out as universal truth when in fact most people are statistically ignorant of it since statistically most people are not programmers.

Conclusion:

I am at a loss on what to search for to get the best additional results to cite, since they are making a technical claim and I want to know the truth and be able to prove it with citations like a real engineer/scientist, even if I am biased against god objects due to my personal experience with code that used them.  Any assistance or citations would be deeply appreciated.

答:
Oh here I go, necroing another old question but I'm going to guess you didn't win this argument and here's why.

It Sounds Like a Culture Problem

You're their manager but you can't replace them and you have to go to your managers in order to get them to do what you believe they should be doing which in this case I assume is to at least knock it off with the god objects moving forward. It sounds to me like a classic case of code mirroring the culture it was born in. In other words the god object is how this company works. Want to make any kind of meaningful change? You're always going to the same place for it ultimately since all decisions apparently have to be cleared at the top.

Having been privy to multiple failed attempts at legacy code cleanups and code policy changes I am now of the opinion that you cannot fight the culture that made the code possible in the first place when that culture is still firmly entrenched or at the very least, fighting culture is a very hard uphill slog that it's unlikely anyone could ever pay me enough or give me enough power to feel like it was a worthwhile endeavor that was likely to succeed ever again.

Before there can be change, they have to be motivated to change and unfortunately as programmers who give enough of a damn to read Stack occasionally it's hard for us to understand that not everybody is motivated by the same stuff. I've won plenty of the rational arguments in my experiences but at the end of the day the company suffers intellectually lazy devs for a reason.

Maybe the business concerns have been motivated to think only about tomorrow rather than next week or five years out (an especially frustrating scenario when you're the child of immigrants from a country that built a seed vault in the Arctic in case of apocalypse). Maybe they're afraid of change. Maybe they value seniority overly much to the point that the chain of command is meaningless even when they have to hire from the outside to bring in a dev team lead or manager because they recognize nobody on their all-lifer team has grown enough in their time there (or because said lifers don't want the risk associated with responsibility). Or, and I've seen this, perhaps it's the very real and depressing phenomenon of mediocrity championing itself because it knows deep down inside that it can't compete with quality and when there's enough tomfoolery to go around it's impossible to figure out who to blame when everything regularly goes to pieces.

How do you fight issues that are ultimately rooted in fear or broken metrics for success? I'll tell you this, it takes a lot more than even a committed quality dev team and you had a lot less than that from the sound it at the time this Q was posted.

In the Not Impossible Event That It's Not an Intractable Culture Problem...

Now assuming people at the top are very receptive to change, and they're actually willing to trust you to make it, you really only need to punctuate all of your arguments with money and lost opportunity.

Every time it takes longer to train somebody new up on this ridiculous codebase, every time it takes longer to modify or add a new feature to something, every time it becomes prohibitively difficult to expose endpoints to another system from a company you want to partner with, it is costing money. Lots of freaking money. Most importantly it leaves a window open for a smaller more agile competitor to swoop in, put you in a position where all you can do is react to them far too slowly, and ultimately snatch it all away from you.

Just recognize that a particularly stubborn and stupid culture will maintain the status quo at all costs even if the actual physical roof of the company is in fact on fire because of it.

OwnerDisplayName: honestduane


问:
What is the best way to work with large databases in Java depending on context?
We are trying to figure out the best practice for working with very large DBs in Java.

What we do is a kind of BI (business Intelligence), i.e analyzing very large DBs, and using them to create intermediate DBs that represent intelligent knowledge of the DBs.

We are currently using JDBC, and just preforming queries using a ResultSet.

As more and more data is being created, we are wondering whether more appropriate ways exist for parsing and manipulating these large DBs:

We need to support 'chunk' manipulation and not an entire DB at once(e.g. limit in JDBC, very poor performance)

We do not need to be constantly connected since we are just pulling results and creating new tables of our own.

We want to understand JDBC alternatives, with respect to advantages and disadvantages.

Whether you think JDBC is the way to go or not, what are the best practices to go by depending on context (e.g. for large DBs queried in chunks)?

答:
Enterprise tools like IBM InfoSphere do exactly what you did with JDBC connection. I touched their IBM DataStage studio for a while, I saw that.

My advice for you is designing the schema of the big source data so that when you do intermediate data transformation you could write down the progress (by using some column), so the the big task can be divided into smaller tasks based on values of the progress column. Say fetch one fetches 20000 rows, marking the offset for fetches 2, etc...

I would do as much in java as possible because of the plethora of ways in which you can log, debug when somethings go wrong. If you rely too much on the DB, I don't think debugging and log-reading would be that comfortable.


问:
Single python file distribution: module or package?
Suppose I have a useful python function or class (or whatever) called useful_thing which exists in a single file. There are essentialy two ways to organize the source tree. The first way uses a single module:

```<code>- setup.py
- README.rst
- ...etc...
- foo.py
</code>```

where useful_thing is defined in foo.py. The second strategy is to make a package:

```<code>- setup.py
- README.rst
- ...etc...
- foo
|-module.py
|-__init__.py
</code>```

where useful_thing is defined in module.py. In the package case __init__.py would look like this

```<code>from foo.module import useful_thing
</code>```

so that in both cases you can do from foo import useful_thing.

Question: Which way is preferred, and why?

EDIT: Since user gnat says this question is poorly formed, I'll add that the official python packaging tutorial does not seem to comment on which of the methods described above is the preferred one. I am explicitly not giving my personal list of pros and cons because I'm interested in whether there is a community preferred method, not generating a discussion of pros/cons :)

答:
You do the simplest thing that works for you.

For a one function module, there is absolutely no point in creating a package. Packages are useful for creating an additional namespace and/or for organising your code across multiple modules.

The json and unittest modules in the Python standard library are really packages for example, for code organisation purposes. But it is perfectly fine to leave code currently living in just one python file, as one module.

If you want examples:

six is a distribution of just one module

I have released several packages that all are single module distributions as well: setuptools_subversion, formatflowed, rtfunicode.

For a good example of projects that really make excellent use of packages, look at:

requests

Flask

There is no 'official' recommendation; both options are entirely valid.

ContentLicense: CC BY-SA 4.0


问:
Quite confused about what constitutes Current state of a resource
From REST in Practice: Hypermedia and Systems Architecture:

The current state of a resource is a combination of:

The values of information items belonging to that resource

Links to related resources

Links that represent a transition to a possible future state of the current resource

The results of evaluating any business rules that relate the resource to other local resources

a) why would "links to related resources" also represent the current state of a resource?

b) I also don't quite understand why "Links that represent a transition to a possible future state of the current resource" also represent the the current state.

Namely, those links represent the possibility, not the current state. Analogy would be an int variable set to value 10. It's possible that in the future this variable will get processed and set to value 100, but we don't claim its current state also includes possible future state of 100?!

REPLY:

1)

links to related resources: imagine a "chat_room" resource. The links
  to people in that room will change as they come and go. In other
  words, a resource's relationships to other resources may change.

a) I also began learning  domain driven design and there an entity E may have child entities ( thus E is a parent in a relationship ) and E's relationship with its child entities may change ( ie some child entities may be assigned to different parent or may get deleted).

But even though E has one-to-many relationship with its child entities, we don't consider that relationship as also representing the current state of E. That's is why I'm puzzled as to why in REST the links to related resources also represent the current state?!

b)

imagine a university has a "student" resource. There might be a link
  to "register" in a class, a link to "apply for housing", and links to
  "tune in", "turn on" and "drop out". Each link may change the state of
  the object, or may yield appropriate information for the resource
  state. For example, the correct Student Housing application for that
  student.

It sort of makes sense that the state of current resource also constitute those links to related resources, which may state change the state of the current resource. But it doesn't make sense that the state of the current resource also constitute links to those related resources which don't change the state of the current resource?!

2)

But the resource may be a sort of state machine. So a "car" resource
  might have links to "start_engine" if it's stopped. It might links to
  "speed_up", "slow_down", and if the speed is slow enough,
  "stop_engine". In other words, links to transition to other valid
  states based on the current state.

a)

links to transition to other valid states based on the current state.

I assume you've made a typo and what you meant to write is "links to transition to other valid states is based on the current state"?

b) So could we say the reason why links that represent transitions to other states also represent current state of a resource is because every resource with such links could be considered a state machine, and with state machine, links to other valid states do represent current state ( I hope my ramblings make some sense )?!

2. REPLY:

1)

But it seems more natural in handling structured data to just bundle
  the "chat_room" and "chat_room_person" information together.

I assume technically, related resources themselves don't represent the current state of a resource. Instead, only links to these related resources represent the current state of a resource?!

2)

In REST, the goal is to eliminate all out-of-band communication. So we
  supplement the state (ON) with links that represent all the valid
  transitions for that state. So we want to avoid more documentation.
  The resource representation can tell us, "This is where I am, and this
  is where I can go next."

In REST the links that represent transitions to other states also represent the current state of a resource, but could we argue that logically ( thus not in the context of REST ) these links don't really represent the current state of a resource, and thus only reason why in REST they do represent the current state of a resource is because REST is build around that assumption ( or is at least more efficient that way )?

thank you

答:
links to related resources: imagine a "chat_room" resource. The links to people in that room will change as they come and go.

In other words, a resource's relationships to other resources may change.

links that represent a transition... : an example is pagination. The resource might have a "next" link that links to page 2 from page 1, page 3 from page 2, etc.

But the resource may be a sort of state machine. So a "car" resource might have links to "start_engine" if it's stopped. It might links to "speed_up", "slow_down", and if the speed is slow enough, "stop_engine".

In other words, links to transition to other valid states, based on the current state.

(That's not the most REST-ful example, but to convey the idea...)

The results of evaluating business rules: imagine a university has a "student" resource. There might be a link to "register" in a class, a link to "apply for housing", and links to "tune in", "turn on" and "drop out". Each link may change the state of the object, or may yield appropriate information for the resource state. For example, the correct Student Housing application form specifically for that student's situation.

...

But even though E has one-to-many relationship with its child entities, we don't consider that relationship as also representing the current state of E.

I think that's just a difference in methodologies. For example, in a database table, you wouldn't include records related to a table in the table itself; you'd have foreign keys in the other table, or else an association array. So, just like your example, the state of a record in a database table doesn't include references to other things (usually).

In a Resource-oriented view, the book is saying you can contain those links in the resource itself, as part of the Resource definition.

Note that you don't have to do it that way. You could still have a "chat_room" resource, a "person" resource, and a "chat_room_person" resource. But it seems more natural in handling structured data to just bundle the "chat_room" and "chat_room_person" information together.

...

Sorry, I just added a comma for clarity: "transition to other valid states, based on the current state"

...

Usually when we represent a state machine, we just say "This is the state: ON". The client needs to know what transitions are available now that the thing is ON.

In REST, the goal is to eliminate all out-of-band communication. So we supplement the state (ON) with links that represent all the valid transitions for that state.

So we want to avoid more documentation. The resource representation can tell us, "This is where I am, and this is where I can go next."

...

related resources themselves don't represent the current state of a resource. Instead, only links to these related resources represent the current state of a resource

That's tricky a tricky question, because the meaning of "Resource" in REST is so amazingly broad. The answer is, yes, if you define your resource that way. And no if you don't.

For example, the "chat_room" might have a list of links to "people" in the chat room. Or it might just include the JSON for each of those people. If your resource includes the nested data, does that mean that the data is part of the state?

It's fair to say that the state of the chat room does not include the state of the people in it. But it turns into a "One, but not the same"  kind of deal, that depends on how you define it.

...

in REST they do represent the current state of a resource is because REST is build around that assumption

I think that's a fair statement. I've never charted out the philosophical implications of that, but in practice it works out that way.

Again, the idea is to represent as much as you can, so that no out-of-band communication is needed. That's the motivation for rolling all this up into a Resource.


问:
Most Active Open Source C# Projects?
I am looking for open source projects being done in C# that are actively looking for developers and does not mind the person coming in from a C++ background. Any pointers appreciated.</p>

答: NHibernate is pretty active project, I am sure they will welcome help. Also SharpArchitecture.net project is always looking for help.</p>

ContentLicense: CC BY-SA 2.5

Title: Most Active Open Source C# Projects?


问:
How to handle mutiple API calls using javascript/jquery
I need to build a service that will call multiple API's at the same time and then output the results on the page (Think of how a price comparison site works for example).

The idea being that as each API call completes the results are sent to the browser immediately and the page would get progressively bigger until all process are complete.

Because these API calls may take several seconds each to return I would like to do this via javascript/jquery in order to create a better user experience. I have never done anything like this before using javascript/jquery so I was wondering if there was any frameworks/advice that anyone would be willing to share.

答:
That should be pretty easy, considering this is the way JavaScript HTTP requests work by default!

Unless you specifically tell XMLHttpRequest(or jQuery's $.ajax or one of it's wrappers) to be synchronous, it'll run at asynchronous mode. That means you give it a callback function, it sends the request, and then continue in the code. Only when the response returns the callback gets invoked with it - that can happen long after the JavaScript event that sent the request ended.

So, what you simply need to do is(shown in jQuery for simplicity, but the concept for pure JS is the same):

```<code>$.ajax({
        type: 'POST',//or 'GET'
        url: 'first_api_url',
        data: {/*first_api_data*/}
}).done(function(data){
        //Data contains the results from the first API.
        //Use them to update the DOM.
});

$.ajax({
        type: 'POST',//or 'GET'
        url: 'second_api_url',
        data: {/*second_api_data*/}
}).done(function(data){
        //Data contains the results from the second API
        //Use them to update the DOM.
});
</code>```

P.S. - you shouldn't count about the second call being sent before the first call's response returns. If the first call's response returns really really fast, it might interrupt in the middle. It's probably depends on the JavaScript implementation, and it's probably not going to happen in this particular code when the second call is immediately after the first, but I've encountered a nasty bug where AJAX calls return too fast and uses things before they're initialized.

Body:
That should be pretty easy, considering this is the way JavaScript HTTP requests work by default!

Unless you specifically tell XMLHttpRequest(or jQuery's $.ajax or one of it's wrappers) to be synchronous, it'll run at asynchronous mode. That means you give it a callback function, it sends the request, and then continue in the code. Only when the response returns the callback gets invoked with it - that can happen long after the JavaScript event that sent the request ended.

So, what you simply need to do is(shown in jQuery for simplicity, but the concept for pure JS is the same):

$.ajax({
        type: 'POST',//or 'GET'
        url: 'first_api_url',
        data: {/*first_api_data*/}
}).done(function(data){
        //Data contains the results from the first API.
        //Use them to update the DOM.
});

$.ajax({
        type: 'POST',//or 'GET'
        url: 'second_api_url',
        data: {/*second_api_data*/}
}).done(function(data){
        //Data contains the results from the second API
        //Use them to update the DOM.
});

P.S. - you shouldn't count about the second call being sent before the first call's response returns. If the first call's response returns really really fast, it might interrupt in the middle. It's probably depends on the JavaScript implementation, and it's probably not going to happen in this particular code when the second call is immediately after the first, but I've encountered a nasty bug where AJAX calls return too fast and uses things before they're initialized.


问:
Pure Front end JavaScript with Web API versus MVC views with ajax
This was more a discussion for what peoples thoughts are these days on how to split a web application.

I am used to creating an MVC application with all its views and controllers. I would normally create a full view and pass this back to the browser on a full page request, unless there were specific areas that I did not want to populate straight away and would then use DOM page load events to call the server to load other areas using AJAX.

Also, when it came to partial page refreshing, I would call an MVC action method which would return the HTML fragment which I could then use to populate parts of the page. This would be for areas that I did not want to slow down initial page load, or areas that fitted better with AJAX calls. One example would be for table paging. If you want to move on to the next page, I would prefer it if an AJAX call got that info rather than using a full page refresh. But the AJAX call would still return an HTML fragment.

My question is. Are my thoughts on this archaic because I come from a .net background rather than a pure front end background?

An intelligent front end developer that I work with, prefers to do more or less nothing in the MVC views, and would rather do everything on the front end. Right down to web API calls populating the page. So that rather than calling an MVC action method, which returns HTML, he would prefer to return a standard object and use javascript to create all the elements of the page.

The front end developer way means that any benefits that I normally get with MVC model validation, including client side validation, would be gone. It also means that any benefits that I get with creating the views, with strongly typed html templates etc would be gone.

I believe this would mean I would need to write the same validation for front end and back end validation. The javascript would also need to have lots of methods for creating all the different parts of the DOM. For example, when adding a new row to a table, I would normally use the MVC partial view for creating the row, and then return this as part of the AJAX call, which then gets injected into the table. By using a pure front end way, the javascript would would take in an object (for, say, a product) for the row from the api call, and then create a row from that object. Creating each individual part of the table row.

The website in question will have lots of different areas, from administration, forms, product searching etc. A website that I don't think requires to be architected in a single page application way.

What are everyone's thoughts on this?

I am interested to hear from front end devs and back end devs.

答:
I'm also somewhat skeptical that every new web app needs to be an SPA but one thing I'm 100% sold on as a generalist with the bulk of his experience on the client side is that a service-oriented architecture that hands off raw data rather than HTML to the client is the way to go whether you're loading prebuilt pages/views from the server and doing a lot of dynamic stuff with data after page load or building almost everything 100% with JavaScript.

The reasons this is preferable to a client-side dev are much the same as the reasons nobody wants HTML in the database. What's the client-side dev supposed to do when they want to resort a table for instance when they've been handed HTML? The performance cost of handling all that on the client is trivial compared to making another server request to do that for you. Also, HTML-building is pretty well-covered in JS-land. Sorting data and building new HTML table rows out of it is pretty trivial work for an experienced client-side dev.

And of what use is the back end architecture to a front end for another device that might need to do something exotic like implement widgets that are 100% canvas or a totally different HTML structure? Why should a client-side dev have to load up visual studio or knock on the back end devs door to make a strictly presentational tweak?

As for your concerns about the loss of strongly typed template validation, trust me when I say that if you're dealing with a competent client-side dev, you will find no .NET framework or visual studio tool that is more coal-to-diamond-to-dust-crushingly anal about well-formed, valid HTML than s/he is.

From the full-stack perspective, I like it because it means I'll never have to fish for business or app logic some yutz decided to drop into the templating layer. Not to mention the per-user load it takes off of your servers while in many cases actually improving load experience for the user in modern browsers with modern computers.

I think it's also easier to reason about the back end architecture when you've fully isolated it from all the presentation stuff. You're no longer grabbing data to mash it into some HTML. You're pulling it together to create an implementation-independent data structure that concerns itself more with general use than what's going to be done with it on the other side. IMO, that tends to lead to more consistency in how things are handled since the data is now an end goal rather than the second to last step of a process and there's fewer opportunities for unrelated concerns to get their wires crossed.


问:
How to simulate inner join on very large files in java (without running out of memory)
I am trying to simulate SQL joins using java and very large text files (INNER, RIGHT OUTER and LEFT OUTER). The files have already been sorted using an external sort routine.

The issue I have is I am trying to find the most efficient way to deal with the INNER join part of the algorithm. Right now I am using two Lists to store the lines that have the same key and iterate through the set of lines in the right file once for every line in the left file (provided the keys still match). In other words, the join key is not unique in each file so would need to account for the Cartesian product situations ...

```<code>left_01, 1
left_02, 1
right_01, 1
right_02, 1
right_03, 1

left_01 joins to right_01 using key 1
left_01 joins to right_02 using key 1
left_01 joins to right_03 using key 1
left_02 joins to right_01 using key 1
left_02 joins to right_02 using key 1
left_02 joins to right_03 using key 1
</code>```

My concern is one of memory. I will run out of memory if i use the approach below but still want the inner join part to work fairly quickly. What is the best approach to deal with the INNER join part keeping in mind that these files may potentially be huge

```<code>public class Joiner {

  private void join(BufferedReader left, BufferedReader right, BufferedWriter output) throws Throwable {

    BufferedReader _left = left;
    BufferedReader _right = right;
    BufferedWriter _output = output;
    Record _leftRecord;
    Record _rightRecord;

    _leftRecord = read(_left);
    _rightRecord = read(_right);

    while( _leftRecord != null && _rightRecord != null ) {

      if( _leftRecord.getKey() < _rightRecord.getKey() ) {
        write(_output, _leftRecord, null);
        _leftRecord = read(_left);
      }
      else if( _leftRecord.getKey() > _rightRecord.getKey() ) {
        write(_output, null, _rightRecord);
        _rightRecord = read(_right);
      }
      else {
        List<Record> leftList = new ArrayList<Record>();
        List<Record> rightList = new ArrayList<Record>();

        _leftRecord = readRecords(leftList, _leftRecord, _left);
        _rightRecord = readRecords(rightList, _rightRecord, _right);

        for( Record equalKeyLeftRecord : leftList ){
          for( Record equalKeyRightRecord : rightList ){
            write(_output, equalKeyLeftRecord, equalKeyRightRecord);
          }
        }
      }
    }

    if( _leftRecord != null ) {
      write(_output, _leftRecord, null);
      _leftRecord = read(_left);
      while(_leftRecord != null) {
        write(_output, _leftRecord, null);
        _leftRecord = read(_left);
      }
    }
    else {
      if( _rightRecord != null ) {
        write(_output, null, _rightRecord);
        _rightRecord = read(_right);
        while(_rightRecord != null) {
          write(_output, null, _rightRecord);
          _rightRecord = read(_right);
        }
      }
    }
    _left.close();
    _right.close();
    _output.flush();
    _output.close();
  }

  private Record read(BufferedReader reader) throws Throwable {
    Record record = null;

    String data = reader.readLine();
    if( data != null ) {
      record = new Record(data.split("\t"));
    }
    return record;
  }

  private Record readRecords(List<Record> list, Record record, BufferedReader reader) throws Throwable {
    int key = record.getKey();
    list.add(record);
    record = read(reader);
    while( record != null && record.getKey() == key) {
      list.add(record);
      record = read(reader);
    }
    return record;
  }

  private void write(BufferedWriter writer, Record left, Record right) throws Throwable {
    String leftKey = (left == null ? "null" : Integer.toString(left.getKey()));
    String leftData = (left == null ? "null" : left.getData());
    String rightKey = (right == null ? "null" : Integer.toString(right.getKey()));
    String rightData = (right == null ? "null" : right.getData());

    writer.write("[" + leftKey + "][" + leftData + "][" + rightKey + "][" + rightData + "]\n");
  }

  public static void main(String[] args) {
        try {

          BufferedReader leftReader = new BufferedReader(new FileReader("LEFT.DAT"));
          BufferedReader rightReader = new BufferedReader(new FileReader("RIGHT.DAT"));
          BufferedWriter output = new BufferedWriter(new FileWriter("OUTPUT.DAT"));

      Joiner joiner = new Joiner();
      joiner.join(leftReader, rightReader, output);
    }
        catch (Throwable e) {
      e.printStackTrace();
    }
  }
}
</code>```

After applying the ideas from the proposed answer, I changed the loop to this

```<code>private void join(RandomAccessFile left, RandomAccessFile right, BufferedWriter output) throws Throwable {

    long _pointer = 0;

    RandomAccessFile _left = left;
    RandomAccessFile _right = right;

    BufferedWriter _output = output;

    Record _leftRecord;
    Record _rightRecord;

    _leftRecord = read(_left);
    _rightRecord = read(_right);

    while( _leftRecord != null && _rightRecord != null ) {

        if( _leftRecord.getKey() < _rightRecord.getKey() ) {
            write(_output, _leftRecord, null);
            _leftRecord = read(_left);
        }
        else if( _leftRecord.getKey() > _rightRecord.getKey() ) {
            write(_output, null, _rightRecord);
            _pointer = _right.getFilePointer();
            _rightRecord = read(_right);
        }
        else {
            long _tempPointer = 0;
            int key = _leftRecord.getKey();
            while( _leftRecord != null && _leftRecord.getKey() == key ) {
                _right.seek(_pointer);
                _rightRecord = read(_right);
                while( _rightRecord != null && _rightRecord.getKey() == key ) {
                    write(_output, _leftRecord, _rightRecord );
                    _tempPointer = _right.getFilePointer();
                    _rightRecord = read(_right);
                }
                _leftRecord = read(_left);
            }
            _pointer = _tempPointer;
        }
    }

    if( _leftRecord != null ) {
        write(_output, _leftRecord, null);
        _leftRecord = read(_left);
        while(_leftRecord != null) {
            write(_output, _leftRecord, null);
            _leftRecord = read(_left);
        }
    }
    else {
        if( _rightRecord != null ) {
            write(_output, null, _rightRecord);
            _rightRecord = read(_right);
            while(_rightRecord != null) {
                write(_output, null, _rightRecord);
                _rightRecord = read(_right);
            }
        }
    }
    _left.close();
    _right.close();
    _output.flush();
    _output.close();
}
</code>```

UPDATE
While this approach worked, it was terribly slow and so I have modified this to create files as buffers and this works very well. Here is the update ...

```<code>private long getMaxBufferedLines(File file) throws Throwable {
    long freeBytes = Runtime.getRuntime().freeMemory() / 2;
    return (freeBytes / (file.length() / getLineCount(file)));
}

private void join(File left, File right, File output, JoinType joinType) throws Throwable {

    BufferedReader leftFile = new BufferedReader(new FileReader(left));
    BufferedReader rightFile = new BufferedReader(new FileReader(right));
    BufferedWriter outputFile = new BufferedWriter(new FileWriter(output));
    long maxBufferedLines = getMaxBufferedLines(right);

    Record leftRecord;
    Record rightRecord;

    leftRecord = read(leftFile);
    rightRecord = read(rightFile);

    while( leftRecord != null && rightRecord != null ) {

        if( leftRecord.getKey().compareTo(rightRecord.getKey()) < 0) {
            if( joinType == JoinType.LeftOuterJoin
                    || joinType == JoinType.LeftExclusiveJoin
                    || joinType == JoinType.FullExclusiveJoin
                    || joinType == JoinType.FullOuterJoin ) {
                write(outputFile, leftRecord, null);
            }
            leftRecord = read(leftFile);
        }
        else if( leftRecord.getKey().compareTo(rightRecord.getKey()) > 0 ) {
            if( joinType == JoinType.RightOuterJoin
                    || joinType == JoinType.RightExclusiveJoin
                    || joinType == JoinType.FullExclusiveJoin
                    || joinType == JoinType.FullOuterJoin ) {
                write(outputFile, null, rightRecord);
            }
            rightRecord = read(rightFile);
        }
        else if( leftRecord.getKey().compareTo(rightRecord.getKey()) == 0 ) {
            String key = leftRecord.getKey();
            List<File> rightRecordFileList = new ArrayList<File>();
            List<Record> rightRecordList = new ArrayList<Record>();
            rightRecordList.add(rightRecord);
            rightRecord = consume(key, rightFile, rightRecordList, rightRecordFileList, maxBufferedLines);

            while( leftRecord != null && leftRecord.getKey().compareTo(key) == 0 ) {
                processRightRecords(outputFile, leftRecord, rightRecordFileList, rightRecordList, joinType);
                leftRecord = read(leftFile);
            }

            // need a dispose for deleting files in list
        }
        else {
            throw new Exception("DATA IS NOT SORTED");
        }
    }

    if( leftRecord != null ) {
        if( joinType == JoinType.LeftOuterJoin
                || joinType == JoinType.LeftExclusiveJoin
                || joinType == JoinType.FullExclusiveJoin
                || joinType == JoinType.FullOuterJoin ) {
            write(outputFile, leftRecord, null);
        }
        leftRecord = read(leftFile);
        while(leftRecord != null) {
            if( joinType == JoinType.LeftOuterJoin
                    || joinType == JoinType.LeftExclusiveJoin
                    || joinType == JoinType.FullExclusiveJoin
                    || joinType == JoinType.FullOuterJoin ) {
                write(outputFile, leftRecord, null);
            }
            leftRecord = read(leftFile);
        }
    }
    else {
        if( rightRecord != null ) {
            if( joinType == JoinType.RightOuterJoin
                    || joinType == JoinType.RightExclusiveJoin
                    || joinType == JoinType.FullExclusiveJoin
                    || joinType == JoinType.FullOuterJoin ) {
                write(outputFile, null, rightRecord);
            }
            rightRecord = read(rightFile);
            while(rightRecord != null) {
                if( joinType == JoinType.RightOuterJoin
                        || joinType == JoinType.RightExclusiveJoin
                        || joinType == JoinType.FullExclusiveJoin
                        || joinType == JoinType.FullOuterJoin ) {
                    write(outputFile, null, rightRecord);
                }
                rightRecord = read(rightFile);
            }
        }
    }
    leftFile.close();
    rightFile.close();
    outputFile.flush();
    outputFile.close();
}

public void processRightRecords(BufferedWriter outputFile, Record leftRecord, List<File> rightFiles, List<Record> rightRecords, JoinType joinType) throws Throwable {

    for(File rightFile : rightFiles) {
        BufferedReader rightReader = new BufferedReader(new FileReader(rightFile));
        Record rightRecord = read(rightReader);
        while(rightRecord != null){

            if( joinType == JoinType.LeftOuterJoin
                    || joinType == JoinType.RightOuterJoin
                    || joinType == JoinType.FullOuterJoin
                    || joinType == JoinType.InnerJoin ) {

                write(outputFile, leftRecord, rightRecord);
            }
            rightRecord = read(rightReader);
        }
        rightReader.close();
    }

    for(Record rightRecord : rightRecords) {
        if( joinType == JoinType.LeftOuterJoin
                || joinType == JoinType.RightOuterJoin
                || joinType == JoinType.FullOuterJoin
                || joinType == JoinType.InnerJoin ) {

            write(outputFile, leftRecord, rightRecord);
        }
    }
}

/**
 * consume all records having key (either to a single list or multiple files) each file will
 * store a buffer full of data. The right record returned represents the outside flow (key is
 * already positioned to next one or null) so we can't use this record in below while loop or
 * within this block in general when comparing current key. The trick is to keep consuming
 * from a List. When it becomes empty, re-fill it from the next file until all files have
 * been consumed (and the last node in the list is read). The next outside iteration will be
 * ready to be processed (either it will be null or it points to the next biggest key
 * @throws Throwable
 *
 */
private Record consume(String key, BufferedReader reader, List<Record> records, List<File> files, long bufferMaxRecordLines ) throws Throwable {
    boolean processComplete = false;
    Record record = records.get(records.size() - 1);

    while(!processComplete){
        long recordCount = records.size();
        if( record.getKey().compareTo(key) == 0 ){
            record = read(reader);
            while( record != null && record.getKey().compareTo(key) == 0 && recordCount < bufferMaxRecordLines ) {
                records.add(record);
                recordCount++;
                record = read(reader);
            }
        }
        processComplete = true;
        // if record is null, we are done
        if( record != null ) {

            // if the key has changed, we are done
            if( record.getKey().compareTo(key) == 0 ) {

                // Same key means we have exhausted the buffer.
                // Dump entire buffer into a file. The list of file
                // pointers will keep track of the files ...
                processComplete = false;
                dumpBufferToFile(records, files);
                records.clear();
                records.add(record);
            }
        }
    }
    return record;
}

/**
 * Dump all records in List of Record objects to a file. Then, add that
 * file to List of File objects
 *
 * NEED TO PLACE A LIMIT ON NUMBER OF FILE POINTERS (check size of file list)
 *
 * @param records
 * @param files
 * @throws Throwable
 */
private void dumpBufferToFile(List<Record> records, List<File> files) throws Throwable {
    String prefix = "joiner_" + files.size() + 1;
    String suffix = ".dat";
    File file = File.createTempFile(prefix, suffix, new File("cache"));
    BufferedWriter writer = new BufferedWriter(new FileWriter(file));

    for( Record record : records ) {
        writer.write( record.dump() );
    }

    files.add(file);
    writer.flush();
    writer.close();
}
</code>```

答:
Why would you want to use "File Cache"?

Caching is supposed to be used when you have some layer with faster accessing ability to reduce time on the slower layers.

So if you read files through network, then caching via file system makes sense because local file system will be faster than network. If you are working on file system, then the faster level will be memory, and BufferedReader will handle memory caching for you. You can write your own wrapper CustomBufferredReader for BufferedReader to control more precisely how many records you want to read in advance and put in memory. To do this properly you need to research on the hardware/disk you are using to figure out how to best prefetch the records.

In your case, you are caching file system operations on file system operations. Doesn't make sense to me. I would not call this caching at all. This would help you only when you are caching intermediate work (like when you do dynamic programming), but it is not the case because the records have been sorted externally via external sorting as you have mentioned. There is no intermediate work.

And you mention cartesian product in the inner join part, if the records are all sorted by external sorting, there is no need to worry about cartesian products, you simply do zig-zag linear scan between the left record list and the right recrod list.


问:
Static / Shared Helper Functions vs Built-In Methods
This is a simple question but a design consideration that I often run across in my day to day development work.  Lets say that you have a class that represents some kinds of collection.

```<code>Public Class ModifiedCustomerOrders
     Public Property Orders as List(Of ModifiedOrders)
End Class
</code>```

Within this class you do all kinds of important work, such as combining many different information sources and, eventually, build the Modified Customer Orders.

Now, you have different processes that consume this class, each of which needs a slightly different slice of the ModifiedCustomerOrders items.  To enable this, you want to add filtering functionality. How do you go about this?  Do you:

Add Filtering calls to the ModifiedCustomerOrders class so that you can say:

MyOrdersClass.RemoveCanceledOrders()

Create a Static / Shared "tooling" class that allows you to call:

OrdersFilters.RemoveCanceledOrders(MyOrders)

Create an extension method to accomplish the same feat as #2 but with less typing:

MyOrders.RemoveCanceledOrders()

Create a "Service" method that handles the getting of Orders as appropriate to the calling function, while using one of the previous approaches "under the hood".

OrdersService.GetOrdersForProcessA()

Others?

I tend to prefer the tooling / extension method approaches as they make testing  a little bit simpler.  Although I dependency inject all my sourcing data into the ModifiedCustomerOrders, having it as part of the class makes it a little bit more complicated to test.  Typically, I choose to use extension methods where I am doing parameterless transformations / filters.  As they get more complex, I will move it into a static class instead.

Thoughts on this approach?  How would you approach it?

答:
I would let the user of that Data object do the work.

The data object should remain simple and generic enough to make the whole things more manageable.

If some class needs certain slice of the data object, it is the job of that class to customize and manipulate that data. If you put that responsibility in the data object class, you implicitly introduce a coupling in the direction of the data object to its user classes when you don't really need to. Doesn't seem like a good idea to me.

Having said that, there are cases when you need to provide customized operation on the data object class. These are the cases where you can put in extra tricks and optimization for that particular operation. Users of the data object won't have the chance to do this since they don't have access to internal structure of the data object.


问:
Where should instantiated classes be stored?
I'm having a bit of a design dilemma here. I'm writing a library that consists of a bunch of template classes that are designed to be used as a base for creating content. For example:

```<code>public class Template
{
    public string Name {get; set;}
    public string Description {get; set;}
    public string Attribute1 {get; set;}
    public string Attribute2 {get; set;}

    public Template()
    {
        //constructor
    }

    public void DoSomething()
    {
        //does something
    }
    ...
}
</code>```

The problem is, not only is the library providing the templates, it will also supply quite a few predefined templates which are instances of these template classes. The question is, where do I put these instances of the templates?

The three solutions I've come up with so far are:

1) Provide serialized instances of the templates as files.

On the one hand, this solution would keep the instances separated from the library itself, which is nice, but it would also potentially add complexity for the user. Even if we provided methods for loading/deserializing the files, they'd still have to deal with a bunch of files, and some kind of config file so the app knows where to look for those files. Plus, creating the template files would probably require a separate app, so if the user wanted to stick with the files method of storing templates, we'd have to provide some kind of app for creating the template files.

Also, this requires external dependencies for testing the templates in the user's code.

2) Add readonly instances to the template class

Example:

```<code>public class Template
{
    public string Name {get; set;}
    public string Description {get; set;}
    public string Attribute1 {get; set;}
    public string Attribute2 {get; set;}

    public Template PredefinedTemplate
    {
        get
        {
            Template templateInstance = new Template();
            templateInstance.Name = "Some Name";
            templateInstance.Description = "A description";
            ...
            return templateInstance;
        }
    }

    public Template()
    {
        //constructor
    }

    public void DoSomething()
    {
        //does something
    }
    ...
}
</code>```

This method would be convenient for users, as they would be able to access the predefined templates in code directly, and would be able to unit test code that used them. The drawback here is that the predefined templates pollute the Template type namespace with a bunch of extra stuff. I suppose I could put the predefined templates in a different namespace to get around this drawback. The only other problem with this approach is that I'd have to basically duplicate all the namespaces in the library in the predefined namespace (e.g. Templates.SubTemplates and Predefined.Templates.SubTemplates) which would be a pain, and would also make refactoring more difficult.

3) Make the templates abstract classes and make the predefined templates inherit from those classes.

For example:

```<code>public abstract class Template
{
    public string Name {get; set;}
    public string Description {get; set;}
    public string Attribute1 {get; set;}
    public string Attribute2 {get; set;}

    public Template()
    {
        //constructor
    }

    public void DoSomething()
    {
        //does something
    }
    ...
}
</code>```

and

```<code>public class PredefinedTemplate : Template
{
    public PredefinedTemplate()
    {
        this.Name = "Some Name";
        this.Description = "A description";
        this.Attribute1 = "Some Value";
        ...
    }
}
</code>```

This solution is pretty similar to #2, but it ends up creating a lot of classes that don't really do anything (none of our predefined templates are currently overriding behavior), and don't have any methods, so I'm not sure how good a practice this is.

Has anyone else had any experience with something like this? Is there a best practice of some kind, or a different/better approach that I haven't thought of? I'm kind of banging my head against a wall trying to figure out the best way to go.

Thanks!

答:
It looks like you are not using Java but in Java you can simply use this

```<code>public class Template{
   public static Template predefined1 = new Template("a", "b");
   public static Template predefined2 = new Template("c", "d");

   public Template(String a, String b) {}
}
</code>```

Very simple and doesn't deal with any of the design pattern at all.
Or you want something else with those templates?

Body:
It looks like you are not using Java but in Java you can simply use this

public class Template{
   public static Template predefined1 = new Template("a", "b");
   public static Template predefined2 = new Template("c", "d");

   public Template(String a, String b) {}
}

Very simple and doesn't deal with any of the design pattern at all.
Or you want something else with those templates?


问:
How does dependecy injection increase coupling?
On the Wikipedia page on dependency injection, the disadvantages section tells us this:

Dependency injection increases coupling by requiring the user of a subsystem to provide for the needs of that subsystem.

with a link to an article against dependency injection.

Dependency injection makes a class use the interface instead of the concrete implementation. That should result in decreased coupling, no?

What am I missing? How is dependency injection increasing coupling between classes?

答:
I strongly disagree that it increases coupling.

Without dependency injection you have tight coupling between a sub system and the concrete implementation of the dependency.

With dependency injection you have decoupled the sub system from the implementation of the dependency.

Making the argument that it increases coupling between the consumer and this sub system is VERY questionable as it implies that the consumer is now tightly coupled to the dependency required by the sub system. All it means is you are writing tightly coupled code that couples your consumer to the dependency. Ideally ALL your code is decoupled.

Constructor Injection:

Dependency resolution is handled by a dependency injection container or a factory. The consumer can get a concrete implementation of the sub system from the dependency injection container or a factory.

The consumer does not need to know what the constructor of the sub system even looks like. There is no coupling to the sub system dependency.

Method Injection:

Same as constructor injection except that now the consumer needs to get a concrete instance of the dependency from the container or factory (or even have it method/constructor injected) and inject it into the method. Again, the consumer is not coupled to a concrete implementation of the dependency.

TL;DR
Worst case for dependency injection in a sub system is that the coupling is shifted to consumer code. THERE IS NO OVERALL INCREASE IN COUPLING.

Best case is that all systems are now loosely coupled and dependency injection is controlled through dependency injection containers or factories.


问:
UML Class Diagram: Abstract or Interface?
I am modeling a class diagram and have spotted an opportunity to simplify it slightly. What I want to know is, would this it be better to implement an abstract class or an interface?

The scenario is this, I have the classes:

Artist

Genre

Album

Song

All of which share the methods getName, setName, and getCount (playcount that is). Would it be best to create an abstract 'Music' class with the aforementioned abstract methods, or should I create an interface, since the classes that implement the interface have to include all of the interface's methods (I think, correct me if I'm wrong).

I hope I've given enough detail, please ask questions if I haven't.

Thanks!

答:
I would use interfaces instead.
From my experience, there are 2 reasons for this:

Only use (abstract) class at the root of the diagram when you are so confident that sub-classes are very very similar

Unless you are very fluent in the area, will you need to make lot of modifications to not just your code but even your design too. This is when interfaces will be much more suitable, changing around the interface inheritance tree is much less disruptive than dealing with abstract classes.

Think about the Venn diagram and two not-equal sets of functionalities. A and B. In java you can only extend one abstract class, which one should you choose?
If you choose A, some time in the future, the need arises that you didn't anticipate so B inflates to be bigger than A. You will need to change to B and then something that you didn't anticipated arises and you change back to A.

Is it better to just use interfaces and then when the code base is stable, you refactor and common up the same functionalities in an abstract class.
Interfaces are much more agile and suitable for those constant changes. Of course if you already know way too well what will be anticipated, then by all means go with abstract classes.


问:
Significant amount of the time, I can't think of a reason to have an object instead of a static class. Do objects have more benefits than I think?
I understand the concept of an object, and as a Java programmer I feel the OO paradigm comes rather naturally to me in practice.

However recently I found myself thinking:

Wait a second, what are actually the practical benefits of using an object over using a static class (with proper encapsulation and OO practices)?

I could think of two benefits of using an object (both are significant and powerful):

Polymorphism: allows you to swap functionality dynamically and
flexibly during runtime. Also allows to add new functionality
'parts' and alternatives to the system easily. For example if
there's a Car class designed to work with Engine objects, and you
want to add a new Engine to the system that the Car can use, you can create a new
Engine subclass and simply pass an object of this class into the
Car object, without having to change anything about Car. And you can decide to do so during runtime.

Being able to 'pass functionality around': you can pass an object
around the system dynamically.

But are there any more advantages to objects over static classes?

Often when I add new 'parts' to a system, I do so by creating a new class and instantiating objects from it.

But recently when I stopped and thought about it, I realized that a static class would do just the same as an object, in a lot of the places where I normally use an object.

For example, I'm working on adding a save/load-file mechanism to my app.

With an object, the calling line of code will look like this: Thing thing = fileLoader.load(file);

With a static class, it would look like this: Thing thing = FileLoader.load(file);

What's the difference?

Fairly often I just can't think of a reason to instantiate an object when a plain-old static-class would act just the same. But in OO systems, static classes are fairly rare. So I must be missing something.

Are there any more advantages to objects other from the two that I listed? Please explain.

EDIT: To clarify. I do find objects very useful when swapping functionality, or passing data around. For example I wrote an app that makes up melodies. MelodyGenerator had several subclasses that create melodies differently, and objects of these classes were interchangable (Strategy pattern).

The melodies were objects too, since it's useful to pass them around. So were the Chords and Scales.

But what about 'static' parts of the system - that aren't going to be passed around? For example - a 'save file' mechanism. Why should I implement it in an object, and not a static class?

答:
A major issue when using static classes is that they force you to hide your dependencies, and force you to depend on implementations. In the following constructor signature, what are your dependencies:

```<code>public Person(String name)
</code>```

Well, judging from the signature, a person just needs a name, right? Well, not if the implementation is:

```<code>public Person(String name) {
    ResultSet rs = DBConnection.getPersonFilePathByName(name);
    File f = FileLoader.load(rs.getPath());
    PersonData.setDataFile(f);
    this.name = name;
    this.age = PersonData.getAge();
}
</code>```

So a person isn't just instantiated. We actually pull data from a database, which gives us a path to a file, which then must be parsed, and then the real data we want has to be teased out. This example is clearly over the top, but it proves the point. But wait, there can be so much more! I write the following test:

```<code>public void testPersonConstructor() {
    Person p = new Person("Milhouse van Houten");
    assertEqual(10, p.age);
}
</code>```

This should pass though, right? I mean, we encapsulated all that other stuff, right? Well, actually it blows up with an exception. Why? Oh, yes, the hidden dependencies we didn't know about have global state. The DBConnection needs initialized and connected. The FileLoader needs initialized with a FileFormat object (like XMLFileFormat or CSVFileFormat). Never heard of those? Well, that's the point. Your code (and your compiler) can't tell you that you need these things because the static calls hide those dependencies. Did I say test? I meant that the new junior developer just shipped something like this in your most recent release. After all, compiled = works, right?

Additionally, say you're on a system that doesn't have a MySQL instance running. Or, say you are on a Windows system but your DBConnection class only goes out to your MySQL server on a Linux box (with Linux paths). Or, say you are on a system where the path returned by the DBConnection is not read/write for you. That means that trying to run or test this system under any of these conditions will fail, not because of a code error, but because of a design error that limits the flexibility of the code and ties you to an implementation.

Now, let's say, we want to log every call to the database for one specific Person instance, one that goes through a certain, troublesome path. We could put logging into DBConnection, but this will log everything, putting a lot of clutter in and making it hard to distinguish the particular code path we want to trace. If, however, we are using dependency injection with a DBConnection instance, we could simply implement the interface in a decorator (or extend the class, as we have both options available with an object). With a static class, we cannot inject the dependency, we cannot implement an interface, we cannot wrap it in a decorator, and we cannot extend the class. We are only able to call it directly, somewhere hidden deep in our code. Thus we are forced to have a hidden dependency on an implementation.

Is this always bad? Not necessarily, but it might be better to reverse your point of view and say "Is there a good reason this should not be an instance?" rather than "Is there a good reason this should be an instance?" If you can truly say that your code will be as unwavering (and stateless) as Math.abs(), both in its implementation and in the manner it will be used, then you can consider making it static. Having instances over static classes, though, gives you a world of flexibility that is not always easy to recapture after the fact. It also can give you more clarity into the true nature of your code's dependencies.

Body:
A major issue when using static classes is that they force you to hide your dependencies, and force you to depend on implementations. In the following constructor signature, what are your dependencies:

public Person(String name)

Well, judging from the signature, a person just needs a name, right? Well, not if the implementation is:

public Person(String name) {
    ResultSet rs = DBConnection.getPersonFilePathByName(name);
    File f = FileLoader.load(rs.getPath());
    PersonData.setDataFile(f);
    this.name = name;
    this.age = PersonData.getAge();
}

So a person isn't just instantiated. We actually pull data from a database, which gives us a path to a file, which then must be parsed, and then the real data we want has to be teased out. This example is clearly over the top, but it proves the point. But wait, there can be so much more! I write the following test:

public void testPersonConstructor() {
    Person p = new Person("Milhouse van Houten");
    assertEqual(10, p.age);
}

This should pass though, right? I mean, we encapsulated all that other stuff, right? Well, actually it blows up with an exception. Why? Oh, yes, the hidden dependencies we didn't know about have global state. The DBConnection needs initialized and connected. The FileLoader needs initialized with a FileFormat object (like XMLFileFormat or CSVFileFormat). Never heard of those? Well, that's the point. Your code (and your compiler) can't tell you that you need these things because the static calls hide those dependencies. Did I say test? I meant that the new junior developer just shipped something like this in your most recent release. After all, compiled = works, right?

Additionally, say you're on a system that doesn't have a MySQL instance running. Or, say you are on a Windows system but your DBConnection class only goes out to your MySQL server on a Linux box (with Linux paths). Or, say you are on a system where the path returned by the DBConnection is not read/write for you. That means that trying to run or test this system under any of these conditions will fail, not because of a code error, but because of a design error that limits the flexibility of the code and ties you to an implementation.

Now, let's say, we want to log every call to the database for one specific Person instance, one that goes through a certain, troublesome path. We could put logging into DBConnection, but this will log everything, putting a lot of clutter in and making it hard to distinguish the particular code path we want to trace. If, however, we are using dependency injection with a DBConnection instance, we could simply implement the interface in a decorator (or extend the class, as we have both options available with an object). With a static class, we cannot inject the dependency, we cannot implement an interface, we cannot wrap it in a decorator, and we cannot extend the class. We are only able to call it directly, somewhere hidden deep in our code. Thus we are forced to have a hidden dependency on an implementation.

Is this always bad? Not necessarily, but it might be better to reverse your point of view and say "Is there a good reason this should not be an instance?" rather than "Is there a good reason this should be an instance?" If you can truly say that your code will be as unwavering (and stateless) as Math.abs(), both in its implementation and in the manner it will be used, then you can consider making it static. Having instances over static classes, though, gives you a world of flexibility that is not always easy to recapture after the fact. It also can give you more clarity into the true nature of your code's dependencies.


问:
Minimizing Average Pairwise Squared Euclidean Norm
I'm pretty new to programming and computer science, but I was wondering if there was any good way to approach the following problem: For a set of 2n points (not necessarily unique) in R^k (assume each entry is positive), I want an algorithm which will partition the set into n pairs such that the average Euclidean distance squared is minimized.

EDIT: Edited for clarity.

答:
Make it work: try every possible pairing through brute-force backtracking (recursive implementation).

Make sure you are not testing the same pairing more than once (there are some pitfalls there). And try to derive your algorithms complexity. I think it is 2k(n!).

Now you can think about more clever solutions, and you have a reference implementation to check correctness and speed improvement.

By comparing with your best-solution-so-far, you can skip entire parts of your search space (=pruning). Eg. If you have an incomplete pairing, and the sum of square distances is already higher than your best score, don't try to compete this pairing. With some precalculations (like finding the nearest m neighbors for every point) you change the search order so that you find a reasonable solution early. This will drastically increase the pruning and speed up the implementation.

Or, try something completely different: Start with any complete pairing, and then look for local optimizations: if you can improve 2 pairs (AB & CD) by switching their partners (either AC & BD or AD & BC), then you have improved the total solution too. You repeat this, until you can't find such a pair or pairs anymore.  (EDIT: Actually, this approach is not guaranteed to find the best solution. It can get 'stuck' in a local minimum).


问:
Tournament bracket method to put distance between teammates
I am using a proper binary tree to simulate a tournament bracket. It's preferred any competitors in the bracket that are teammates don't meet each other until the later rounds.  What is an efficient method in which I can ensure that teammates in the bracket have as much distance as possible from each other?  Are there any other data structures besides a tree that would be better for this purpose?

EDIT: There can be more than 2 teams represented in a bracket and there is no requirement to have an equal number of people from each team in a bracket.  I intend to use this for individual sports where a person still has an affiliation with a team and we want to delay teammates facing each other until as late as possible.

答:
I hope this has a proof built into itself.

The main idea is to delay the meeting of 2 teammates as much as possible.
So a greedy algorithm would work.

The most we can delay is in the final, then semi-final1, semi-final2, quarter-final1, etc... Save this ordered list of match-up in list L1.

Sort the list of players with same team all together at the beginning of the list. This is list L2

Pop a match-up from L1, pop out 2 players in the same team from L2, fill the players into any leaves of the empty branches of the match up just got from L1. [correction: after the first iteration of the loop, each match-up will only have one branch unfilled (with players of the same team), so we need to pop another match-up to fill the second player]. This will ensure maximum distance between 2 teammates.

Continue with new match-up until the match-up is the first round. Now we can no longer delay, so fill all players in any arbitrary way. This is quite abstract, but I am sure it has enough details to implement


答:
You are looking for a minimum weight perfect matching in a complete weighted undirected graph. Your 2n points are the vertices, and each pair of vertices has an undirected edge between them with weight equal to the Euclidean distance between the points.

The Blossom Algorithm can solve this problem. According to (Kolmogorov, 2009) linked from the Wikipedia page, the best known complexity bound is O(|V||E| + |V| log |V|), which in your case would be O(n^3) because it's a complete graph.

Reference: Kolmogorov, 2009. Blossom V: a new implementation of a minimum cost perfect matching algorithm. Mathematical Programming Computation, vol. 1, issue 1, pp. 43-67.

Title: Minimizing Average Pairwise Squared Euclidean Norm

Body:
I'm pretty new to programming and computer science, but I was wondering if there was any good way to approach the following problem: For a set of 2n points (not necessarily unique) in R^k (assume each entry is positive), I want an algorithm which will partition the set into n pairs such that the average Euclidean distance squared is minimized.

EDIT: Edited for clarity.


问:
Algorithmic Forecasting and Pattern Recognition
Say a user could enter project data into my software. Each project has 2 variables "size" and "work" and they're related but the relationship is not known. Is there a way to programmatically determine the relationship between the variables based on previous data and forecast the amount of work provided if only given the size of the project in the future?

For Example, say the user had manually entered the following projects.

Project 1 - Size:1, Work: 4

Project 2 - Size:2, Work: 7

Project 3 - Size:3, Work: 10

Project 4 - Size:4, Work: x

What should I look into to be able to programmatically determine, that Work = Size*3+1 and therefor be able to say that x=13?

答:
This is called regression analysis. In your example, you're looking for a linear relationship between Work and Size,

Work = a * Size + b + noise

Given a set of (Work, Size) observations, you can solve for a and b using ordinary least squares. This method can handle any number of explanatory variables (in addition to Size).

More sophisticated approaches to this type of problem fall under the umbrella of machine learning.


问:
Estimating file transfer time over network?
I am transferring file from one server to another. So, to estimate the time it would take to transfer some GB's of file over the network, I am pinging to that IP and taking the average time.

For ex: i ping to 172.26.26.36 I get the average round trip time to be x ms, since ping send 32 bytes of data each time. I estimate speed of network to be 2*32*8(bits)/x = y Mbps --> multiplication with 2 because its average round trip time.

So transferring 5GB of data will take 5000/y seconds

Am I correct in my method of estimating time.

If you find any mistake or any other good method please share.

答:
Latency and bandwidth are largely orthogonal things.

They do relate in that mechanisms like TCP window sizes and latency put an upper ceiling on the effective bandwidth for long-haul traffic.

The only way you can effectively determine throughput for transfer on a connection is to transfer on a connection and measure it. From the measurements you can estimate the time it would take if conditions remain the same.


答:
Short answer: your method is wrong.

There is a large difference between bandwidth and latency.

Latency only tells you how "responsive" a server is. It doesn't tell you how much data you can send through a connection.
A connection can both have a high latency and also be able to transfer data at high speed in high volumes.

Consider the bandwidth of a boeing 747 filled with dvd's. It has horrible latency, but the amount of data it can transport is much much more then any internet connection you have.

Title: Estimating file transfer time over network?

Body:
I am transferring file from one server to another. So, to estimate the time it would take to transfer some GB's of file over the network, I am pinging to that IP and taking the average time.

For ex: i ping to 172.26.26.36 I get the average round trip time to be x ms, since ping send 32 bytes of data each time. I estimate speed of network to be 2*32*8(bits)/x = y Mbps --> multiplication with 2 because its average round trip time.

So transferring 5GB of data will take 5000/y seconds

Am I correct in my method of estimating time.

If you find any mistake or any other good method please share.


答:
There are plenty on codeplex.

http://www.codeplex.com/site/search/openings?query=&sortBy=DownloadCount&tagName=%2cC%23%2c&licenses=|&refinedSearch=true

Title: Most Active Open Source C# Projects?

Body: I am looking for open source projects being done in C# that are actively looking for developers and does not mind the person coming in from a C++ background. Any pointers appreciated.</p>


问:
Any patent issue if I want to call my classes "signal/slot" as in Qt?
I need to code a signal-like mechanism and I was thinking of using the same "slot" and "signal" terms to indicate the signal and the function that needs to be called.

Since this is a commercial application I'd like to know if there might be any issue with using these names, e.g. if Qt has some sort of patent on them (I searched around but couldn't find it).

I believe this is a stupid question since patenting a class name would be moronic, to say the least.. but anyway...

To add some detail: my code is ENTIRELY different and has NOTHING TO DO with Qt except the above. I don't use moc or any Qt class.

答:
No, using the terms "signal" and "slot" for a communication mechanism that works completely different from QT's signal/slot mechanism will not get you into conflict with any patents that might exist on the Qt mechanism.

Patents work by protecting an idea, regardless of how that idea is called. To give an example1, the inventors of the foreach loop could have applied for a patent for "a looping construct that iterates over all elements of a container, without the need for the programmer to keep track if there are more elements." This patent would apply to the foreach construct found in many languages, but also to the range-based for loop in C++, even though that uses a different keyword.

The only legal protection that exists for simple names is trademark protection. This also doesn't apply in this case, because Qt doesn't use the terms 'signal' and 'slot' as trademarks.

1: To my knowledge, no such patent exists or at the very least it isn't being enforced. Otherwise there wouldn't be so many languages with this construct.


问:
Worst coding standard you've ever had to follow?
Have you ever had to work to coding standards that:

Greatly decreased your productivity?

Were originally included for good reasons but were kept long after the original concern became irrelevant?

Were in a list so long that it was impossible to remember them all?

Made you think the author was just trying to leave their mark rather than encouraging good coding practice?

You had no idea why they were included?

If so, what is your least favorite rule and why?

Some examples here

答:
I did code reviews on a project led by a C++ first timer who decided that all class member functions should be prefixed with the class name and visibility:</p>

```<code>class MyClass
{
   public:
      void MyClass_Public_setValue(int value);
}
</code>```

Body:
I did code reviews on a project led by a C++ first timer who decided that all class member functions should be prefixed with the class name and visibility:

class MyClass
{
   public:
      void MyClass_Public_setValue(int value);
}


问:
Tips on persuading boss that code review is a good thing
Let's say one works in a hypothetical company that has several developers that rarely worked together on projects and the Boss didn't believe that code reviews are worth the time and cost.

What are various arguments that could be presented in this scenario that will portray the benefit of code review?  Furthermore, what are the potential arguments against code review here and how can these be countered?

答:
When presenting it, focus on the bigger picture.

List the benefits (better code, less bugs, less re-writes, etc) and mention code review as one of the techniques that you would recommend.

I would make it part of a bigger picture of doing software craftsmanship

code reviews

tests

retrospectives

knowledge sharing

education

book reviews

lunchtime lectures

Be prepared to do a lot of work yourself in promoting these principles.

Most off all don't expect the persuasion to be a "one meeting and done" kinda thing.

You should build the case over time with calmness and consistently.  When you are most annoyed by bugs that would have been fixed by better techniques, is often the worst time to make your case as you are more likely to be over-emotional and less rational.  This might seem somewhat counter-intuitiuve but it is what I have learned over 30 years of programming.  Obviously ymmv.


问:
Recursion VS memory allocation
Which approach is most popular in real-world examples: recursion or iteration?

For example, simple tree preorder traversal with recursion:

```<code>void preorderTraversal( Node root ){
    if( root == null ) return;
    root.printValue();
    preorderTraversal( root.getLeft() );
    preorderTraversal( root.getRight() );
}
</code>```

and with iteration (using stack):

```<code>Push the root node on the stack
While the stack is not empty
    Pop a node
    Print its value
    If right child exists, push the node's right child
    If left child exists, push the node's left child
</code>```

In the first example we have recursive method calls, but in the second - new ancillary data structure.

Complexity is similar in both cases - O(n). So, the main question is memory footprint requirement?

答:
In the case of descending a recursive structure such as a tree, memory requirement is of the same order using either recursion or iteration (proportional to depth).

It's probably a bit cheaper to use iteration since you don't have to keep track of stack frames, and you also get the additional flexibility of being able to choose your maximum recursion depth (some languages have finite call stack size).

That being said, this is quite language-dependent as it depends on whether tail-recursion optimizations are available and whether stack frames are kept track of.  In some languages, the recursive example can be optimized into the equivalent iterative form.

In general, for languages where iteration is possible, recursion costs at least as much memory as iteration.

Title: Recursion VS memory allocation


问:
Does it make sense to make a field mandatory which has a default value?
Consider a drop down "Duration" which has 3 options

```<code>1. 30 days
2. 60 days
3. 90 days
</code>```

and by default 30 days will be the selected value. Does it make sense to make this dropdown mandatory? Do we really need to put a * (Mandatory mark) for this field?

答:
No it does not make sense.
The only context in which it could be argued to make sense is if there was a blank selection present. But having a blank selection be available for a required field with a default value doesn't make any sense.

If there were no default value, then one would want a blank field and required status. As it stands, I think that that would only add confusion.


问:
Interconnect nodes in a Java distributed infrastructure for tweet processing
I'm working in a new version of an old project that I used to download and process user statuses from Twitter. The main problem of that project was its infrastructure. I used multiple instances of a java application (trackers) to download from Twitter given an specific task (basically terms to search for), connected with a central node (a web application) that had to process all tweets once per day and generate a new task for each trackers once each 15 minutes. The central node also had to monitor all trackers and enable/disable them under user petition.

This, as I said, was too slow because I had multiple bottlenecks, so in this new version I want to improve the infrastructure and isolate all functionalities in specific nodes. I also need a good notification system to receive notifications for any node. So, in the next diagram I show the components that I'll need in this new version:

As you can see, there are more nodes. Here are some notes about them:

Dashboard: Controls trackers statuses and send a single task to each of them (under user request). The trackers will use this task until replaced with a new one (if done, not each 15 minutes like before).

Search engine: I need to store all the tweets. They are firstly stored in a local database for each tracker but after that I'm thinking on using something like Elasticsearch to be able to do fast searches.

Tweet processor: Just and isolated component with its own database (maybe something like the search engine to have fast access to info generated by the module). In the future more could be added.

Application UI: A web application with a shared database with the Dashboard (mainly to store users information and preferences). Indeed, both could be merged into a single web. The main difference with the previous version of the project is that now they will be isolated and they will only show information and send requests. I will not do any heavy task in them (like process tweets as I did before).

So, having this components, my main headache is how to structure all to not have to rewrite a lot of code every time I need to access any new data. Another headache is how can I interconnect nodes. I could use sockets but that is a pain in the ass. Maybe a REST layer? And finally, if all the nodes are isolated, how could I generate notifications for each user which info is only in the database used by the Application UI?

I'm programming this using Java and Spring (at least I used them in the last version) but I have no problems with changing the language if I can take advantage of a tool/library/engine to make my life easier and have a better platform. Any comment will be appreciated.

答:
You could use RPC to communicate between nodes. In my opinion that's the best way to do it. Communicate nodes directly to each other, and have the master send them tasks also via RPC.

Master node can have a table of slave nodes. Each new slave node checks-in with the master node, this is how the table is created. In the master node you can have an RPC server up and running on some fixed port like i.e. 15000. Each slave node once it's started pings the master node and reports to it by executing some method and pass in something like data from the slave node. This is how it's usually done. You can check Hadoop's IPC for more info.