Posts

Showing posts from April, 2022

Lambda expression

  Lambda expression Why lambda? Avoid defining too many anonymous inner class Simplify the code, keep the core part of code Premise of lambda Requires a functional interface //if you want to use lambda like this way, only one line code is acceptable like = () -> System . out . println ( "I like lambda expression - lambda expression2" ); The following code illustrates the simplified process from outer class to lambda package com . lilrich . lambda ; ​ import java . sql . SQLOutput ; ​ public class TestLambda {     //2. static inner class     static class Like2 implements ILike {         @Override         public void lambda () {             System . out . println ( "I like lambda expression - static inner class" );       }   }     public static void main ( String [] args ) {         ILike like = new Like1 (); ...

Multithreading - Race(turtle&rabbit)

  Multithreading - Race(turtle&rabbit) package Thread ; ​ public class TestRace implements Runnable {     private static String winner ;     @Override     public void run () {         for ( int i = 0 ; i <= 100 ; i ++ ) {             boolean flag = gameOver ( i );             if ( flag ){                 System . out . println ( flag );                 System . exit ( 0 );           }             if ( Thread . currentThread (). getName (). equals ( "rabbit" ) && i % 10 == 0 ){                 try {                     Thread . sleep ( 100 );               } catch ( I...

Static Proxy - Marry example

  Static proxy - marry example How to implement static proxy? The proxy object and real object need to implement same interface Proxy object need to proxy real role (e.g., Wedding company help you to prepare wedding) Benefit of static proxy: Proxy object can do many operations that real object cannot Real object can focus on its own task Example of wedding company package staticProxy ; ​ public class TestStaticProxy {     public static void main ( String [] args ) {         Customer customer = new Customer ();         WeddingCompany weddingCompany = new WeddingCompany ( customer );         weddingCompany . HappyMarry ();   } } ​ //interface interface Marry {     void HappyMarry (); } //Customer: you class Customer implements Marry {     @Override     public void HappyMarry () {         System . out . println ( "You're getting m...

Object-oriented programming (OOP)

Image
  Object-oriented programming(OOP) What is OOP? Process-oriented Procedural programming is about writing procedures or methods that perform operations on the data Ref: https://www.w3schools.com/java/java_oop.asp Object-oriented Object-oriented programming is about creating objects that contain both data and methods. Advantage of OOP: Faster and easier to execute Provides a clear structure Easier to maintain, modify and debug Object-Oriented Creation and Analysis      3. Three characteristics of object-oriented Encapsulation attributes Encapsulate data into a box and access the data through an interface Encapsulation mainly focuses on attribute, e.g., name, age, height High cohesion, low coupling High cohesion - internal data operation details of the class are done by class itself, and exteanl interference is not allowed Low coupling - Expose only a few methods for external use Here is an example, a large amount of user data in a bank is managed by bank (high cohesi...