With this screencast I want to show you how to document your REST APIs with Swagger specification. Simply, your code synchronized with your documentation.
When you start a project, one of the first task to do is to design the application model. Some people associate this task with the design of the relational database model. When you have the model designed, you have the 80% of the application done. In fact, if you are working with a Oriented Object language, you should design your model in terms of objects first. Relational databases are one option to implement the persistency requirement, but as you know is not the only way. Hence, if you are thinking your model in terms of tables and so on, you are coupling your business with a concrete technology. Then if you choose a OO language like Java, you have to design the model in terms of objects. In fact , for this reason exist tools like ORMs (hibernate, Ibatis,...) or APIs like JPA, to decouple your model from the persistence technology chosen.
I think that they are some key questions to help you to do a good choice:
Do you need ACID transactions ? Is it critical for your business the inconsistency (for example ghost reads) ? If not NoSQL could be a good option.
Do you need storage procedures or triggers at the persistency tool level? If you can manage this from your code, NoSQL could be a good option.
Do you need a rigid scheme? If not NoSQL could be a good option.
Is the consistency critical? If not NoSQL could be a good option.
Do you need to support a very large data loads? NoSQL can scale better than RDBMS. Normally RDBMS scales with more hardware.
Do you need to support a lot of concurrent users? NoSQL again.
Do you need a very consistent performance ? NoSQL again.
Think in your requirements, imagine you are a driver. If you want to win a race, your skills are important, but also your car. You need a competitive car. You don't need a comfortable car, with radio, alarm , leather seats, or whatever else. What you need is a faster car, with good brakes and powerful engine, nothing more. Something similar happens with a RDBMS and a NoSQL system.
Dependency Injection and Service Locator, two different ways to implement Inversion of control principle.
A dependency injector container acts as an assembler of objects, decoupling your business logic from their dependencies. But sometimes is more complex to debug when we have a issue.
On the other hand, Service Locator hides the implementation details too, but your class has dependency with the Service Locator class. Only, when this dependency is not a problem, the Service Locator pattern is a good choice.
Most important of all, is to split the configuration code from the use code. Take it into account , and analyze when one option is better than the other.
With this post, I want to show you step by step the process of transforming a static and rigid code, to something more flexible and reusable using lambda expressions. Let's stop talking, let's to see code.
Imagine you have a selling car business, and you want an application to filtering the cars by brand. Then your business logic could look like this.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
That it's ok. But now you want to filtering by brand and color too. You have to add a new method in order to support the new functionality. In this case, we can do it easily.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
What can we say about this code? In front of any new requirement, a new method is implemented. Then we have the phenomenon of method explosion.
We can try to do it better with a new interface called CarChecker.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Yes, I know what are you thinking. Now we have a generic method, but now we have class explosion instead of method explosion. You are right. We can try to do it with anonymous classes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
But CarChecker it's a functional interface. We can use lambda expressions.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
At this point, our code looks cool. But Java 8 includes something called Predicate. Then we can rewrite our business logic to this.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Usually, when you filter a collection of objects then you want to do something with the result. We can try to generalize a function to processing the result of cars filtering using Consumer interface, included also by Java 8.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Java 8 has introduced three new features about functional programming.
Lambda expressions.
Higher-order functions.
Streams API.
I think that Java 8 did a great step to integrate functional programming in its platform. But if you want to codify in a declarative way, or in other words, you want to tell to your computer what you want to compute instead of how, you need more expressiveness or abstractions. Maybe Java 9 will provide new features to make our programs more declarative. But until now, libraries like Javaslang fills this gap, giving steroids to Java 8.
Javaslang provides structures like Tuple, Either, Match, Laizy, Promise, Try, and so on. Most of them available in languages like Scala, Haskell,...
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
It looks like a regular POJO. And you are right, it's regular POJO, until here all it's ok.
Now, imagine your main program needs to instantiate our class Car 10 times. Then, the java heap memory will look like something like that.
What if I tell you that there are only two types of engines and two different colors ? Do you think that the memory allocation can be optimized to something that looks like the following picture ?
The answer is yes, with the Flyweight pattern. First of all, we have to design our light car with the common attributes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
After that, the next step is the container which contains all different types of light cars. It shall contain 4 different car types (2 engines x 2 colors).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Then our Car class looks like something like that (pay attention to the new interface created Car and the delegation through LightCar).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Is this boilerplate code familiar for you? Null checking everywhere.
To give some historical context, Tony Hoare - one of the giants of computer science - wrote, "I call it my billion-dollar mistake. It was the invention of the null reference in 1965. I couldn't resist the temptation to put in a null reference, simple because it was so easy to implement."
What can you do to prevent unintended null pointer exceptions ? You can be defensive and add checks to prevent null references like you see in the above code, or you can use one of the amazing stuffs of Java 8 like the Optional pattern. We are going directly to the action, let me show you some code!
Creating Optional objects
An empty Optional.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
A non null Optional. If audioSystem were null, a NullPointerException would be thrown. Take it into account.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Finally, you can create an Optional from a null reference. If audioSystem were null, the resulting Optional would be an empty Optional.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
or you can use it the imperative way (not recommended)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Extracting and transforming values using the map method
Instead of this
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
The purpose of Optional is not to replace every single null reference in your codebase but rather to help design better APIs in which—just by reading the signature of a method—users can tell whether to expect an optional value. In addition, Optional forces you to actively unwrap an Optional to deal with the absence of a value; as a result, you protect your code against unintended null pointer exceptions.
I would like to share with you guys an article that form me is pretty interesting. The sentence to remember is the following one:
Private methods are for hiding implementation and we don’t want to test implementation details when we do TDD, that’s better achieved as part of a QA effort, if needed.
This is the new trend. Where something works, the next step is socialize it.
In other words, Bintray is like a "Maven Central" but with a social layer. I have to say that it works very well, it is very easy to deploy your own repositories following a simple guided steps. Also is very easy to pass your atifacts to JCenter or Maven Central.
Today, I want to show you guys an API which I developed to create commands in a fluent way. This API is based on the Builder pattern, to provide us something similar to a DSL (Domain Specific Language), in this case to create commands.
Note that this API provides a model based on Command, ErrorHandler and CommandRepository objects. With these three classes, you can build any complex command you want, applying patterns like Decorator, Composite, Visitor and so on.