java - How to avoid coupling two methods that have a similar implementation now, but which may change in the future? -


I find two ways in a class that currently share a very similar implementation, but one Demonstrate the implementation which is quite expensive.

Sample:

  class examples {public zero process (object obj) {boolean certain difference = found forestfact (obj); Boolean Certain Object Fact = Milte-Second Fact (OBJ); // Use two facts in any way) Public Boolean GO Forfact (Object OBJE) {Boolean Data = Some Exclusive Method (OBJ); // return some values ​​derived from the data} Get Public BooleanConnect (Object OBJE) {Boolean Data = Some Expensive Mode (OBJ); // return any other values ​​derived from the data} Public Boolean Certain Essential Method (object obj) {...}}  

Ive somehow thought about caching results of something It is excessive, but it seems useless, the things given are given, processed and then discarded. It also seems that - it is necessary to know about a cache, or I need to cache the results in the SSMXApendive Math.

Even short term cash can be bad news, since literally millions of objects are processed every day.

My concerns are two times - Firstly, there is no guarantee that both of these methods will always depend on the third, so any solution should be transparent with your POV, and the other thing is that Clear solutions (caching in some expensive meth) can be very costly in terms of place for those results, which do not need to be kept for long periods of time.

I caching the results of certain methods It is thought of, but it seems useless, given that things come, processed and then discarded.

I do not think how it is worthless. It's basically a cache work. You compare the items that you have recently processed, and when you receive "hits", you avoid the cost of calling someExpensiveMethod .

Caching will actually depend on a number of factors that work for your application such as:

  • The number of objects / result pairs that you can put in your cache,
  • In a "Hit" and "Miss" cases),
  • The average cost of calling Certain ways
  • >
  • Direct costs of maintaining a cash; such as if you use LRU or some other strategy to get rid of cash entries that do not help And,
  • Indirect costs of maintaining cache.

(The final point is predict / difficult to measure, but additional memory required to represent cache structures in it Also, what GC has to do to deal with the fact that the cash and its content are "accessible", and the GC overheads related to weak references ... assuming that you use them.)

Finally, the caching solution Success (or otherwise) is fixed in the case of average behavior of the system for realistic The workload is the fact that some cached results are never used anymore Not relevant in

It also seems that it is necessary to know ways about a cache, or give me someExpensiveMethod .

Again, the IMO is not in any way "clanky" it is the way that you implement caching.

Even short-term cash can be bad news, because literally thousands of objects are processed every day.

I do not see the logic of your argument, if millions of objects are processed one day and you value (say) the last 5 minutes, then only thousands of objects Cache in the cache. This is hardly a "bad news".

If you are doing "really millions" items in a day actually

  • some special ways can not be expensive ... unless you have either a very effective caching and a lot of memory, or large number of processors, Or both ,
  • Avoid your concerns and coupling of elegance should should be secondary to the issue of designing the application. So it continues, and
  • You may have a multiprocessor will need to run, and you may arise fact a cache a concurrency barrier that will need to deal with.

Comments