Module 9 - 04

Dick Wall
A free video tutorial from Dick Wall
Scala Developer
4.6 instructor rating • 7 courses • 21,678 students

Learn more from the full course

Scala Advanced, Part 2 - Best Practices, Async, Serializing

Idioms and Patterns, Dependency Injection, XML, JSON, Custom Extractors and Asynchronous Programming with Futures

05:42:07 of on-demand video • Updated March 2018

  • Be aware of the latest best practices, patterns and idioms in Scala programming
  • Save and load data into Scala, taking full advantage of the Scala type system to obtain rich, useful types
  • Use Scala Futures to program asynchronously and reactively, and implement more parallelization and performance in your code
English [Auto] So how about the gang of four patents. These are mostly object oriented centric patents. This was from a time when Object Orientation was really the the the way people wrote software. And the most of them have just been incorporated into the scanner language already. So Singleton was a very popular pattern caught on from the Gang of Four. And really that's just an object and that comes with all the benefits and all of the costs. For example static cling that Singleton itself displayed. But if you need a singleton you just create an object and that becomes the class definition and the only instance of that class all in one convenient package abstract factory. Actually they apply method is probably the most common form of this factory pattern in Scala. If you define an object with an apply method you simply stamp out whatever you want to from that by calling the object and using the apply method on each You called object and then use parens. Several options are also possible. Oh sorry that's for build. So the next one is the builder pattern and this has several different options available including infix methods that you can use to create a flow an API case classes where you use named and default parameters methods using copy uncased classes. All of these are you know pretty much a direct substitute for the builder and they have one more thing which is that they are just as part of being built into Skanda usually immutable themselves. For example if you use infix methods or case classes with name default parameters everything is just immutable. At the end of it there's no more work to do. Whereas in the original build a pattern usually they were mutable up until the point where you did a build and then it would reject an immutable result. But we don't even have to do that anymore with things like copy on case classes and the kind of flow and methods that are possible by dropping the dot and the parens. We can get a very nice result from those prototype. This is just the way you use one object to create to copy objects and we have a copy method uncased classes for that so that's really easy adapter. This is really just trait ins or possibly using type classes if you want the adapter to be something that you can add on in an ad hoc way. Bridge is just an application of traits. Composite is case closed as if some of these you need to remind yourself go back and have a look at the gang for book and you'll be up to see the pattern and then see how this applies. But yeah composit is just case classes and side of case classes. Decorator is type classes this probably the natural way to do that that's Ad-Hoc polymorphism you just say hey I have one of these and I need it to be traced on serializable. Here's how that works. Facade traits. And what I call just in time traits and just in traits are literally way you create a trait with the abstract API that you want. And if that happens to coincide with the class you can simply new up an instance of a class and say with that trait and everything happy the trait is satisfied by the methods with the same type signature in the class and you can use it is that right now so if facades it very easily and automatically for you. Flyweights So this would really just be a map or what I tend to use as we've already seen in the stateful in part one of this advanced course is guava caches and natural way to do flyweight and it actually brings a lot more functionality in like configurable eviction and so on proxy that is various features like lazy Val is a good example of where a proxy is sort of just implemented at the language level chain of responsibility you can use partial functions with an is defined and then you can basically say to each partial function hey can you handle this. And if not then it will just pass along to the next one. That's a kind of a natural way to implement that command would be just function literals But you know what the reason command existed was for languages that didn't have first class functions. And we have those now. Interpreter as we can to say that really is a details which is just coming up in a minute but Case class based Abts with pattern matching. And if you want to go further you can literally go all the way through to free monads which we will be covering later in the course and really free monads are an interpreter amongst other things they are full of algebraic data type and an interpreter for that algebraic type. Iterator is just the iterator class or iterative method on collections. Mediator is just in time traits for one solution or that's one solution one way of doing it. Momento is another possible use of type classes. Observer you can just do pattern matching for that. You really don't need observe. If you have a language with pattern matching state is discouraged in functional programming unless you use a state monad. And we're going to look at that later and the things is really in this case what it means is encapsulated mutable state and we tend not to do that the other place that you'll find this scammer is embedded in actors so actors really are a way of kind of safely while at least tightly encapsulating your mutable state and allowing only concurrent access to it or synchronized access to it strategy is really traits or ID Tay's template is tripes with some abstract members and visitor ceiled traits pattern matching 80 Tay's all of those kind of things apply. So really like I said all of the gang of four really just have solutions in Skala. For the most part already and they're very lightweight and easy to do.