项目作者: morvanabonin

项目描述 :
Programming Foundations: Design Patterns
高级语言: Java
项目地址: git://github.com/morvanabonin/design_patterns.git
创建时间: 2020-09-07T02:16:01Z
项目社区:https://github.com/morvanabonin/design_patterns

开源协议:

下载


Programming Foundations: Design Patterns

Fundamentals properties of Object-Oriented Design

  • Inheritance
  • Polymorphism
  • Abstraction
  • Encapsulation

Design Patterns provide paths to solutions for some of the most common object-oriented design conundrums.

Benefits

  • Not reinvent the wheel
  • Building resilient code
  • Preparing for future additions
Design Principles Design Patterns
General guidelines Specific Solutions

Encapsulate your varies
Identify the aspects of your application that vary and separate them from what stays the same.

  • if some aspect of your code is changing , that’s a sign you should pull it out and separate it
  • By separating out the parts of your code that change, you can extend or alter them without affecting the rest of your code
  • This principle is fundamental to almost every design pattern
Principle Pattern
Encapsulate what varies The Strategy Pattern
The Iterator Pattern
The Factory Pattern
Loose Coopling The Observe Pattern

Loose Coupling
Strive for loosely coupled designs between objects that interact.

Objects are loosely coupled when they interact with one another, which makes them coupled, but they don’t know a lot about each other, which makes them loosely coupled.

The Strategy Pattern

Program to an interface, NOT an Implementation
Clients remain unaware of the specific types of objects they use, as long as the objects adhere to the interface that clients expect.

alt text

The Strategy Pattern
This Pattern defines a family of algorithms, encapsulate each one, and makes them interchangeable.
This lets the algorithm vary independently clients that use it.

HAS-A Is Better Than IS-A

  • We’re using a HAS-A relationship

    • Each duck has a FlyBehavior and a QuackBehavior
  • Instead of inheriting behavior, we’re composing it

  • A duck is compose with a fly behavior and a quack behavior

Favor Composition over Inheritance
Classes should achieve code reuse using composition rather than inheritance from a superclass.

The Adapter Pattern

The Adapter class handles the work of translating the request to the new Vendor class.

The Adapter Pattern
This Pattern converts the interface of a class into another interface that clients expect.
It allows classes to work together that couldn’t otherwise because of incompatible interfaces.

alt text

Adapters Use Composition

  • The client is composed with the class with the target interface
  • The adapter is composed with the adaptee
  • The adapter delegates calls to the adaptee, and returns any needed value

The Observer Pattern

The Observer Pattern
This Pattern defines a one-to-many dependency between objects so that when one object changes state, all of its
dependents are notified and updated automatically.

alt text

Loose Coupling
Subjects and observers are loosely coupled
They interact, but have little knowledge of each other
Subject knows only that the observer implements a specific interface
Subject doesn’t know need to know the concrete class of the obe

There are threads about the Observer Pattern
Links and a paper about it:

And Java in its 9 version, deprecated java.util.Observer.