Friday, March 24, 2023
Learning Code
  • Home
  • JavaScript
  • Java
  • Python
  • Swift
  • C++
  • C#
No Result
View All Result
  • Home
  • JavaScript
  • Java
  • Python
  • Swift
  • C++
  • C#
No Result
View All Result
Learning Code
No Result
View All Result
Home Java

Venkat Subramaniam Brings a Contemporary Twist to GoF Design Patterns with Modern Java at Devoxx BE

learningcode_x1mckf by learningcode_x1mckf
October 23, 2022
in Java
0
Venkat Subramaniam Brings a Contemporary Twist to GoF Design Patterns With Modern Java at Devoxx BE
74
SHARES
1.2k
VIEWS
Share on FacebookShare on Twitter


You might also like

Java Developer Survey Reveals Increased Need for Java … – PR Newswire

What You Should Definitely Pay Attention to When Hiring Java Developers – Modern Diplomacy

Java Web Frameworks Software Market Research Report 2023 … – Los Alamos Monitor

The GoF Design Patterns, printed again in 1998, qualifies as a traditional of laptop science as it’s nonetheless being taught each in universities but additionally beneficial as finest observe within the business. In his deep dive session from Devoxx, Venkat Subramaniam gave them a up to date twist, by implementing Iterator, Strategy, Decorator or Factory Method with fashionable Java.

Within the introduction of his talk, Subramaniam considers the guide authors to be grandfathers of software program improvement and their design patterns with grandma’s recipes: even when you’ve got them, you will be unable to breed the dish. So, he considers that design patterns are phenomenal as a communication software, however a catastrophe as a software program design software.

The next are typical patterns we may meet in our day-to-day coding, which he made a bit extra fluent in his energetic and joyful method.

The iterator sample modified quite a bit because of Java’s embrace of practical programming. One of many largest modifications was the shift from an exterior iterator to an inner iterator, which got here with Java’s practical API. With this transformation, you possibly can evolve from utilizing the verbose crucial type iteration


int depend = 0;
for(var identify: names) 
   if(identify.size() == 4) 
     System.out.println(identify.toUpperCase());
	 depend++;

     if(depend == 2) 
        break;
     
   
  
}

to the fluent practical iteration:


names.stream()
     .filter(identify -> identify.size() == 4)
     .map(String::toUpperCase)
     .restrict(2)
     .forEach(System.out::println);

 

The limit(long) and takeWhile(Predicate<? super T>) (added in Java 9) are the practical equivalents of continue and break statements, the primary one is taking only a numerical restrict, whereas the second makes use of an expression.

Even when Java’s practical API has been a part of the JDK for nearly a decade, there are nonetheless widespread errors that linger within the code bases. The one that may make the outcomes of iteration operations unpredictable (particularly in parallel executions) is when the practical pipeline is *not* pure (it modifications or relies on any state seen from the surface).

Light-weight technique – the place we need to range a small a part of an algorithm whereas maintaining the remainder of the algorithm the identical. Traditionally, the sample was carried out by having a way that takes a single methodology interface as a parameter that has a number of implementations for every of the methods to be carried out, as methods are sometimes a single methodology or perform. So, practical interfaces and lambdas work very well. 

Though nameless lessons represented an implementation mechanism, practical interfaces (Predicate<? super T> is an efficient candidate) or lambdas make the code much more fluent and simpler to grasp. In fashionable Java, Technique is extra of a characteristic than a sample that requires important effort to implement.


public class Pattern 
  public static int totalValues(Record<Integer> numbers) 
    int whole = 0;

    for(var quantity: numbers) 
      whole += quantity;
    

    return  whole;
  

  public static int totalEvenValues(Record<Integer> numbers) 
    int whole = 0;

    for(var quantity: numbers) 
      if(quantity % 2 == 0)  whole += quantity; 
    

    return  whole;
  

  public static int totalOddValues(Record<Integer> numbers) 
    int whole = 0;

    for(var quantity: numbers) 
      if(quantity % 2 != 0)  whole += quantity; 
    

    return  whole;
  


  public static void primary(String[] args) 
    var numbers = Record.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);

    System.out.println(totalValues(numbers));
    System.out.println(totalEvenValues(numbers));
    System.out.println(totalOddValues(numbers));
  


The extra fashionable take can be to make use of a lambda for the technique: 


import java.util.perform.Predicate;

public class Pattern 
  public static int totalValues(Record<Integer> numbers,
    Predicate<Integer> selector) 
    int whole = 0;

    for(var quantity: numbers) 
      if(selector.take a look at(quantity)) 
        whole += quantity;
      
    

    return  whole;
  

  public static boolean isOdd(int quantity) 
    return quantity % 2 != 0;
  

  public static void primary(String[] args) 
    var numbers = Record.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);

    System.out.println(totalValues(numbers, ignore -> true));
    System.out.println(totalValues(numbers, 
      quantity -> quantity % 2 == 0));

    System.out.println(totalValues(numbers, Pattern::isOdd));
  


 

Manufacturing facility methodology utilizing default strategies

Within the introduction to the manufacturing unit methodology implementation, Venkat said the next:

What’s the worst key phrase in Java from the polymorphism perspective? […] Though closing, instanceof and static are good candidates for this, they’re mininions. new is the mafia of all of them.

A number of patterns (creational patterns), frameworks (Spring, Guice) or methods had been conceived so as to deal with the “evilness” of recent, its lack of polymorphism help and its tight coupling. Impressed by Ruby’s polymorphic potential to create totally different objects primarily based on context, Venkat proposes an implementation of the manufacturing unit methodology sample by utilizing Java’s default keyword. This strategy would permit one to utilize interfaces and really small implementing lessons, making the code simpler to observe.


import java.util.*;

interface Pet 
class Canine implements Pet 
class Cat implements Pet 

interface Particular person 
  Pet getPet();

  default void play() 
    System.out.println("enjoying with " + getPet());
  


class DogPerson implements Particular person 
  non-public Canine canine = new Canine();

  public Pet getPet()  return canine; 


class CatLover implements Particular person 
  non-public Cat cat = new Cat();
  public Pet getPet()  return cat; 


public class Pattern 
  public static void name(Particular person particular person) 
    particular person.play();
  

  public static void primary(String[] args) 
    name(new DogPerson());
    name(new CatLover());
  

 

Decorator

Even when the decorator sample is theoretically well-known by many programmers, few truly carried out it in observe. In all probability essentially the most notorious instance of its implementation is the development of io packages. Venkat proposes a special strategy to this sample, primarily based on the features composability: by utilizing the identity function and andThen(Function<? super R,? extends V> ), he has the flexibility to construct easy, fluent mechanisms that improve the talents of an object.

 


class Digicam 
  non-public Perform<Coloration, Coloration> filter;

  public Digicam(Perform<Coloration, Coloration>... filters) 
    filter = Stream.of(filters)
      .cut back(Perform.identification(), Perform::andThen);
  

  public Coloration snap(Coloration enter) 
    return filter.apply(enter);
  


public class Pattern 
  public static void print(Digicam digicam) 
    System.out.println(digicam.snap(new Coloration(125, 125, 125)));
  

  public static void primary(String[] args) 
    print(new Digicam());

    print(new Digicam(Coloration::brighter));
    print(new Digicam(Coloration::darker));

    print(new Digicam(Coloration::brighter, Coloration::darker));
  


 

Even when the patterns appear to be ceaselessly younger, as Subramaniam mentioned during his talk: “Design patterns typically kick in to fill the gaps of a programming language. The extra highly effective a language is, the much less we speak about design patterns as these naturally change into the options of the language.”

With the evolution of programming languages and our expertise, the patterns additionally evolve as time progresses. A few of them are absorbed as options of the languages, others had been deemed out of date and others are simpler to implement. No matter which class your favorite falls into, Venkat suggests utilizing them as communication means and letting the code evolve in direction of them. Additionally, he recommends experimenting with a number of programming languages, as a method to make your coding extra fluent.





Source link

Share30Tweet19
learningcode_x1mckf

learningcode_x1mckf

Recommended For You

Java Developer Survey Reveals Increased Need for Java … – PR Newswire

by learningcode_x1mckf
March 24, 2023
0
Google expands open source bounties, will soon support Javascript fuzzing too – ZDNet

Java Developer Survey Reveals Increased Need for Java ...  PR Newswire Source link

Read more

What You Should Definitely Pay Attention to When Hiring Java Developers – Modern Diplomacy

by learningcode_x1mckf
March 24, 2023
0
Google expands open source bounties, will soon support Javascript fuzzing too – ZDNet

What You Should Definitely Pay Attention to When Hiring Java Developers  Trendy Diplomacy Source link

Read more

Java Web Frameworks Software Market Research Report 2023 … – Los Alamos Monitor

by learningcode_x1mckf
March 23, 2023
0
Google expands open source bounties, will soon support Javascript fuzzing too – ZDNet

Java Web Frameworks Software Market Research Report 2023 ...  Los Alamos Monitor Source link

Read more

Minecraft Java Edition: 10 Best World Editors – TheGamer

by learningcode_x1mckf
March 23, 2023
0
Google expands open source bounties, will soon support Javascript fuzzing too – ZDNet

Minecraft Java Edition: 10 Best World Editors  TheGamer Source link

Read more

Oracle Releases Java 20 – PR Newswire

by learningcode_x1mckf
March 23, 2023
0
Google expands open source bounties, will soon support Javascript fuzzing too – ZDNet

Oracle Releases Java 20  PR Newswire Source link

Read more
Next Post
Police Chief Reveals East Java Police Chief Teddy Minahasa’s Alleged Drug Case

Police Chief Reveals East Java Police Chief Teddy Minahasa's Alleged Drug Case

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Related News

Python Basics: Dictionaries – Real Python

Python Basics: Dictionaries – Real Python

December 6, 2022
Swift actors tutorial – a beginner’s guide to thread safe concurrency

Swift actors tutorial – a beginner’s guide to thread safe concurrency

September 14, 2022
Matrix Inverses and Least Squares – Real Python

Matrix Inverses and Least Squares – Real Python

January 23, 2023

Browse by Category

  • C#
  • C++
  • Java
  • JavaScript
  • Python
  • Swift

RECENT POSTS

  • Java Developer Survey Reveals Increased Need for Java … – PR Newswire
  • What You Should Definitely Pay Attention to When Hiring Java Developers – Modern Diplomacy
  • Java Web Frameworks Software Market Research Report 2023 … – Los Alamos Monitor

CATEGORIES

  • C#
  • C++
  • Java
  • JavaScript
  • Python
  • Swift

© 2022 Copyright Learning Code

No Result
View All Result
  • Home
  • JavaScript
  • Java
  • Python
  • Swift
  • C++
  • C#

© 2022 Copyright Learning Code

Are you sure want to unlock this post?
Unlock left : 0
Are you sure want to cancel subscription?