Design Patterns and Best Practices in Java

Book description

Create various design patterns to master the art of solving problems using Java

About This Book
  • This book demonstrates the shift from OOP to functional programming and covers reactive and functional patterns in a clear and step-by-step manner
  • All the design patterns come with a practical use case as part of the explanation, which will improve your productivity
  • Tackle all kinds of performance-related issues and streamline your development
Who This Book Is For

This book is for those who are familiar with Java development and want to be in the driver's seat when it comes to modern development techniques. Basic OOP Java programming experience and elementary familiarity with Java is expected.

What You Will Learn
  • Understand the OOP and FP paradigms
  • Explore the traditional Java design patterns
  • Get to know the new functional features of Java
  • See how design patterns are changed and affected by the new features
  • Discover what reactive programming is and why is it the natural augmentation of FP
  • Work with reactive design patterns and find the best ways to solve common problems using them
  • See the latest trends in architecture and the shift from MVC to serverless applications
  • Use best practices when working with the new features
In Detail

Having a knowledge of design patterns enables you, as a developer, to improve your code base, promote code reuse, and make the architecture more robust. As languages evolve, new features take time to fully understand before they are adopted en masse. The mission of this book is to ease the adoption of the latest trends and provide good practices for programmers.

We focus on showing you the practical aspects of smarter coding in Java. We'll start off by going over object-oriented (OOP) and functional programming (FP) paradigms, moving on to describe the most frequently used design patterns in their classical format and explain how Java's functional programming features are changing them.

You will learn to enhance implementations by mixing OOP and FP, and finally get to know about the reactive programming model, where FP and OOP are used in conjunction with a view to writing better code. Gradually, the book will show you the latest trends in architecture, moving from MVC to microservices and serverless architecture. We will finish off by highlighting the new Java features and best practices. By the end of the book, you will be able to efficiently address common problems faced while developing applications and be comfortable working on scalable and maintainable projects of any size.

Style and approach

This book explains design patterns in a step-by-step manner with clear and concise code explanations.

Table of contents

  1. Title Page
  2. Copyright and Credits
    1. Design Patterns and Best Practices in Java
  3. Packt Upsell
    1. Why subscribe?
    2. PacktPub.com
  4. Contributors
    1. About the authors
    2. About the reviewer
    3. Packt is searching for authors like you
  5. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
      1. Download the example code files
      2. Download the color images
      3. Conventions used
    4. Get in touch
      1. Reviews
  6. From Object-Oriented to Functional Programming
    1. Java – an introduction
    2. Java programming paradigms
    3. Imperative programming
      1. Real-life imperative example
    4. Object-oriented paradigm
      1. Objects and classes
      2. Encapsulation
      3. Abstraction
      4. Inheritance
      5. Polymorphism
    5. Declarative programming
    6. Functional programming
      1. Working with collections versus working with streams
    7. An introduction to Unified Modeling Language
      1. Class relations
        1. Generalization
        2. Realization
        3. Dependency
        4. Association
          1. Aggregation
          2. Composition
    8. Design patterns and principles
      1. Single responsibility principle
      2. Open/closed principle
      3. Liskov Substitution Principle
      4. Interface Segregation Principle
      5. Dependency inversion principle
    9. Summary
  7. Creational Patterns
    1. Singleton pattern
      1. Synchronized singletons
      2. Synchronized singleton with double-checked locking mechanism
      3. Lock-free thread-safe singleton
      4. Early and lazy loading
    2. The factory pattern
      1. Simple factory pattern
        1. Static factory
        2. Simple factory with class registration using reflection
        3. Simple factory with class registration using Product.newInstance
      2. Factory method pattern
        1. Anonymous concrete factory
      3. Abstract factory
      4. Simple factory versus factory method versus abstract factory
    3. Builder pattern
      1. Car builder example
      2. Simplified builder pattern
      3. Anonymous builders with method chaining
    4. Prototype pattern
      1. Shallow clone versus deep clone
    5. Object pool pattern
    6. Summary
  8. Behavioral Patterns
    1. The chain-of-responsibility pattern
      1. Intent
      2. Implementation
      3. Applicability and examples
    2. The command pattern
      1. Intent
      2. Implementation
      3. Applicability and examples
    3. The interpreter pattern
      1. Intent
      2. Implementation
      3. Applicability and examples
    4. The iterator pattern
      1. Intent
      2. Implementation
      3. Applicability and examples
    5. The observer pattern
      1. Intent
      2. Implementation
    6. The mediator pattern
      1. Intent
      2. Implementation
      3. Applicability and examples
    7. The memento pattern
      1. Intent
      2. Implementation
      3. Applicability
    8. The state pattern
    9. The strategy pattern
      1. Intent
      2. Implementation
    10. The template method pattern
      1. Intent
      2. Implementation
    11. The null object pattern
      1. Implementation
    12. The visitor pattern
      1. Intent
      2. Implementation
    13. Summary
  9. Structural Patterns
    1. Adapter pattern
      1. Intent
      2. Implementation
      3. Examples
    2. Proxy pattern
      1. Intent
      2. Implementation
      3. Examples
    3. Decorator pattern
      1. Intent
      2. Implementation
      3. Examples
    4. Bridge pattern
      1. Intent
      2. Implementation
      3. Examples
    5. Composite pattern
      1. Intent
      2. Implementation
      3. Examples
    6. Façade pattern
      1. Intent
      2. Implementation
      3. Examples
    7. Flyweight pattern
      1. Intent
      2. Implementation
      3. Examples
    8. Summary
  10. Functional Patterns
    1. Introducing functional programming
      1. Lambda expressions
      2. Pure functions
      3. Referential transparency
      4. First-class functions
      5. Higher-order functions
      6. Composition
      7. Currying
      8. Closure
      9. Immutability
      10. Functors
      11. Applicatives
      12. Monads
    2. Introducing functional programming in Java
      1. Lambda expressions
      2. Streams
      3. Stream creator operations
      4. Stream intermediate operations
      5. Stream terminal operations
    3. Re-implementing OOP design patterns
      1. Singleton
      2. Builder
      3. Adapter
      4. Decorator
      5. Chain of responsibility
      6. Command
      7. Interpreter
      8. Iterator
      9. Observer
      10. Strategy
      11. Template method
    4. Functional design patterns
      1. MapReduce
        1. Intent
        2. Examples
      2. Loan pattern
        1. Intent
        2. Examples
      3. Tail call optimization
        1. Intent
        2. Examples
      4. Memoization
        1. Intent
        2. Examples
      5. The execute around method
        1. Intent
        2. Examples
    5. Summary
  11. Let's Get Reactive
    1. What is reactive programming?
    2. Introduction to RxJava
    3. Installing the RxJava framework
      1. Maven installation
      2. JShell installation
    4. Observables, Flowables, Observers, and Subscriptions
    5. Creating Observables
      1. The create operator
      2. The defer operator
      3. The empty operator
      4. The from operator
      5. The interval operator
      6. The timer operator
      7. The range operator
      8. The repeat operator
    6. Transforming Observables
      1. The subscribe operator
      2. The buffer operator
      3. The flatMap operator
      4. The groupBy operator
      5. The map operator
      6. The scan operator
      7. The window operator
    7. Filtering Observables
      1. The debounce operator
      2. The distinct operator
      3. The elementAt operator
      4. The filter operator
      5. The first/last operator
      6. The sample operator
      7. The skip operator
      8. The take operator
    8. Combining Observables
      1. The combine operator
      2. The join operator
      3. The merge operator
      4. The zip operator
    9. Error handling
      1. The catch operator
      2. The do operator
      3. The using operator
      4. The retry operator
    10. Schedulers
    11. Subjects
    12. Example project
    13. Summary
  12. Reactive Design Patterns
    1. Patterns for responsiveness
      1. Request-response pattern
      2. Asynchronous-communication pattern
      3. Caching pattern
      4. Fan-out and quickest-reply pattern
      5. Fail-fast pattern
    2. Patterns for resilience
      1. The circuit-breaker pattern
      2. Failure-handling pattern
      3. Bounded-queue pattern
      4. Monitoring patterns
      5. Bulkhead pattern
    3. Patterns for elasticity
      1. Single responsibility pattern
      2. Stateless-services pattern
      3. Autoscaling pattern
      4. Self-containment pattern
    4. Patterns for message-driven implementation
      1. Event-driven communication pattern
      2. Publisher-subscriber pattern
      3. Idempotency pattern
    5. Summary
  13. Trends in Application Architecture
    1. What is application architecture?
    2. Layered architecture
      1. Layered architecture with an example
      2. Tiers versus layers
      3. What does layered architecture guarantee?
      4. What are the challenges with layered architecture?
    3. Model View Controller architecture
      1. MVC architecture with an example
      2. A more contemporary MVC implementation
      3. What does MVC architecture guarantee?
      4. What are the challenges with MVC architecture?
    4. Service-oriented architecture
      1. Service-oriented architecture with an example
      2. Web services
        1. SOAP versus REST
      3. Enterprise service bus
      4. What does service-oriented architecture guarantee?
      5. What are the challenges with service-oriented architecture?
    5. Microservices-based Architecture
      1. Microservice architecture with an example
      2. Communicating among services
      3. What does microservices-based architecture guarantee?
      4. What are challenges with microservices-based architecture?
    6. Serverless architecture
      1. Serverless architecture with an example
      2. Independence from infrastructure planning
      3. What does serverless architecture guarantee?
      4. What are the challenges with serverless architecture?
    7. Summary
  14. Best Practices in Java
    1. A brief history of Java
      1. Features of Java 5
      2. Features of Java 8
      3. Currently supported versions of Java
    2. Best practices and new features of Java 9
      1. Java platform module system
      2. JShell
      3. Private methods in interfaces
      4. Enhancements in streams
      5. Creating immutable collections
      6. Method addition in arrays
      7. Additions to the Optional class
      8. New HTTP client
      9. Some more additions to Java 9
    3. Best practices and new features of Java 10
      1. Local variable type inference
      2. copyOf method for collections
      3. Parallelization of full garbage collection
      4. Some more additions to Java 10
    4. What should be expected in Java 11?
    5. Summary
  15. Other Books You May Enjoy
    1. Leave a review - let other readers know what you think

Product information

  • Title: Design Patterns and Best Practices in Java
  • Author(s): Kamalmeet Singh, Adrian Ianculescu, Lucian-Paul Torje
  • Release date: June 2018
  • Publisher(s): Packt Publishing
  • ISBN: 9781786463593