答:
Going off your comment, where you do not need a new list every time.  It would be better to build up the list once, and return the same "singleton" list in that function.

```<code>public class RuleProvider
{
    private readonly List<IRule> _theRules =
        new List<IRule>{
         new Rule1(new Writer1()),
         new Rule2(new Writer2())
        };

    Public IList<IRule> GetRules()
    {
        return _theRules;
    }
}
</code>```

Depending on how you want this list to be used it would be worth looking into:

1) Lazy Loading - defer the "intial" load until someone needs to use the Rules list.

2) Read-only collection - so that users of your "rules" cannot modify the list.

Title: Design method to return List of objects

Body:
I need to create a method which will return a List of IRule objects.

This is what I have written, am I correct in the approach? Or should I follow any patterns/principles?

```<code>public IList<IRule> GetRules()
{
    return new List<IRule> {
         new Rule1(),
         new Rule2(),
         ...etc
     }
}
</code>```

Body:
Going off your comment, where you do not need a new list every time.  It would be better to build up the list once, and return the same "singleton" list in that function.

public class RuleProvider
{
    private readonly List<IRule> _theRules =
        new List<IRule>{
         new Rule1(new Writer1()),
         new Rule2(new Writer2())
        };

    Public IList<IRule> GetRules()
    {
        return _theRules;
    }
}

Depending on how you want this list to be used it would be worth looking into:

1) Lazy Loading - defer the "intial" load until someone needs to use the Rules list.

2) Read-only collection - so that users of your "rules" cannot modify the list.


问:
Design method to return List of objects
I need to create a method which will return a List of IRule objects.

This is what I have written, am I correct in the approach? Or should I follow any patterns/principles?

```<code>public IList<IRule> GetRules()
{
    return new List<IRule> {
         new Rule1(),
         new Rule2(),
         ...etc
     }
}
</code>```

答:
It depends on how you need to use GetRules.

If it's expensive and and you really do want to access the same set of Rules everywhere in your application, then I'd create a Cache class which stores the list of rules.

In this instance (and almost all others) a Singleton would be an ant-pattern. Instead you would have an ICache interface with a GetRules method and pass an ICache reference to every object that needs it.

While this is more work than rolling it into a Singleton (it's actually surprisingly difficult to implement the Singleton pattern correctly), it gives you correct separation of concerns and allows you to swap out the mechanism behind the ICache interface. e.g. In time you find you need to refresh the cache every 8 hours. The Cache object can do that, but the client code doesn't need to change.

Or you find you need to implement two different types of caching for two different games/clients? You simply inject a different implementation of the cache.

You find in a years time that one part of the running app needs to see live rules in a database and can't work from the cache? You inject a different cache that isn't actually a cache but returns live rules each time it's called. You'd be in trouble with a singleton on that one.

That's one way you could do it. Have a Cache that returns rules, or an IRulesProvider that knows how to get rules.

Abstract the getting of rules, inject that abstraction into the classes that need to get rules, then implement that interface.

You can still choose to create only one instance of the class that implements IRulesProvider but that's up to you, you don't need all the crap that comes along with the Singleton pattern, half of which is there to protect yourself from creating more than on instance of the class.

You may by now have guessed I've butted heads with Singletons in the past . . . it has left some scars :)

Even if it's not expensive to create the objects

Still consider the Provider pattern. It give you great flexibility and proper separation of concerns. Things that use rules can get rules, but they don't even know what gets the rules. It's a realy powerful pattern to follow and leaves you and your code extremely agile.

An example of what I mean by a provider pattern.

```<code>interface IRulesProvider
{
    IEnumerable<IRule> GetRules();
}

class RulesProvider: IRulesProvider
{
    IEnumerable<IRule> GetRules()
    {
        // return some rules;
    }
}
</code>```

Are the rules cached on the RulesProvider object? are they fetched from an Xml file and cached on it, are they read live from a database each time GetRules is called? Thats up to you, that class you inject an IRulesProvider into neither knows nor care how the provider comes by the rules (unless of course it needs to know, at which point we need a different mechanism).

ContentLicense: CC BY-SA 3.0

Title: Design method to return List of objects

Body:
It depends on how you need to use GetRules.

If it's expensive and and you really do want to access the same set of Rules everywhere in your application, then I'd create a Cache class which stores the list of rules.

In this instance (and almost all others) a Singleton would be an ant-pattern. Instead you would have an ICache interface with a GetRules method and pass an ICache reference to every object that needs it.

While this is more work than rolling it into a Singleton (it's actually surprisingly difficult to implement the Singleton pattern correctly), it gives you correct separation of concerns and allows you to swap out the mechanism behind the ICache interface. e.g. In time you find you need to refresh the cache every 8 hours. The Cache object can do that, but the client code doesn't need to change.

Or you find you need to implement two different types of caching for two different games/clients? You simply inject a different implementation of the cache.

You find in a years time that one part of the running app needs to see live rules in a database and can't work from the cache? You inject a different cache that isn't actually a cache but returns live rules each time it's called. You'd be in trouble with a singleton on that one.

That's one way you could do it. Have a Cache that returns rules, or an IRulesProvider that knows how to get rules.

Abstract the getting of rules, inject that abstraction into the classes that need to get rules, then implement that interface.

You can still choose to create only one instance of the class that implements IRulesProvider but that's up to you, you don't need all the crap that comes along with the Singleton pattern, half of which is there to protect yourself from creating more than on instance of the class.

You may by now have guessed I've butted heads with Singletons in the past . . . it has left some scars :)

Even if it's not expensive to create the objects

Still consider the Provider pattern. It give you great flexibility and proper separation of concerns. Things that use rules can get rules, but they don't even know what gets the rules. It's a realy powerful pattern to follow and leaves you and your code extremely agile.

An example of what I mean by a provider pattern.

interface IRulesProvider
{
    IEnumerable<IRule> GetRules();
}

class RulesProvider: IRulesProvider
{
    IEnumerable<IRule> GetRules()
    {
        // return some rules;
    }
}

Are the rules cached on the RulesProvider object? are they fetched from an Xml file and cached on it, are they read live from a database each time GetRules is called? Thats up to you, that class you inject an IRulesProvider into neither knows nor care how the provider comes by the rules (unless of course it needs to know, at which point we need a different mechanism).


问:
Non-ambiguous wording for 'Backend'
When discussing a general and abstract project idea I often use the terms frontend and backend for GUI and well.. backend.

Often however, some people misunderstand frontend as the part of GUI used by 'customers' and backend as a seperat GUI for administrative personell etc.

I think I get where they come from (CMS or DMS probably use backend in that sense) but I am struggling with making a point when halve the people don't understand what I'm saying.

Is this a more common problem in German speaking countries? Is it my fault? Does anyone else have these problems?

Are there any synonyms for backend that mean the real backend (data access layer, non-gui functionalities etc.) and not the CMS/DMS backend which I could use?

答:
The term "backend" is not offcially defined, so there is no "real" backend. For example, in my current working context, I would call the database a backend component (even if it does run on the same machine, maybe embedded in the program's process), but not a "data access layer".

So my suggestion is

whenever you use the word backend, give a clear definition of what you mean in the specific context

or, name the backend component you are talking of more specifically (the database, the web service, a non-gui library, a background monitor, a network service, and so on )

Title: Non-ambiguous wording for 'Backend'


问:
GPLing an implementation of a patented algorithm
TL;DR: Can you publicly release your own implementation of a patented algorithm as a free research tool for others, under the GPL, when you do not hold the patent (but will happily give clear and thorough attribution to the owners of the patent)? see below for clarification.

First off, apologies if this question has already been answered.  After ~30 minutes of googling and stack-exchanging, I haven't found anything that quite answers what I'm facing.

I work in an academic group and part of our work has been implementing a CT reconstruction algorithm similar to what is done clinically by a major manufacturer.  This manufacturer has a patent that definitely covers what we have implemented.  (I did the implementation from a paper published almost 10 years earlier, only to find that they had also patented the algorithm a little over a year ago. Damn.)

They have a patent on the method of reconstruction and several of the equations utilized, but no source code or anything of the sort.  The code implementation is 100% unique to our group. We're talking patents, not copyright.

We are planning to write a "technical note" about our implementation (gpu-based, more detail on how we implemented the published algorithms (i.e. much more detail than what's patented), etc. all stuff which is not described in the patent) for publication in a major journal ** AND ** this would include releasing our source code.  I would like to release it under GPLv2.0 to provide folks with a research tool.

The GPL would prevent us and anyone down the road from using our code in proprietary software, and if anyone were to use it to make money, they would have to pay rights to the patent owner (and release any code derived from ours). I would like to somehow make clear that we are not claiming any rights to the algorithm, and the only the implementation would be covered by the GPL, however I recognize this distinction may not be possible under the current US legal setup (blegh).

Even if we could somehow make that distinction clear when/if we release, are we violating any of their rights with the patent?  I'm thinking they would have to somehow show that our release damaged their business covered by the patent.

Any thoughts from more experienced folks are much appreciated!  I think this may be kind of a gray area (or not! I'm not a lawyer! haha).

We do NOT wish to infringe upon the patent and don't pretend to have any claim to.  We have a good relationship with the vendor, so we'll most likely end up figuring it out with them, but if they said no, I was curious if it's just because of their feels or if thay have a legitimate claim against our group.

答:
You need to speak to a lawyer before proceeding any further with this.

Implementing the methods covered by this patent will constitute use of the patent, and if permission has not been given by the vendor for the exact use case proposed, then you are infringing on their rights, and are opening yourself up to legal action if they are not happy with what you are doing.

The license in which you release the software is irrelevant, you are using their patent, and therefore need their permission to do so. As part of granting this permission, they will also specify under what terms you may release the software. You will need to ensure they grant explicit permission for it to be released as GPL.

You are also wrong in assuming that because it is released as GPL that people cannot make money from the code. You can still use the code in a commercial application and charge a fee for it, you just need to also release the code for your application. But that's another discussion.

So in short, you need to speak to the vendor and get their explicit permission before proceeding.


答:
This is because "backend" can mean a lot of things. Without context it has no meaning.

For instance:
In a CMS it is not uncommon to actually call the administrative interface "the backend". Try a search for "Wordpres backend." and you'll see what I mean.

Wikipedia list of front and back ends

Title: Non-ambiguous wording for 'Backend'

Body:
When discussing a general and abstract project idea I often use the terms frontend and backend for GUI and well.. backend.

Often however, some people misunderstand frontend as the part of GUI used by 'customers' and backend as a seperat GUI for administrative personell etc.

I think I get where they come from (CMS or DMS probably use backend in that sense) but I am struggling with making a point when halve the people don't understand what I'm saying.

Is this a more common problem in German speaking countries? Is it my fault? Does anyone else have these problems?

Are there any synonyms for backend that mean the real backend (data access layer, non-gui functionalities etc.) and not the CMS/DMS backend which I could use?


答:
In the current state-of-the-art english, "backend" is not defined on the context of systems engineering, therefore, no synonyms or antonyms.

In the current state-of-the-art systems engineering "backend" is generally understood as everything that a user does not directly interact with. In another words, it's all that infrastructure only reached by servers. So a web server is front end because a user might write a HTTP request in Fiddler (although highly unlike) and a database server might also be front-end because a user might sniff the network and catch a "SELECT * FROM myTable" going to 192.168.0.4, somewhere he can call a pipe or something else.

In other scenarios "backend" is also a gigantic stack composed of many layers such as caching, security, OR mapping and etc... not only a "business-layer" and "database-layer"...

But if you put a gun in my head and you know... i would use the "Hidden-Stack" or maybe "Server-Side"... they are not perfect... but theres a gun in my head...

Title: Non-ambiguous wording for 'Backend'


问:
How to optimize the application with huge number of database requests per minute
I have to provide free demo of some service to end users in my application. Free demo could be of 30 mins, 1 hours, 5 hours etc. (predefined time) for a new user for one time only.

User can also consume that time in parts. like in 30 mins of free demo, they can use like 10 mins today, 15 mins tomorrow and rest of the time on next day etc.
Now If a user opt the free demo of 30 mins and logged in & using the service. I can restrict the user for 30 mins via his start time & end time. I can send them to payment page if sum of start & end-time is equals to 30 min.

Now problem arises with some uncertain conditions like what if user closes the browser or their internet stopped working or anything else at their end during their active session. In this, I can't calculate their consumed time because of lack of endtime.

Scenario could be like below (for 30 min demo).

```<code>UserID  StartTime           EndTime             Consumed(mins)
10      09-04-2015 10:00    09-04-2015 10:10        10
10      10-04-2015 05:00    10-04-2015 05:04        4
10      11-04-2015 07:46    11-04-2015 07:56        10
10      11-04-2015 10:00    // Browser closed or any uncertain condition
10      11-04-2015 11:00    // How to restrict user to use actual 30 mins because I do not have EndTime in above row to calculate Consumed mins.
</code>```

I may have more than 100000 users at the time same to use our services, So I am finding an efficient solution for this.

As per my understanding, I can create a Separate Job to check user's LastActiviteTime and based on that I can update their Consumed(mins) in database. That Job would be executed every minute and also on the other hand, browser of each session user would update the LastActiveTime in database.

This can solve my problem but I'm not very sure about the performance of my application becuase of huge number of database request per minute.

Please advice.

答:
I suppose that with this number of users it is not hosting but dedicated servers where you can install everything you need to your service. If it is right my suggestion is to use some external storage like Redis where login token (or other user identifier that passed from client to server) will be the key and value is all data you want to know about the user including time left in demo.

Each time the request received from user you can update time elapsed in Redis without inserting junk data into database and overloading it with requests. And additional profit is that you can set when Redis record will be terminated it may be done automatically by Redis if you will not update termination date of the record.

In this case users may have time limit to use their demo and you do not need to do anything to implement it.


问:
Skillset improvement in coming new year
Here is a little background information.

I have been working for Java 10 years.

The product I am working on went to live about 3 years ago. Now, the product is getting stable.

After all the post-product drama, I gained a lot of knowledge about Oracle & SQL.
People(mainly management) were desperated enough to give me deep oracle-related task over DBAs. I admit I considered becoming DBA but eventually decided to remain as a programmer.

DBAs & Management are demanding all the DB & Query related task back to DBA, which makes me a bit sad.

In short, I anticipate a lot of time next year.
What would you do to improve your skillset??

I am thinking to upgrade my Java version(Not from experience though, we are using JDK1.5) to 1.6 getting certificate.

Any good idea from fellow developers??

-----------Edit ---------------------

How about data modeling for application? Do you guys think it is developer role??

答:
In your position I would have a very close look on the JPA technology which is a bridge between objects and database tables.  Very few people have both the hardcore Java experience, as well as the hardcore database experience needed to understand, analyze and improve this layer in a production setting.

Additionally it is part of recent versions of Java EE, which makes it very relevant to a Java EE team as this is essentially the backend engine.

ContentLicense: CC BY-SA 2.5

Title: Skillset improvement in coming new year


问:
Is there a valid symbol for "two or more" with Crow's Foot notation?
I have a database design for which one entity of Class A always has at least two entities of Class B. Can I express this with valid Crow's Foot notation?

One possible idea I had was this:

Excuse the giant crow's foot, was a quick drawing! I know this breaks the Crow's Foot format of always having two symbols on each end. Also I'm concerned that it's not particularly obvious. I think doing a UML with 2...M is more obvious.

So is there a way I can do this cleanly with Crow's Foot?

答: AFAIK there is no "offical" symbol for this. You can help yourself by adding a free-form commentary, or use UML instead.</p>


答:
You are correct in that you cant ensure logout with a web application, where you essentially download a client program and then make intermittent requests for data the server.

There are a few work arounds for this problem that I know of

1: define 'logged out' as automatically occurring after a period of inactivity

This may not be best for you with such short periods as 30min as a user who's computer crashes will use up actual time + inactivity timeout

2: use javascript to constantly poll the server 'i am logged in!' you can improve this with websockets or long polling

This gets you more time resolution than 1 but at the cost of constantly processing the messages

For your specific case I think you just need to define 'time used' in your terms of service to take account of the possibility of crashes using up a bit more time.

Either the customer loses out on a few min because you include the inactivity period, or they can possibly scam you for a few min because you measure to the last time you are sure they were logged in rather than to when you assume they are logged out.

Title: How to optimize the application with huge number of database requests per minute

Body:
I have to provide free demo of some service to end users in my application. Free demo could be of 30 mins, 1 hours, 5 hours etc. (predefined time) for a new user for one time only.

User can also consume that time in parts. like in 30 mins of free demo, they can use like 10 mins today, 15 mins tomorrow and rest of the time on next day etc.
Now If a user opt the free demo of 30 mins and logged in & using the service. I can restrict the user for 30 mins via his start time & end time. I can send them to payment page if sum of start & end-time is equals to 30 min.

Now problem arises with some uncertain conditions like what if user closes the browser or their internet stopped working or anything else at their end during their active session. In this, I can't calculate their consumed time because of lack of endtime.

Scenario could be like below (for 30 min demo).

```<code>UserID  StartTime           EndTime             Consumed(mins)
10      09-04-2015 10:00    09-04-2015 10:10        10
10      10-04-2015 05:00    10-04-2015 05:04        4
10      11-04-2015 07:46    11-04-2015 07:56        10
10      11-04-2015 10:00    // Browser closed or any uncertain condition
10      11-04-2015 11:00    // How to restrict user to use actual 30 mins because I do not have EndTime in above row to calculate Consumed mins.
</code>```

I may have more than 100000 users at the time same to use our services, So I am finding an efficient solution for this.

As per my understanding, I can create a Separate Job to check user's LastActiviteTime and based on that I can update their Consumed(mins) in database. That Job would be executed every minute and also on the other hand, browser of each session user would update the LastActiveTime in database.

This can solve my problem but I'm not very sure about the performance of my application becuase of huge number of database request per minute.

Please advice.


问:
AST processing and usefulness of visitor pattern
I know the visitor pattern is typically used to traverse a hierarchy of heterogeneous objects (inheriting a same abstract object) and dissociate the processing of these objects from the data within them. A classic use of the visitor pattern (often quoted), is the processing of an abstract syntax tree in a compiler. Indeed, the structure of the tree is only known at runtime (once the program is parsed), and one want to traverse the tree modifying the nodes according to semantic passes implemented as visitor. But the visitor pattern looked like an overkill to me since it allow a double (dynamic) dispatch as a function of the ASTNode type and the Visitor type, or one statically know the order of the visitor we will use to process the AST during the semantic phase. If I had to code this in C++ (done on the spot without compiling), I would do :

```<code>struct AstNode
{
    //virtual void accept() = 0; //this won't work since template does not override virtual pure method
    virtual ~AstNode();
};

struct SpecificNode : public AstNode
{
    template <class Visitor>
    void accept(Visitor v)
    {
        v.visit(*this);
    }
};

//other AST nodes

class MyVisitor
{
    void visit(SpecificNode n) {/*...*/}

    //other visit methods for other AST nodes
};

//other visitors
</code>```

There is no double dispatch here, that is not a true visitor. But I can figure out why I would need this double dispatch.

Notes :

This wikipedia pages list other uses of double dispatch but not AST : http://en.wikipedia.org/wiki/Double_dispatch#Examples.

答:
One thing the Visitor Pattern does that is often not talked about, is enabling to choose which side of the Expression Problem you want to tackle.

So, what is the Expression Problem? It refers to the basic problem of extensibility: our programs manipulate data types using operations. As our programs evolve, we need to extend them with new data types and new operations. And particularly, we want to be able to add new operations which work with the existing data types, and we want to add new data types which work with the existing operations. And we want this to be true extension, i.e. we don't want to modify the existing program, we want to respect the existing abstractions, we want our extensions to be separate modules, in separate namespaces, separately compiled, separately deployed, separately type checked. We want them to be type-safe.

The Expression Problem is, how do you actually provide such extensibility in a language?

It turns out that for typical naive implementations of procedural and/or functional programming, it is very easy to add new operations (procedures, functions), but very hard to add new data types, since basically the operations work with the data types using some sort of case discrimination (switch, case, pattern matching) and you need to add new cases to them, i.e. modify existing code:

```<code>func print(node):
  case node of:
    AddOperator => print(node.left) + '+' + print(node.right)
    NotOperator => '!' + print(node)

func eval(node):
  case node of:
    AddOperator => eval(node.left) + eval(node.right)
    NotOperator => !eval(node)
</code>```

Now, if you want to add a new operation, say, type-checking, that's easy, but if you want to add a new node type, you have to modify all the existing pattern matching expressions in all operations.

And for typical naive OO, you have the exact opposite problem: it is easy to add new data types which work with the existing operations (either by inheriting or overriding them), but it is hard to add new operations, since that basically means modifying existing classes/objects.

```<code>class AddOperator(left: Node, right: Node) < Node:
  meth print:
    left.print + '+' + right.print

  meth eval
    left.eval + right.eval

class NotOperator(expr: Node) < Node:
  meth print:
    '!' + expr.print

  meth eval
    !expr.eval
</code>```

Here, adding a new node type is easy, because you either inherit, override or implement all required operations, but adding a new operation is hard, because you need to add it either to all leaf classes or to a base class, thus modifying existing code.

Several languages have several constructs for solving the Expression Problem: Haskell has typeclasses, Scala has implicit arguments, Racket has Units, Go has Interfaces, CLOS and Clojure have Multimethods.

However, in an OO language that doesn't have a way of solving the Expression Problem (such as Java or C#), the Visitor Pattern at least allows you to "pick your poison". What the pattern does, is turn your design 90° to the side: the operations become classes (PrintVisitor, EvalVisitor) and conversely, the types become methods (visitAddOperator, visitNotOperator (or just visit, if your language supports argument-based overloading)). This does not solve the Expression Problem (i.e. how to make it easy to add both types and operations), but it does allow you to choose which one to make easy.

So, if your language does support a way to solve the Expression Problem, then you don't need this workaround.

Note, however, this is not the only thing the Visitor Pattern does.

Note: you will note the conspicuous absence of any mention of C++, whatsoever. Unfortunately, I simply don't know enough about it. I suspect that between its overloading and argument-based dispatch, virtual inheritance, free functions, macros, and most importantly compile-time template metaprogramming, the Expression Problem is solved in C++, but I don't know for sure.

The problem is that once someone finds a solution for the Expression Problem, they redefine it to make it even harder so solve, so that new solutions are even more powerful and expressive. For example, the original formulation by the Haskell community did not require modular typechecking, but the Scala community proposed that the Expression Problem should not only include modular extension (separate compilation etc.) of types and operations, but also modular typechecking and type inference of those extensions, which at the moment is something only Scala's implicits can do and Haskell's typeclasses and ML's functors can't.

Body:
One thing the Visitor Pattern does that is often not talked about, is enabling to choose which side of the Expression Problem you want to tackle.

So, what is the Expression Problem? It refers to the basic problem of extensibility: our programs manipulate data types using operations. As our programs evolve, we need to extend them with new data types and new operations. And particularly, we want to be able to add new operations which work with the existing data types, and we want to add new data types which work with the existing operations. And we want this to be true extension, i.e. we don't want to modify the existing program, we want to respect the existing abstractions, we want our extensions to be separate modules, in separate namespaces, separately compiled, separately deployed, separately type checked. We want them to be type-safe.

The Expression Problem is, how do you actually provide such extensibility in a language?

It turns out that for typical naive implementations of procedural and/or functional programming, it is very easy to add new operations (procedures, functions), but very hard to add new data types, since basically the operations work with the data types using some sort of case discrimination (switch, case, pattern matching) and you need to add new cases to them, i.e. modify existing code:

func print(node):
  case node of:
    AddOperator => print(node.left) + '+' + print(node.right)
    NotOperator => '!' + print(node)

func eval(node):
  case node of:
    AddOperator => eval(node.left) + eval(node.right)
    NotOperator => !eval(node)

Now, if you want to add a new operation, say, type-checking, that's easy, but if you want to add a new node type, you have to modify all the existing pattern matching expressions in all operations.

And for typical naive OO, you have the exact opposite problem: it is easy to add new data types which work with the existing operations (either by inheriting or overriding them), but it is hard to add new operations, since that basically means modifying existing classes/objects.

class AddOperator(left: Node, right: Node) < Node:
  meth print:
    left.print + '+' + right.print

  meth eval
    left.eval + right.eval

class NotOperator(expr: Node) < Node:
  meth print:
    '!' + expr.print

  meth eval
    !expr.eval

Here, adding a new node type is easy, because you either inherit, override or implement all required operations, but adding a new operation is hard, because you need to add it either to all leaf classes or to a base class, thus modifying existing code.

Several languages have several constructs for solving the Expression Problem: Haskell has typeclasses, Scala has implicit arguments, Racket has Units, Go has Interfaces, CLOS and Clojure have Multimethods.

However, in an OO language that doesn't have a way of solving the Expression Problem (such as Java or C#), the Visitor Pattern at least allows you to "pick your poison". What the pattern does, is turn your design 90° to the side: the operations become classes (PrintVisitor, EvalVisitor) and conversely, the types become methods (visitAddOperator, visitNotOperator (or just visit, if your language supports argument-based overloading)). This does not solve the Expression Problem (i.e. how to make it easy to add both types and operations), but it does allow you to choose which one to make easy.

So, if your language does support a way to solve the Expression Problem, then you don't need this workaround.

Note, however, this is not the only thing the Visitor Pattern does.

Note: you will note the conspicuous absence of any mention of C++, whatsoever. Unfortunately, I simply don't know enough about it. I suspect that between its overloading and argument-based dispatch, virtual inheritance, free functions, macros, and most importantly compile-time template metaprogramming, the Expression Problem is solved in C++, but I don't know for sure.

The problem is that once someone finds a solution for the Expression Problem, they redefine it to make it even harder so solve, so that new solutions are even more powerful and expressive. For example, the original formulation by the Haskell community did not require modular typechecking, but the Scala community proposed that the Expression Problem should not only include modular extension (separate compilation etc.) of types and operations, but also modular typechecking and type inference of those extensions, which at the moment is something only Scala's implicits can do and Haskell's typeclasses and ML's functors can't.


问:
role based access control model in object oriented
I want to prepare a swing application. I am designing a role based access control model in mvc design.

When a view wants to request something from a controller, how can this request be realized in object oriented manner. That is, while requesting how can session information of user be sent to the model or controller?

For example, a model method can be like that

```<code>createCustomeraccount(EntityCustomer obj, Session user)
</code>```

so all methods in model classes should have session object parameter which has role information of user and also role and rights should be store in a class to check user right. Therefore every model has a roleright object which includes role and right mapping.

Is this correct design?

答:
It will probably be a lot easier for you if you don't have to pass the Session to every method call on a controller. This puts the requirement for keeping the Session on the view and that's not really where it belongs. It also makes your controller methods very heavy as they will need to always have this parameter, even if it makes no sense.

Instead, why not set up this 'session' when the user logs into the application and make it available through a class? In the simplest model your controller could have a 'SessionContext' or 'UserContext' injected in the constructor and every method could call that context and validate authorization to call the method, throwing exceptions if the requirements are not met.

```<code>public class CustomerController
{
    private UserContext _context;

    public CustomerController(UserContext context)
    {
            _context = context;
    }

    public void createCustomerAccount(EntityCustomer obj)
    {
        if(!_context.isUserAdmin())
            throw new UnauthorizedMethodException("User not authorized to call createCustomerAccount");

        //do your thing
    }
}
</code>```

(Not Java, but you get the idea)

More advanced ways of doing this could include using a Command Object-driven approach or using Aspects on methods.

Body:
It will probably be a lot easier for you if you don't have to pass the Session to every method call on a controller. This puts the requirement for keeping the Session on the view and that's not really where it belongs. It also makes your controller methods very heavy as they will need to always have this parameter, even if it makes no sense.

Instead, why not set up this 'session' when the user logs into the application and make it available through a class? In the simplest model your controller could have a 'SessionContext' or 'UserContext' injected in the constructor and every method could call that context and validate authorization to call the method, throwing exceptions if the requirements are not met.

public class CustomerController
{
    private UserContext _context;

    public CustomerController(UserContext context)
    {
            _context = context;
    }

    public void createCustomerAccount(EntityCustomer obj)
    {
        if(!_context.isUserAdmin())
            throw new UnauthorizedMethodException("User not authorized to call createCustomerAccount");

        //do your thing
    }
}

(Not Java, but you get the idea)

More advanced ways of doing this could include using a Command Object-driven approach or using Aspects on methods.


答:
Since users will be somehow authenticated during usage, you could "flag" trial accounts status and keep track of their activity:

User A logs in.

User A performs an action against the service

A combination of the user ID + timestamp is tracked

Check the earliest timestamp associated with the same user ID

IF the check passes (the earliest and the current are within the allowed interval), perform the requested action

ELSE you can deny the request (your trial "window" has expired).

This can get fancier and cover subscription plans and the like, it just needs a handful of easily handled flags. It's also tamper-proof to a certain degree, since it's all server-side (I'm not considering hacked account, but that's another issue).

This approach would start "counting" when users perform the first request. If you want to consider the interval start at the time of registration, you can simply "track" a fake action to provide you with a timestamp to compare against.


问:
Concurrency: How do you approach the design and debug the implementation?
I've been developing concurrent systems for several years now, and I have a pretty good grasp on the subject despite my lack of formal training (i.e. no degree).  There's a few new languages that have become popular to at least talk about lately that are designed to make concurrency easier such as Erlang and Go.  It appears that their approach to concurrency echoes my own experience as to how to make systems scalable and take advantage of multiple cores/processors/machines.

However, I find that there are very few tools to help visualize what you intend to do, and verify that you are at least close to your original vision.  Debugging concurrent code can be a nightmare with languages that are not designed for concurrency (like C/C++, C#, Java, etc.).  In particular, it can be near impossible to recreate conditions that happen readily on one system in your development environment.

So, what are your approaches to designing a system to deal with concurrency and parallel processing?  Examples:

How do you figure out what can be made concurrent vs. what has to be sequential?

How do you reproduce error conditions and view what is happening as the application executes?

How do you visualize the interactions between the different concurrent parts of the application?

I have my own answers for some of these, but I'd also like to learn a bit more.

Edit

So far we have a lot of good input.  Many of the articles linked to are very good, and I've already read some of them.

My personal experience with concurrent programming leads me to believe you need a different mindset than you do with sequential programming.  The mental divide is probably as wide as the difference between object oriented programming and procedural programming.  I'd like this set of questions to focus more on the thought processes necessary (i.e. theory) to systematically approach the answers.  When providing more concrete answers, it helps to provide an example--something you went through personally.

Goal for the Bounty

Don't tell me what I should do.  I already have that under control.  Tell me what you do.  Tell me how you solve these problems.

答:
I've been developing concurrent
  systems for several years now, and I
  have a pretty good grasp on the
  subject despite my lack of formal
  training (i.e. no degree).

Many of best programmers I know didn't finish the University.
As for me I studied Philosophy.

C/C++, C#, Java, etc.). In particular,
  it can be near impossible to recreate
  conditions that happen readily on one
  system in your development
  environment.

yes

How do you figure out what can be made concurrent vs. what has to be
  sequential?

we usually start with a 1000 miles high metaphor to clarify our architecture to ourselves (firstly) and to others (secondly).

When we faced that problem, we always found a way to limiting the visibility of concurrent objects to non concurrent ones.

Lately I discovered Actors in scala and I saw that my old solutions were a kind of "miniactors", much less powerful than scala ones. So my suggestion is to start from there.

Another suggestion is to skip as many problems as possible: for example we use centralised cache (terracotta) instead of keeping maps in memory, using inner class callbacks instead of synchronised methods, sending messages instead of writing shared memory etc.

With scala it's all much easier anyway.

How do you reproduce error conditions and view what is happening
  as the application executes?

No real answer here. We have some unit test for concurrency and we have a load test suite to stress the application as much as we can.

How do you visualize the interactions between the different
  concurrent parts of the application?

Again no real answer: we design our Metaphor on the whiteboard and we try to make sure there are no conflicts on the architectural side.

For Arch here I mean the Neal Ford's definition: Sw Architecture is everything that will be very hard to change later.

programming leads me to believe you
  need a different mindset than you do
  with sequential programming.

Maybe but for me it's simply impossible to think in a parallel way, so better design our software in a way that doesn't require parallel thinking and with clear guardrails to avoid crashes between concurrency lanes.


问:
Tracing an object
Suppose I want an object with a list of strings and every method that this object gets passed to adds its name to the list of strings. Basically it's a stack trace, except unlike a stack trace it keeps a record of everywhere it's previously been.

In order to do this, I would have to put code in every method it goes through to add itself but if it went through methods that didn't know to add themselves then of course it wouldn't have a record of having been through those methods.

It would be cool if it were possible to make an object self aware, so to speak, of the methods it goes through but I don't think it is. But I'm wondering if there is any other way to get a record of all methods the object goes through, including those methods that don't know to update the object?

EDIT: Currently, I'm thinking the closest you could come to solving this would be to make any property/method that's accessed on the object update itself at that point in time. of course this wouldn't catch places that it simply passes through without being accessed.

答: Take a look at the StackTrace and StackFrame objects in the System.Diagnostics namespace.  They may be able to help log out information that is desired.  When an exception occurs the stack trace in the exception is captured in a similar manner.  It sounds like you want to log non-exception situations as well, so this may help as one can get the stack and frame at any point in the code.

Title: Tracing an object


问:
Is this a user story or a bug?
Consider the following user story(ies) for a manufacturing management system I am building:

As a shipper/receiver I need to be able to create work orders So that
  we can begin tracking jobs internally

As a shipper/receiver I need to be able to update work orders So in
  the case of an error at incoming I can change that before handing off
  to production

As a inspector I need to be able to change the part number/repair
  scheme of a work order So in the case of a mis-identified part I can
  change the work scope before actually starting

These are all tasks which would take several days to implement, once the work order UI dialog had been established and the CRUD facilities, etc were in place.

Now lets say three weeks down the road, after using the prototype, and inspector comes to me and says "Receiving created a new work order by accident; The work order already existed but the part had left the building for sub-contract work and upon it's return should have been expedited past work order creation"

I might write this story out as follows:

As a shipper/receiver I should not be able to re-create work orders
  already in the system as WIP; In these cases the system should
  determine the subcon and notify me of that status so I simply pass it
  on through So that multiple live work orders are not duplicating each
  others work scope

The story sounds quite verbose...but the requirement was literally a few lines of code added to the beginning of work order creation and took only 30 mins to add. Feels odd using a story for such a request.

Better suited for a bug tracker???

My question ultimately becomes this: When I am analyzing requirements, asking questions and figuring out high level components (ie: Work orders, repair orders, sales orders, etc) I am not focusing on specific business logic or rules but establishing components.

As a user I need to be able to create and manage work orders So that
  we can begin tracking parts internally

Does it make sense to further break these down into atomic user stories, such that:

As a user I should not be able to receive a work order when...

Listing out the exceptions as individual stories?

To me this is where use-case and it's format for capturing requirements makes more sense...

Anyway what has your experience been? Opinions & comments welcome...

I am attempting to find a balance between the terse agile/scrum user story and the BUFD use-case approach to project planning (please leave the they are mutually exclusive argument for another topic)

答:
Scrum doesn't really care about the type. A Feature, story, bug, thing, item, use-case... Anything can live on the product backlog. So it doesn't really matter what you call them.

The items on the top should have a decent enough level of detail so you can forecast as far as you need and so that the scrum team knows enough to start the work and be comfortable that they'll be able to finish by the end of the sprint (which doesn't necessarily mean that it's fully defined, as long as the team knows they'll be able to refine the final things during the sprint). That's the idea behind just-in-time planning and refinement. Items lower on the list may have a lot less detail as you still have time to refine them over time.

If you've chosen to use User Stories as your format, then there is generally a certain level of detail that still makes sense, then the next level is generally specified in useful test cases or acceptance criteria.

You should look at writing your acceptance criteria for your stories, the latter ones really feel more like the more detailed steps you described. Specification by Example and the "Given...When...Then..." notation would fit really well.

Only break down to a level that's needed to provide your level of forecasting and estimation. If a specific acceptance criterium would cost a lot of extra time, you may break it down into it's own story. You can always break it down at any later point in time, so only do it if you need it broken down now to set the order of implementation.

Remember that as you're refining your backlog over time, you may indeed get to a detail level that's closer to you BUFD level, but if you do, make sure that it's in some form of executable specification or in the form of test cases that you want executed, that way you don't create a lot of duplicate work.

Tools like Specflow and Cucumber can really help in doing this. Your specification is automatically mapped to Unit tests and integration tests so that you will automatically see when the work is finished by looking at your passing tests. That takes away a lot of the waste that's associated with the traditional use-case definitions. It also allows your stories to be written at a high level, then as you go into details, it's specific and easy to validate.

Title: Is this a user story or a bug?


问:
Data format for binary data transfer
I have to make an oscilloscope. Ideally, the oscilloscope connects to a probe. This probe should be another program, and could get the data locally or from a network. This data are numbers, floats and/or integers. The number of channels may vary. And they change with time. Not all channels need to have the same frequency.

Now, sometimes the frequency can be 1KHz, so using json is not ok because is wastes a lot of resources since is based in text. I'm looking for a binary data format, so the data transfer is faster.

So the first part of the question is: Do you know of a format to transfer binary data like this? It should have some kind of header, and then just numbers. My guess is that somebody must have had this problem before and there should be some kind of library to use this.

Then comes the second part of the question: What protocol should we use to transfer the data?

Maybe using json but with a base 64 encoder for the data using standart http calls could work, but for 1KHz signal that doesn't make sense. I wonder what people do to solve this.

答:
Base 64 encoding is used for sending binary data over a text channel.  It is exactly the opposite of what you want.  It will merely require encoding/decoding code and make your messages larger.

It is hard to make a recommendation without knowing exactly what data you need to send.  You might want to look at Protocol Buffers as it is designed to be compact.

Title: Data format for binary data transfer


问:
In Agile SCRUM, how do you handle even distribution of work in a sprint between "experts"?
Where I work, we have a lot of silos of knowledge. Basically each member of the development team is the expert (and sometimes the only one that knows a thing) on certain pieces of the overall software. For example, one person might be the expert on the rendering engine of a game. Another is the guy that works on the GUI. Rarely do the two intermingle. The concern is that it doesn't make sense to have the GUI guy work on Networking stuff, when there is already someone that can (in terms of efficiency) get something done better because they are already familiar with it.

This situation seems to make sprint planning difficult in SCRUM. It's quite possible that people on the team have no tasks or are seriously underutilized in a specific sprint simply because there is nothing there they are the "best fit" to work on. This makes calculating velocity unreliable as it will vary based on who actually gets more assignments during one sprint versus the other.

How would I overcome this issue? It seems like the focus should be more on the tasks and what needs to be done and less on the specific people to work on them. I think upper management will always prefer things to be done as quickly as possible. And if they see that GUI developer estimates 21 story points on a task related to the Engine, where the Engine guy estimates 5 points, they will question why we chose 21 (The goal would obviously be knowledge sharing, but this will cause the team to work inefficiently overall).

答:
To some extent, there's nothing you can do if your teams are that specialized. And if you're asking from the viewpoint of a manager, there's nothing you should do, either. This is a problem for the team itself to solve.

A point to remember is that scrum tries to optimize the team, not the individual. It's perfectly fine (from a scrum perspective) for an individual to be idle. The goal is for the team as a whole to be productive, and if that means the UI guy is idle part of time time in order for the team to be productive, so be it.

As your team matures, the team itself will learn how to optimize itself to avoid having team members with nothing to do. For example, team members can learn to write tests, or they can create documentation, or tackle technical debt, or internal support, or they can be perhaps be in conversations with the product owner about upcoming requirements.


问:
Large file / data transfer in a Microservice Architecture
My company is currently working on adopting a microservice architecture but we are encountering some growing pains (shock!) along the way. One of the key contention points we are facing is how to communicate large quantities of data between our different services.

As a bit of background we have a document store that serves as a repository for any document we might need to handle across the company. Interacting with said store is done via a service which provides a client with a unique ID and a location to stream the document. The document's location can later be accessed via a lookup with the provided ID.

The problem is this - Does it make sense for all our microservices to be accepting this unique ID as part of their API for the purposes of interacting with documents or not? To me this feels inherently wrong - the services are no longer independent and rely upon the document store's service. While I do acknowledge this might simplify API design and perhaps even have some performance gains the resulting coupling more than counterbalances the benefits.

Does anyone know how the rainbow unicorns (Netflix, Amazon, Google, etc.) handle large files / data exchange between their services?

答:
Does anyone know how the rainbow unicorns (Netflix, Amazon, Google, etc.) handle large files / data exchange between their services?

Unfortunately I do not know how they deal with such problems.

The problem is this - Does it make sense for all our microservices to be accepting this unique ID as part of their API for the purposes of interacting with documents or not?

It violates the Single Responsibility Principle, which should be inherently in your microservice's architecture. One microservice - logically one, physically many instances representing one - should be dealing with one topic.

In the case of your document store, you have one point, where all queries for documents go (of course you could split this logical unit up into multiple document stores for several kinds of documents).

If your "application" needs to work on a document, it asks the respective microservice and processes its result(s).

If another service needs an actual document or parts of it, it has to ask the document service.

One of the key contention points we are facing is how to communicate large quantities of data between our different services.

This is an architectural problem:

Decrease the need to transfer big amounts of data

Ideally, each service has all of it's data and needs no transfer to simply serve requests.
As an extension of this idea - if you have the need to transfer data, think of redundancy (*in a positive way_): Does it make sense to have the data redundant in many places (where they are needed)? Think of how possible inconsistencies might harm your processes. There is no transfer faster as actually none.

Decrease the size of the data itself

Think of how you could compress your data: Starting with actual compression algortihms up to smart data structures. The less goes over the wire, the faster you are.


答:
Where I work, we have a lot of silos of knowledge.

Not good.

The concern is that it doesn't make sense to have the GUI guy work on Networking stuff, when there is already someone that can (in terms of efficiency) get something done better because they are already familiar with it.

Except of course that it leads to this sort of scenario or the ever present "what happens if Bob gets hit by a bus?" problem.

How would I overcome this issue?

First, upper management doesn't get to see your estimates. They don't get to see person level assignments. They should never know you picked 21 over 5. They should see that at the end of the sprint, the team got cool stuff done.

Beyond that, you should cross train your people so that instead of 5 and 21 estimates you have 5 and... 15. After that, it is up to your project manager and/or team lead to make sure that your features are prioritized to space out the work. Don't line up 90 hours of renderer work as the top priority if you know the renderer expert has got 60 hours in the next sprint. It'll still happen sometimes, but managing the natural ebbs and flows of work vs. people is a common need, even with agile's focus on doing things once they're needed.

Title: In Agile SCRUM, how do you handle even distribution of work in a sprint between "experts"?

Body:
Where I work, we have a lot of silos of knowledge. Basically each member of the development team is the expert (and sometimes the only one that knows a thing) on certain pieces of the overall software. For example, one person might be the expert on the rendering engine of a game. Another is the guy that works on the GUI. Rarely do the two intermingle. The concern is that it doesn't make sense to have the GUI guy work on Networking stuff, when there is already someone that can (in terms of efficiency) get something done better because they are already familiar with it.

This situation seems to make sprint planning difficult in SCRUM. It's quite possible that people on the team have no tasks or are seriously underutilized in a specific sprint simply because there is nothing there they are the "best fit" to work on. This makes calculating velocity unreliable as it will vary based on who actually gets more assignments during one sprint versus the other.

How would I overcome this issue? It seems like the focus should be more on the tasks and what needs to be done and less on the specific people to work on them. I think upper management will always prefer things to be done as quickly as possible. And if they see that GUI developer estimates 21 story points on a task related to the Engine, where the Engine guy estimates 5 points, they will question why we chose 21 (The goal would obviously be knowledge sharing, but this will cause the team to work inefficiently overall).


答:
If the ID returned by your document store is the way to reference documents throughout the system, then it makes sense for all services to accept that 'Document ID' on their API when the service needs to know which document it needs to work with.

This does not necessarily create a tighter coupling between the services than needed. Services that need to access documents need to access the document-store service anyway and they need that ID to tell the store which document to access.

Services that don't access documents directly might need to pass the Document ID along, but to those services it would be just an arbitrary string that does not create a dependency.

Title: Large file / data transfer in a Microservice Architecture

Body:
My company is currently working on adopting a microservice architecture but we are encountering some growing pains (shock!) along the way. One of the key contention points we are facing is how to communicate large quantities of data between our different services.

As a bit of background we have a document store that serves as a repository for any document we might need to handle across the company. Interacting with said store is done via a service which provides a client with a unique ID and a location to stream the document. The document's location can later be accessed via a lookup with the provided ID.

The problem is this - Does it make sense for all our microservices to be accepting this unique ID as part of their API for the purposes of interacting with documents or not? To me this feels inherently wrong - the services are no longer independent and rely upon the document store's service. While I do acknowledge this might simplify API design and perhaps even have some performance gains the resulting coupling more than counterbalances the benefits.

Does anyone know how the rainbow unicorns (Netflix, Amazon, Google, etc.) handle large files / data exchange between their services?


答:
There is a file format called EDF, intended to serialize a stream of multi-channel, multi-sample-rate data. Using a file structure as example is good because it is a stream, just like a network stream or any other byte stream between the probe and the oscilloscope.

The idea of the EDF format is to multiplex the various data channels into data "blocks", where each block contain a given amount of time from each channel, and blocks are saved serially into file.

The file itself contains a file header with info about the file and the channels, then N channel headers containing info about each channel, and then you have only raw data. So, if hypothetically you lose header data, you could not (at least easily) recompose the signals.

At last, the numeric part (the raw data) of this file format is simple because it is rigid: every value is of type Int16 (short integer, two bytes, little-endian).

Now let's see how this file format could be useful in the context of network transfer:

Network transfer assumes you have a session, this session is configured (that is, you have a contract between sender and receiver), and session configuration does not change unexpectedly. This would mean you can get info from your probe (or conversely, set it up), and this config info should be enough for the receiver to know the structure of each data-frame. So, you would have two moments: one to configure the data-transfer (sending the "header" data between probe and oscilloscope), and the data-transfer itself (serially sending data-frames with a given format).

Now for the data-frame, it is (as usual) a byte stream/array with a given length, and it should encode the information someway. Usually, you use something similar to Type-Length-Value, where "Type" is a field of a predefined length describing "what" is coming, "Length" is another field describing how many bytes the following Length field will have, and finally the "Value" itself is the data itself.

From your question, configurable variables seem to be:

Number of channels;

Sampling rate for a given channel;

Numeric type: int, float, double;

I didn't quite understand what you mean by "binary" format, but I believe this would be compared to text (ascii, utf, etc.). Mind that every stream-based transfer would be composed of sequential bytes, and there are standard ways to represent numeric values as an array of bytes (float has 4 bytes, double has 8 bytes, Uint16 (aka short) has 2 bytes, etc) . Also, every language has its library to convert between typed numbers in memory and bytes in a stream.

Hope this helps, and comment something if you feel like, because I am currently working exactely with a custom binary file format for multi-channel data-acquisition in my current project, and we surely could exchange some thoughts about it.

Title: Data format for binary data transfer

Body:
I have to make an oscilloscope. Ideally, the oscilloscope connects to a probe. This probe should be another program, and could get the data locally or from a network. This data are numbers, floats and/or integers. The number of channels may vary. And they change with time. Not all channels need to have the same frequency.

Now, sometimes the frequency can be 1KHz, so using json is not ok because is wastes a lot of resources since is based in text. I'm looking for a binary data format, so the data transfer is faster.

So the first part of the question is: Do you know of a format to transfer binary data like this? It should have some kind of header, and then just numbers. My guess is that somebody must have had this problem before and there should be some kind of library to use this.

Then comes the second part of the question: What protocol should we use to transfer the data?

Maybe using json but with a base 64 encoder for the data using standart http calls could work, but for 1KHz signal that doesn't make sense. I wonder what people do to solve this.


答:
You might also want to consider looking up the subject of "ber tlv"

http://en.wikipedia.org/wiki/Type-length-value

This is the data stream format used by the worlds financial processing network, and is the backbone behind Chip & Pin terminals, ATM's and many other banking systems.

The best part about it is it's very simple to use.

The T,L,V part stands for

Tag

Length

Value

The Tag is a numerical identifier that specify's the type of message, in your case if your just making use of the format, you can most likely define your own tags for this, but if you where actually handling real financial data you'd have to use the tag numbers defined in the standard.

The Length part is again simple, it specify's how many bytes of data the value part is, like any value the more bytes you allocate to the length, the longer your payload can be.

The Value part is then just the payload for your actual message, and how you format the bytes for this is entirely up to you.

Beacuse it's such a simple to decode/encode protocol (You can do it entirely using byte arrays) you can easily sent it over UDP using a very small, very fast packet size.

It's also suitable for streaming, and multiple messages can be sent back to back in a continuous non closing connection, which is quite ideal if you decide you must use TCP.

It would in theory work well over HTTP too using a web socket, but I've never actually tried that so I couldn't comment on how well.

As for libraries supporting it, last time I looked "Craig's Utility Library"

https://github.com/JaCraig/Craig-s-Utility-Library

had very good support for TLV based structures in it, as do many of the smartcard libraries that are floating around (TLV is used on a lot of cards too)

If TLV is not your thing, then I'd definately back up what others have said and take a good close look at "Protobuf"

Update

I don't know what language your working in, or even if what I'm about to post will be of any use to you :-) , but here goes anyway.

This is a TLV decoder (Note it has no encoding capabilities, but it should be easy to reverse it) I wrote back in 2008(ish) using C# , it was designed for decoding ber-tlv packets coming off a smart card in a payment terminal, but it might serve as a starting point for you to hack it into a more useful shape.

```<code>using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Card_Analyzer
{
  public class tlv
  {
    public int tag = 0;
    public int length = 0;
    public byte tagClass = 0;
    public byte constructed = 0;
    public List<byte> data = new List<byte>();
  }

  public class tlvparser
  {
    // List of found TLV structures
    public List<tlv> tlvList = new List<tlv>();

    // Constructor
    public tlvparser(byte[] data)
    {
      if (data != null)
      {
        this.doParse(data);
      }
    }

    // Main parsing function
    public void  doParse(byte[] data)
    {
      int fulltag = 0;
      int tlvlen = 0;
      int dptr = 0;

      while (dptr < data.Length)
      {
        byte temp = data[dptr];
        int iclass = temp & 0xC0;
        int dobj = temp & 0x20;
        int tag = temp & 0x1F;

        if (tag >= 31) // Using extracted vars, decide if tag is a 2 byte tag
        {
          fulltag = (temp << 8) + data[dptr + 1];
          tlvlen = data[dptr + 2];
          dptr += 3;
        }
        else
        {
          fulltag = temp;
          tlvlen = data[dptr + 1];
          dptr += 2;
        }// End if tag 16 bit

        if ((tlvlen & 128) == 128)
        {
          tlvlen = (tlvlen << 8) + data[dptr];
          dptr++;
        }

        tlv myTlv = new tlv();
        myTlv.tag = fulltag;
        myTlv.length = tlvlen;
        myTlv.tagClass = Convert.ToByte(iclass >> 6);
        myTlv.constructed = Convert.ToByte(dobj >> 5);

        for (int i = 0; i < tlvlen; i++)
        {
          if(dptr < data.Length)
            myTlv.data.Add(data[dptr++]);
        }

        if (myTlv.constructed == 1)
          this.doParse(myTlv.data.ToArray());

        tlvList.Add(myTlv);

      }// End main while loop
    }// End constructor
  }// end class tlvparser
}// end namespace
</code>```

If it's of no use, then feel free to just ignore it.

Title: Data format for binary data transfer

Body:
You might also want to consider looking up the subject of "ber tlv"

http://en.wikipedia.org/wiki/Type-length-value

This is the data stream format used by the worlds financial processing network, and is the backbone behind Chip & Pin terminals, ATM's and many other banking systems.

The best part about it is it's very simple to use.

The T,L,V part stands for

Tag

Length

Value

The Tag is a numerical identifier that specify's the type of message, in your case if your just making use of the format, you can most likely define your own tags for this, but if you where actually handling real financial data you'd have to use the tag numbers defined in the standard.

The Length part is again simple, it specify's how many bytes of data the value part is, like any value the more bytes you allocate to the length, the longer your payload can be.

The Value part is then just the payload for your actual message, and how you format the bytes for this is entirely up to you.

Beacuse it's such a simple to decode/encode protocol (You can do it entirely using byte arrays) you can easily sent it over UDP using a very small, very fast packet size.

It's also suitable for streaming, and multiple messages can be sent back to back in a continuous non closing connection, which is quite ideal if you decide you must use TCP.

It would in theory work well over HTTP too using a web socket, but I've never actually tried that so I couldn't comment on how well.

As for libraries supporting it, last time I looked "Craig's Utility Library"

https://github.com/JaCraig/Craig-s-Utility-Library

had very good support for TLV based structures in it, as do many of the smartcard libraries that are floating around (TLV is used on a lot of cards too)

If TLV is not your thing, then I'd definately back up what others have said and take a good close look at "Protobuf"

Update

I don't know what language your working in, or even if what I'm about to post will be of any use to you :-) , but here goes anyway.

This is a TLV decoder (Note it has no encoding capabilities, but it should be easy to reverse it) I wrote back in 2008(ish) using C# , it was designed for decoding ber-tlv packets coming off a smart card in a payment terminal, but it might serve as a starting point for you to hack it into a more useful shape.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Card_Analyzer
{
  public class tlv
  {
    public int tag = 0;
    public int length = 0;
    public byte tagClass = 0;
    public byte constructed = 0;
    public List<byte> data = new List<byte>();
  }

  public class tlvparser
  {
    // List of found TLV structures
    public List<tlv> tlvList = new List<tlv>();

    // Constructor
    public tlvparser(byte[] data)
    {
      if (data != null)
      {
        this.doParse(data);
      }
    }

    // Main parsing function
    public void  doParse(byte[] data)
    {
      int fulltag = 0;
      int tlvlen = 0;
      int dptr = 0;

      while (dptr < data.Length)
      {
        byte temp = data[dptr];
        int iclass = temp & 0xC0;
        int dobj = temp & 0x20;
        int tag = temp & 0x1F;

        if (tag >= 31) // Using extracted vars, decide if tag is a 2 byte tag
        {
          fulltag = (temp << 8) + data[dptr + 1];
          tlvlen = data[dptr + 2];
          dptr += 3;
        }
        else
        {
          fulltag = temp;
          tlvlen = data[dptr + 1];
          dptr += 2;
        }// End if tag 16 bit

        if ((tlvlen & 128) == 128)
        {
          tlvlen = (tlvlen << 8) + data[dptr];
          dptr++;
        }

        tlv myTlv = new tlv();
        myTlv.tag = fulltag;
        myTlv.length = tlvlen;
        myTlv.tagClass = Convert.ToByte(iclass >> 6);
        myTlv.constructed = Convert.ToByte(dobj >> 5);

        for (int i = 0; i < tlvlen; i++)
        {
          if(dptr < data.Length)
            myTlv.data.Add(data[dptr++]);
        }

        if (myTlv.constructed == 1)
          this.doParse(myTlv.data.ToArray());

        tlvList.Add(myTlv);

      }// End main while loop
    }// End constructor
  }// end class tlvparser
}// end namespace

If it's of no use, then feel free to just ignore it.


问:
How to plan your software project if you are work depend on required deliverable with unexpected delivery date
I have worked for many software projects where I am developing software solution to the customer where I need to integrate with the customer back-end using web service provided by the customer in order to retrieve information, etc...

The problem is the customer might not deliver the required web service on time. Or might deliver web service in a different sequence than the plan.

My question is how to plan the project so that my coding and implementation will start according to the delivered required web services? Should I consider using agile? an how to use it in that case?

And how to deal with the risk of the delaying from customer?

The solution can be divided into phases and might be delivered based on the readiness of the web service.

Suppose I require 16 web service. If the customer deliver three now  I can implement part of the project and go live with it. After if customer develop another two web service I can be ready to implement another part of the project. and so on
How to put that in the plan structure ?

答:
The important thing is to develop an open, transparent and trusting relationship with the client.  The customer needs to understand what your challenges - and cost - are to work in the manner you are being forced to work in.  At the same time, you need to understand why is it that the client cannot deliver in a predictable manner.  Once there is appreciation for both sides, usually solutions will emerge that will satisfy both sides.

But it takes time to get there with the client.  There are a few things you could do to make it easier to have such a discussion:

Set up a Kanban board that highlights and visualizes the work being done by your customer and you.  A simple example is the one shown below:

The board visualizes the process being used by both sides - and the work being done at any point of time.  The numbers next to the column headers are WIP (work-in-progress) limits - the maximum number of work that can go on at any stage at any point of time.  If you are the only worker on your team, for example, then the WIP limit for the entire second row should ideally be just 1. For now, ignore these numbers and simply put up the board with your and the customer's process so you and the customer can see what is going on.

Work out an agreement with the customer on the minimum number of web-services that should always be available in your input queue.  So, for example, the last column in the first row (Customer - Done) must have at least 2 or 3 web-services (minimum WIP limit) so when you finish working on one, you have a second one always available to start on.  So, initially, you may have a bit of a lag as you wait for the customer to complete at least 203 web-services, but from then on, you should always have work to do.

The overall sequencing of the web-services depends on your overall design - and when you need which services broadly.  That needs to be done initially, with some leeway given for the variations in the actual delivery that you have come to experience.

Based on that design, you might identify initially, the set of web-services you need to deliver each 'feature' or "MVP" (minimum viable product) - that you can actually demonstrate working.

The advantage of using the Kanban board is that you can use it to show the customer the problem you are facing much more effectively than by just describing it - a picture is worth a 1000 words - and probably 100s of 1000s of dollars!  To build the board, you could use a physical board with stickies or an electronic Kanban board - especially, if your customer is in a remote location.

Hope this helps.


问:
How exactly do exclusive code rights work?
I was asked by a client to submit a proposal for a web-app he wants to built. This client is a startup with a relatively good traction so far (as it seems).

I have been asked to draft up a contract that prevents me from writing any of the code I write for them anywhere else. Up to this point I was drafting up contracts where I retain the exclusive rights to my source code but the client retains all rights as well (selling, modifying etc.)

So this sounds pretty bizarre to me:

What If I have another client with a similar (but not exactly same) project and he wants me to develop his application?

Of course I won't copy and paste from the previous project - but it sounds to me that I won't be able to use any of the insights I gained in the previous project and I won't be able to write this code again for any client.

So the question is - If I give all the rights of my source code to the client, does that mean I am never able to rewrite this code for someone else?

Note: I know most of you here are not lawyers and I'm not really looking for legal advice, rather just what these type of contracts really mean in terms of ownership to the source code, what's the usual thing to do etc.

答:
Ownership of the code means that you will be assigning copyright to them.  In practical terms, that means that you will not retain any of their source code when the project is finished.  That way, you can't be accused of reusing the code you wrote for them in other projects.  This is a fairly typical arrangement; they are paying you to write code for them, so naturally they want to retain ownership of said code.

If there is other intellectual property that needs to be protected (i.e. some proprietary algorithm), then that algorithm needs to be dealt with specifically.  In particular, the innovation needs to be novel, and not easily reproducible.  Ideally, it is covered by a patent, but you can still be asked to sign non-disclosure agreements.  You should abide by those agreements, and refrain from using the proprietary algorithms in other projects.

The key here is specificity.  Your client should make it clear specifically what intellectual property and ideas they want to protect.  Don't sign anything that casts a wide net like "you can't use any ideas you've obtained in other projects."  That's an unreasonable demand.  Don't sign a non-compete agreement either; it may prevent you from working on industry-related or competitor's projects.


答:
This sounds pretty normal to me.  The startup is being careful to be able to show that they fully own all of the code in their solution.  This is very important if they are eventually purchased by another company.  That other company needs to know that no one is going to later claim ownership to the software they thought they bought.

You might want to research the term 'work for hire'.

The short version is that you do your work, they pay you for it, then you hand over all copies of the code you wrote.  Unless your contract says otherwise, this doesn't necessarily prevent you from writing similar code for another client (assuming of course that you don't share libraries or cut & paste code).

You may want to consult a lawyer about specifics.

Title: How exactly do exclusive code rights work?

Body:
I was asked by a client to submit a proposal for a web-app he wants to built. This client is a startup with a relatively good traction so far (as it seems).

I have been asked to draft up a contract that prevents me from writing any of the code I write for them anywhere else. Up to this point I was drafting up contracts where I retain the exclusive rights to my source code but the client retains all rights as well (selling, modifying etc.)

So this sounds pretty bizarre to me:

What If I have another client with a similar (but not exactly same) project and he wants me to develop his application?

Of course I won't copy and paste from the previous project - but it sounds to me that I won't be able to use any of the insights I gained in the previous project and I won't be able to write this code again for any client.

So the question is - If I give all the rights of my source code to the client, does that mean I am never able to rewrite this code for someone else?

Note: I know most of you here are not lawyers and I'm not really looking for legal advice, rather just what these type of contracts really mean in terms of ownership to the source code, what's the usual thing to do etc.


问:
Are mocks in unit tests dangerous in dynamic languages?
I've started relying heavily on a mocking framework in php for my unit tests.

My concern is that with a dynamic language, there is no way of enforcing a return type. When mocking, you have to ensure that the return type of your mocked method is correct.

Take for example the following interface

```<code>interface MyInterface {

  /**
   * @return SomeObject
   */
   public function myMethod();

}
</code>```

If we are unit testing a class which requires an instance of MyInterface, we would mock the myMethod function to return a known SomeObject.

My concern is what happens if we accidentally have our mocked method return a different type of object, e.g. SomeOtherObject, and SomeOtherObject and SomeObject are not the same duck type (don't have the same methods). We'll now have code that is relying on havingSomeOtherObject, when all implementations ofMyInterfacemyMethodwill returnSomeObject`.

For example, we could write the following production code;

```<code>$someOtherObject = $myInterfaceImp->myMethod();
$newObj = new NewObject($someOtherObject);
</code>```

Hopefully the issue is obvious. We are passing what we think is SomeOtherObject into a constructor. Our tests will pass, but its very likely that in production we will get failures as myMethod() will actually be returning SomeObject. We then try passing this into the constructor, and get an error if there is a type check, or a non existent method is called.

Unless we write very detailed integration and acceptance tests using the real implementations (basically we'd have to ensure they covered all the testing we do an the unit level) we may not ever realise that we have an issue until we receive some runtime error in production.

Other than developer discipline, is there any way to mitigate the risk of incorrect mocks and implementations?

答:
The answer to this question "are mocks in unit tests dangerous in dynamic languages?" is "Hell yes!"

The following article explains the dangers.

They describe a scenario where an interfaces method name changed. For example, changing a methods name from error() to errors(). If we are mocking the error() method in our tests, they will never turn red to alert us that we have a problem.

Instead, mocks should be used sparingly. Behaviours of the unit under test should be tested rather than testing implementation details, which happens as a consequence of using mocks in unit tests. Real classes should be used as dependencies, instead of an over reliance on mocks.


问:
What is your favorite whiteboard interview problem?
Just as the title says, what is your favorite whiteboard interview problem, and why has it proven effective for you?

Junior, senior, Java, C, Javascript, PHP, SQL, pseudo-code, etc.

答: "Draw for me on the whiteboard the design of the last project you worked on, without revealing me any sensitive detail."</p>


问:
Token based authentication and multiple sessions
I've a token based authentication system (REST) that I inherited for an iOS app (can't change), and I've to re-use the same authentication web api system (that I can change to adapt for the web requests while still accommodating iOS app).

Here's how the authentication system works.

username, password -> if valid a token is returned to the user and also saved to the database

getNewToken -> passes the old authToken, web api verifies from the table, issues a new token, updates the database table

1 is triggered by user login while 2 is automated interval based call every 15 minutes by the iOS app (I guess to keep the session alive, like heart beat)

Now when user is on the web and logs in, I call #1 to get the token but then if user is already on the device, the old token at device won't work because it got updated on the server as a result of user signing on the web.

This makes me wonder and in the light of above scenario my question is how multiple sessions are handled using authentication token system out there in real life, for instance I could open gmail or Facebook in two different browsers and both sessions are maintained (I'm not sure if they are using token based system or some other but let's say they are for an example). Please advise.

答: Yes if id is id of the device cause you need to store device id in some place. If you can install programs on the server better to store login tokens not in database but in some external key value storage like redis or memcached, if you can do it of course.</p>


问:
What is the best type to represent a number in java and why?
I am developing a dynamically typed, interpreted programming language, which is interpreted by a runtime written in Java. As Java is statically typed, I need to define how the numbers used in the language are stored in the interpreter.

My initial idea was to use BigDecimal for all calculations, but this makes the code more complex and I assume has added overhead for the runtime. Another possibility is to parse the number, if it is a natural then use a long and if it a real number use a double.

So which solution is optimal, and why?

答: All the numeric types can represent different numbers and have different semantics. Stop thinking about implementation concerns for a second and do language design. What kind of numeric types should your language offer its users? (Note that most languages offer multiple types, and for good reasons.) Find the answer to that, then implement it.


答: Depending on the level of specialization you might consider some type of implementation of pair-programming. If somebody is under-utilized during a sprint, don't worry about velocity, and have them pair up with somebody who is being utilized in an area of expertise that they aren't familiar with. The obvious benefit of this being that members of your team will build skills in a wider variety of areas towards the end of everybody being capable of doing anything.


答:
Why not both?

In an interpreter for a dynamically typed language, you will need a common representation for all values. For example, we might have a function that can return either a number or a dictionary. In JavaScript:

```<code>function foo(want_dict) {
  if (want_dict) return { value: 42 };
  else return 42;
}

var result = foo(someVariable);
</code>```

So, how could a dynamic language represent the contents of the result variable? Since the variable could be either a number or a dictionary, the variable does not have a specific type. Instead, the type information lives in the value itself, often in form of a type ID. In C, a value might be represented as

```<code>typedef struct {
    unsigned type_id;
    void *value;
} Value;
</code>```

In Java, we could use a similar class:

```<code>class Value {
  private TypeId type;
  private Object value;
  public Value(TypeId type, Object value) {
    this.type = type;
    this.value = value;
  }
  public TypeId type() { return type; }
  public Object value() { return value; }
}
</code>```

We could then have various values such as

```<code>return new Value(TypeId.Int,  new Integer(42));
...
HashMap<String, Value> rawValue = new HashMap<>();
rawValue.put("value", new Value(TypeId.Int, new Integer(42)));
return new Value(TypeId.Dict, rawValue);
</code>```

When we try to use such a value, we first have to dynamically check that all values have acceptable types. E.g. the interpreter's implementation of addition might look like this:

```<code>Value doAddition(Value a, Value b) {
  if (a.type() != TypeId.Int)
    throw ...;
  if (b.type() != TypeId.Int)
    throw ...;
  return new Value(TypeId.Int, (Integer) a.value() + (Integer) b.value());
}
</code>```

Alternatively, we could leverage the dynamic typing features of Java itself, i.e. type information is encoded in an inheritance hierarchy.

```<code>interface Value<T> {
  public T value();
}

class MyInt implements Value<Integer> {
  private int value;
  public MyInt(int value) { this.value = value; }
  @Override public Integer value() { return value; }
}

class MyDict implements Value<Map<String, Value>> {
  private Map<String, Value> value;
  public MyInt(Map<String, Value> value) { this.value = value; }
  @Override public Map<String, Value> value() { return value; }
}
</code>```

Now, we can create various values like

```<code>return new MyInt(42);
...
HashMap<String, Value> rawValue = new HashMap<>();
rawValue.put("value", new MyInt(42));
return new MyDict(rawValue);
</code>```

With this encoding, an implementation of an addition operator would look like this:

```<code>Value doAddition(Value a, Value b) {
  if (!(a instanceof MyInt))
    throw ...;
  if (!(b instanceof MyInt))
    throw ...;
  return new MyInt(a.value() + b.value());
}
</code>```

So, both encodings look very much the same, but I would prefer an explicit tag since it's a bit more flexible, and is not limited by Java's type erasure (on the downside, everything is an Object and needs to be cast).

With dynamic typing we necessarily have some kind of runtime type support available, so we can trivially support multiple numeric types. In one encoding, we just have to add a new type ID, in the other another subclass:

```<code>class MyRat implements Value<Double> {
  private double value;
  public MyRat(double value) { this.value = value; }
  @Override public Double value() { return value; }
}
</code>```

We can also make our implementation of addition polymorphic, so that the same operator can handle both integers and doubles:

```<code>Value doAddition(Value a, Value b) {
  if ((a instanceof MyInt) && (b instanceof MyInt))
    return new MyInt(a.value() + b.value());
  if ((a instanceof MyRat) && (b instanceof MyRat))
    return new MyRat(a.value() + b.value());
  throw ...;
}
</code>```

That wasn't so hard!

The difficult part is deciding:

Which numeric types do we want to support in our language? Only floating point numbers? (Javascript) Only integral types? (B) Both? (most languages) Both, but as a single type and we switch to doubles if the number would otherwise overflow? (Perl)

Which sizes to we want to support? Native sizes for efficiency? Arbitrary-precision numbers? A collection of different width types? Do we want to support unsigned types?

Most of these problems can be answered when you think about what numbers will be used for in the language. E.g. doubles are useless when indexing an array, since not every natural number has a representation as a double. Integers are useless for non-discrete domains such as physical measurements. Doubles are useless for currency calculations (which are discrete). Integer overflow is useless, but efficient. And so on. As the other answer points out, this is ends up being a question of language design, but it's not too difficult to implement any choice you arrive at.

Title: What is the best type to represent a number in java and why?

Body:
I am developing a dynamically typed, interpreted programming language, which is interpreted by a runtime written in Java. As Java is statically typed, I need to define how the numbers used in the language are stored in the interpreter.

My initial idea was to use BigDecimal for all calculations, but this makes the code more complex and I assume has added overhead for the runtime. Another possibility is to parse the number, if it is a natural then use a long and if it a real number use a double.

So which solution is optimal, and why?

Body:
Why not both?

In an interpreter for a dynamically typed language, you will need a common representation for all values. For example, we might have a function that can return either a number or a dictionary. In JavaScript:

function foo(want_dict) {
  if (want_dict) return { value: 42 };
  else return 42;
}

var result = foo(someVariable);

So, how could a dynamic language represent the contents of the result variable? Since the variable could be either a number or a dictionary, the variable does not have a specific type. Instead, the type information lives in the value itself, often in form of a type ID. In C, a value might be represented as

typedef struct {
    unsigned type_id;
    void *value;
} Value;

In Java, we could use a similar class:

class Value {
  private TypeId type;
  private Object value;
  public Value(TypeId type, Object value) {
    this.type = type;
    this.value = value;
  }
  public TypeId type() { return type; }
  public Object value() { return value; }
}

We could then have various values such as

return new Value(TypeId.Int,  new Integer(42));
...
HashMap<String, Value> rawValue = new HashMap<>();
rawValue.put("value", new Value(TypeId.Int, new Integer(42)));
return new Value(TypeId.Dict, rawValue);

When we try to use such a value, we first have to dynamically check that all values have acceptable types. E.g. the interpreter's implementation of addition might look like this:

Value doAddition(Value a, Value b) {
  if (a.type() != TypeId.Int)
    throw ...;
  if (b.type() != TypeId.Int)
    throw ...;
  return new Value(TypeId.Int, (Integer) a.value() + (Integer) b.value());
}

Alternatively, we could leverage the dynamic typing features of Java itself, i.e. type information is encoded in an inheritance hierarchy.

interface Value<T> {
  public T value();
}

class MyInt implements Value<Integer> {
  private int value;
  public MyInt(int value) { this.value = value; }
  @Override public Integer value() { return value; }
}

class MyDict implements Value<Map<String, Value>> {
  private Map<String, Value> value;
  public MyInt(Map<String, Value> value) { this.value = value; }
  @Override public Map<String, Value> value() { return value; }
}

Now, we can create various values like

return new MyInt(42);
...
HashMap<String, Value> rawValue = new HashMap<>();
rawValue.put("value", new MyInt(42));
return new MyDict(rawValue);

With this encoding, an implementation of an addition operator would look like this:

Value doAddition(Value a, Value b) {
  if (!(a instanceof MyInt))
    throw ...;
  if (!(b instanceof MyInt))
    throw ...;
  return new MyInt(a.value() + b.value());
}

So, both encodings look very much the same, but I would prefer an explicit tag since it's a bit more flexible, and is not limited by Java's type erasure (on the downside, everything is an Object and needs to be cast).

With dynamic typing we necessarily have some kind of runtime type support available, so we can trivially support multiple numeric types. In one encoding, we just have to add a new type ID, in the other another subclass:

class MyRat implements Value<Double> {
  private double value;
  public MyRat(double value) { this.value = value; }
  @Override public Double value() { return value; }
}

We can also make our implementation of addition polymorphic, so that the same operator can handle both integers and doubles:

Value doAddition(Value a, Value b) {
  if ((a instanceof MyInt) && (b instanceof MyInt))
    return new MyInt(a.value() + b.value());
  if ((a instanceof MyRat) && (b instanceof MyRat))
    return new MyRat(a.value() + b.value());
  throw ...;
}

That wasn't so hard!

The difficult part is deciding:

Which numeric types do we want to support in our language? Only floating point numbers? (Javascript) Only integral types? (B) Both? (most languages) Both, but as a single type and we switch to doubles if the number would otherwise overflow? (Perl)

Which sizes to we want to support? Native sizes for efficiency? Arbitrary-precision numbers? A collection of different width types? Do we want to support unsigned types?

Most of these problems can be answered when you think about what numbers will be used for in the language. E.g. doubles are useless when indexing an array, since not every natural number has a representation as a double. Integers are useless for non-discrete domains such as physical measurements. Doubles are useless for currency calculations (which are discrete). Integer overflow is useless, but efficient. And so on. As the other answer points out, this is ends up being a question of language design, but it's not too difficult to implement any choice you arrive at.