yuhaolu1994 / java-design-pattern-practice

Java Design Patterns

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Java Design Pattern

SOLID

Open/closed Principle

Open for extention, close for implementation

Dependency Inversion Principle

Interface oriented coding

Single Responsibility Principle

Class / Interface / Method

Interface Segregation Principle

Demeter Principle

Member variable, input parameters and return value

Design Patterns

Simple Factory

Use java reflection

Factory Method

Example Logic


Factory Used In JDK

Example 1

  1. Iterator interface -> Video abstract class (abstract product)
  2. iterator() in Collection interface -> VideoFactory abstract class
  3. iterator() in ArrayList class (implements Collection) -> JavaVideoFactory class
  4. Itr class in ArrayList implements Iterator -> JavaVideo class (actual product)

Example 2

  1. URLStreamHandlerFactory interface -> VideoFactory
  2. Factory in Launcher.class -> JavaVideoFactory
  3. URLStreamHandler abstract class -> Video
  4. use reflection Class.forName(actual class path) -> JavaVideo

Abstract Factory Method

Product group, not same product grade

About

Java Design Patterns


Languages

Language:Java 100.0%