Code Monkey home page Code Monkey logo

designpatterns's Introduction

This repository is used to have a quick overview on those design patterns introduced from the book Head First Design Patterns.

A Pattern is a solution to a problem in a context:

  • The context is the situaion in which the pattern applies. This should be a recurring situation.
  • The problem refers to the goal you are trying to achieve in this context, but it also referes to any constraints that occur in the context.
  • Ths solution is what you're after: a general design that anyone can apply which resolves the goal and set of constraints.

Note that the diagram is created via Draw.io.

  • To export reusable SVG format, config the export:
    • ZOOM: 100%
    • Border Width: 0
    • Include a copy of my diagram: check
  • To export the PNG diagram, config the export:
    • ZOOM: 100%
    • Border Width: 20

Design Principle

  • Favor composition over inheritance.

  • Strive for loosely coupled designs between objects that interact.

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

  • Program to an interface, not an implementation.

  • Classes should be open for extension, but closed for modification.

  • Depend upon abstractions. Do not depend upon concrete classes.

  • Principle of Least Knowledge - talk only to your immediate friends.

  • A class should have only one reason to change.

Patterns

Strategy

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

Example Diagram:

Strategy - overview

How Strategy is actually applied:

Strategy - detail

Observer

The Observer 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.

For simplicity, the connection between Subject & StatisticsDiplay in Push mode, and connection between Observable & StatisticsDiplay in Pull mode is not drawn.

Push Mode

In Push mode, it's WeatherData to automatically push notifications to all observers. Whenever there is a change, WeatherData would call each observer's update() method with its attributes to notify.

Example Diagram:

Observer - push

Pull Mode

Like the Push mode, WeatherData would notify all observers when there is a change under Pull mode. However, unlike data is forcely pushed to observers under Push mode, it's the observer's choice to decide whether to "pull" data from the Observable since the update() method in Pull mode pass the Observable in. That's the key difference between Pull and Push.

Note that the code implemented in Pull mode uses existing Observer and Observable from java.util library.

Example Diagram:

Observer - pull

Decorator

The Decorator Pattern attaches additional responsibilities to an object dynamically. Decorator provides a flexible alternative to subclassing for extending functionality.

Example Diagram:

Decorator

Factory

The Factory Method Pattern defines an interface for creating an object, but lets subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses.

The Abstract Factory Pattern provides an interface for creating families of related or dependent objects without specifying their concrete classes.

Factory Method is not the only technique for adhering to the Dependency Inversion Principle, but it is one of the more powerful ones. The following guidelines can help you avoid OO designs that violate the Dependency Inversion Principle:

  • No variable should hold a reference to a concrete class
  • No class should derive from a concrete class
  • No method should override an implemented method of any of its base classes

Simple Factory

Defining a simple factory as a static method is a common technique and is often called a static factory. Why use a static method? Because you don’t need to instantiate an object to make use of the create method. But remember it also has the disadvanage that you can’t subclass and change the behavior of the create method.

Set Pizza to be abstract is because a pizza must be concrete, such as CheesePizza, VeggiePizza.

Example Diagram:

Simple Factory

Factory Method

The Factory Method Pattern encapsulates object creation by letting subclasses decide what objects to create.

For Simple Factory, there is one factory to createPizza(). However, it's concrete PizzaStore's job to createPizza() when it comes to Factor Method.

Example Diagram:

Factory Method - overview

How Factory Method is actually applied:

Factory Method - detail

The higher level diagram of the Factory Method.

Factory Method

Abstract Factory

Example Diagram:

Abstract Factory - overview

The higher level diagram of the Abstract Factory.

Abstract Factory

Singleton

The Singleton Pattern ensures a class has only one instance, and provides a global point of access to it.

Singleton

Command

The Command Pattern encapsulates a request as an object, thereby letting you parameterize other objects with different requests, queue or log requests, and support undoable operations.

Example Diagram:

Command - overview

The mapping of the simple remote control is as below:

  • Invoker: SimpleRemoteControl
  • Command: Command
  • ConcreteCommand: LightOnCommand
  • Receiver: Light

Command

Adapter

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

Example Diagram:

Adapter - overview

The sketch mapping of the example is as below:

  • Target: Duck
  • Adapter: TurkeyAdapter
  • Adaptee: WildTurkey

Adapter

Facade

The Facade Pattern provides a unified interface to a set of interfaces in a subsytem. Facade defines a higher-level interface that makes the subsystem easier to use.

Example Diagram:

Facade - overview

TemplateMethod

The Template Method Pattern defines the skeleton of an algorithm in a method, deferring some steps to subclasses. Template Method lets subclasses redefine certain steps of an algorithm without changing the algorithm’s structure.

Example Diagram:

TemplateMethod - overview

Iterator

The Iterator Pattern provides a way to access the elements of an aggregate object sequentially without exposing its underlying representation.

Example Diagram:

Iterator - overview

The sketch of pattern Iterator is:

Iterator

Composite

The Composite Pattern allows you to compose objects into tree structures to represent part-whole hierarchies. Composite lets clients treat individual objects and compositions of objects uniformly.

Example Diagram:

Composite - overview

The sketch of pattern Composite is:

Composite

State

The State Pattern allows an object to alter its behavior when its internal state changes. The object will appear to change its class.

Example Diagram:

State - overview

The sketch of pattern State is:

State

Proxy

The Proxy Pattern provides a surrogate or placeholder for another object to control access to it.

Example Diagram:

Proxy - overview

The sketch of pattern Proxy is:

Proxy

Reference

designpatterns's People

Contributors

castielwong avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.