Code On JVM (My First JAVA Meetup)

java dev.to

Today I attended My first Java Meetup and there I learned a lot of things from the professionals who take the session, they provide more info about the code and how the code actually works in backend and how we write a good code to avoid complex problems.

Community

They are Strong community with lot of professionals, students, and java enthusiasts, they are very motivated and well organised community who make Monthly Meetups to discuss about the technology and how the java works in current tech-world.

Session

The Session was taken by Mahati ChandraMohan, she is a senior software engineer at PAYPAL and she have 14Years of Experience.
She take the Session very clearly as even a student can also understand what she will convey to us, like that much clarity she gave to each and everyone who attend the Meetup.

What she actually taught

  • She tells one thing clearly that a good developer developes a good code.

  • good code => means not the code works well today, is the code which works for long time without any complexcity or without create any problem, which is adaptable to future integration of new features.

Design Patterns

  • She taught something like Design Pattern is nothing but the design which we used to write our code is known as Design Patterns.

  • She tell that Around 23 Design Patters are there but she taught 3 Design Patterns in a simple manner and which is more understandable to all.
    1. Creational Pattern
    2. Structural pattern
    3. Behavioral pattern
    These are all the Patterns she taught today.

  • design pattern are the design, the way of writing efficient and good code which allow us make changes and add features without any problem, which means the clear code allow us to do anything around it but still its working well.

  • If we use these Design Patterns to write a code then our code also a good one and we also a good developer who able to create a scalable application, and which adaptable to add new features and works well for long years.

Example:

Bad Design (Without Design Pattern)

class PaymentProcessor {
  processPayment(method, amount) {

    if (method === "UPI") {
      console.log(`Paid ₹${amount} using UPI`);
    }

    else if (method === "CreditCard") {
      console.log(`Paid ₹${amount} using Credit Card`);
    }

    else if (method === "PayPal") {
      console.log(`Paid ₹${amount} using PayPal`);
    }

    else {
      console.log("Invalid Payment Method");
    }
  }
}

const payment = new PaymentProcessor();

payment.processPayment("UPI", 500);
payment.processPayment("CreditCard", 1000);
Enter fullscreen mode Exit fullscreen mode

Here, every time a new payment method is added, we must modify the existing code.
Problems
Imagine tomorrow your client says:

  • Add Google Pay

  • Add PhonePe

  • Add Apple Pay
    You have to keep editing:

if(method==="GooglePay") { ... }
else if(method==="PhonePe") { ... }
else if(method==="ApplePay") { ... }
Enter fullscreen mode Exit fullscreen mode

Good Design (Strategy Design Pattern)
Each payment method becomes a separate strategy.
Step 1: Create Payment Strategies

class UPIPayment {
  pay(amount) {
    console.log(`Paid ₹${amount} using UPI`);
  }
}

class CreditCardPayment {
  pay(amount) {
    console.log(`Paid ₹${amount} using Credit Card`);
  }
}

class PayPalPayment {
  pay(amount) {
    console.log(`Paid ₹${amount} using PayPal`);
  }
}
Enter fullscreen mode Exit fullscreen mode

Step 2: Payment Processor

class PaymentProcessor {

  constructor(paymentMethod) {
    this.paymentMethod = paymentMethod;
  }

  processPayment(amount) {
    this.paymentMethod.pay(amount);
  }

}
Enter fullscreen mode Exit fullscreen mode

Step 3: Use It

const upi = new UPIPayment();
const card = new CreditCardPayment();
const paypal = new PayPalPayment();

const payment1 = new PaymentProcessor(upi);
payment1.processPayment(500);

const payment2 = new PaymentProcessor(card);
payment2.processPayment(1000);

const payment3 = new PaymentProcessor(paypal);
payment3.processPayment(1500);
Enter fullscreen mode Exit fullscreen mode

Output

Paid ₹500 using UPI
Paid ₹1000 using Credit Card
Paid ₹1500 using PayPal
Enter fullscreen mode Exit fullscreen mode

Why is this better?
Suppose you need to add PhonePe.
Just create a new class:

class PhonePePayment {
  pay(amount) {
    console.log(`Paid ₹${amount} using PhonePe`);
  }
}
Enter fullscreen mode Exit fullscreen mode

Use it:

const phonepe = new PhonePePayment();

const payment = new PaymentProcessor(phonepe);

payment.processPayment(2000);
Enter fullscreen mode Exit fullscreen mode

Note:

  • No changes to PaymentProcessor

  • No huge if-else chain

  • Easy to extend

  • Easy to test

Design Pattern Used

  • This is the Strategy Pattern.

  • She tells like this the good developer developes the code which is more efficient and scalable and also adaptable to future integrations.

Quiz

They also conduct Quiz About the JAVA, for students and professional they participated. If they score well they also rewarded by gifts and they also asked some funny questions in that Quiz.

Grateful to Mahati ChandraMohan for an insightful and well-structured session that made complex concepts easy to understand. Special thanks to our trainers for continuously encouraging us to participate in meetups and expand our knowledge.

Source: dev.to

arrow_back Back to Tutorials