Practical Use of Artificial Intelligence in Oncology and Genetics

Cancer is a devastating disease causing mortality to millions of people globally. I introduce practical use of Artificial Intelligence in Oncology. This chapter covers how AI, deep learning, deep neural networks (DDNs) contribute to cancer, genetic, epigenetics research. Transcriptome profiles from pathological images ImageNet,

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




The Adapter Pattern

I was recently working on a project where I wanted to read a file and send it directly onto a socket. I kind of assumed that Java would make this easy. And it does: InputStream.transferTo.

My code could therefore look like this:

Nice, simple and clean. Perfect! But, sadly, this is a Java 9 feature, and I was working on a project which (due to various incompatibility issues) could not use Java 9. So I was left with having to do this manually, which looked like this:

The difference here is stark.

The Java 9 code is very clear. It declares: take the input stream and transfer it to the output stream.

The Java 8 code describes the process of transferring the data from the input to the output stream. It therefore requires the reader to follow a series of steps in order to understand what is going on and then (in their head) give that process a name:

The Java 8 code could easily be improved by extracting this process into a method:

The limitation here is that this code needs to live somewhere. Should it be in a particular class that uses the transfer process? But what if this transfer were to be used in many different, unrelated places? It is, after all, a fairly generic process. So it could be a static method in a helper class: StreamHelper.transferInputToOutput perhaps?

But, given I knew that Java 9 has a baked-in solution to this problem, and this was implemented as an instance method on the InputStream class, it seemed to make most sense to mirror this interface.

However, as InputStream is a Java core class, I could not modify it directly even if I had wanted to. A better solution was to adapt the InputStream class by adding the transferTo functionality at runtime. This seemed a perfect opportunity for an Adapter.

Here’s my implementation:

Now, my code can look like this:

Which gets us most of the way to code as clean as the Java 9 example!

Add a comment

Related posts:

Why we hate banks

For those who do not know me, my name is Shahar Larry. I’ve spent most of my career as an innovation consultant and an entrepreneur. Over the past 6 months, I have been making my first steps into the…

Update from Healing Central

In late May I experienced a setback that I believe was precipitated by a trip to north of 10,000 feet. While one could circle endlessly around the drain of, “why did I get this”, I tend more towards…

The Rent and Mortgage Cancellation Act

Are you a landlord or mortgagee? I got some bad news for you. A bill recently introduced by Rep. Ilhan Omar (D-MN) and supported by a sizable group of Congressional progressives, including all four…