Git Product home page Git Product logo

100-words-design-patterns-java's Introduction

Build Status Coverage Status GitPitch GitHub Stats License

100 words GoF Design Patterns in Java

Introduction

Idea: describe GoF Design Patterns on a simple way. Each pattern will be described with following structure:

  • Story(less than 100 words)
  • Implementation in Java

GoF Design Patterns

Creational Patterns

Structural Patterns

Behavioral Patterns

Singleton
  • Story

Singleton ensures that only one(single) object can be created from the class.

Men's 100 meters world record holder is a singleton. There can be at most one active "Men's 100 meters world record holder" at any given time. Regardless of who that person is the title, "Men's 100 meters world record holder" is a global point of access that identifies the fastes person in the world.

  • Image

alt text

Brick Lane Graffiti Usain Bolt (CC BY 2.0) by Martin Pettitt
  • Implementation

UML:

alt text

Source Code:

Clone repo:

$  git clone https://github.com/dstar55/100-words-design-patterns-java.git .

Move to singleton folder:

$  cd /src/main/java/com/hundredwordsgof/singleton
Prototype
  • Story

Clone itself.

Sheep Dolly is the first mammal to be cloned, so Dolly is a duplicate.

  • Image

alt text

  • Implementation

UML:

alt text

Source Code:

Clone repo:

$  git clone https://github.com/dstar55/100-words-design-patterns-java.git .

Move to prototype folder:

$  cd /src/main/java/com/hundredwordsgof/prototype
Builder
  • Story

Separates the construction of a complex object from its representation so that the same construction process can create different representations.

This pattern is used by PC shops to contruct PC's. PC is combination of various parts like CPU, motherboard, memory, storage, power supply, video card, etc. To build a PC same construction process is used even for each part we have different variation. Whether a customer picks a classical hard disk or SSD for storage, the construction process is the same.

  • Image

alt text

By DonES (Own work) [GFDL or CC-BY-SA-3.0], via Wikimedia Commons
  • Implementation

UML:

alt text

Source Code:

Clone repo:

$  git clone https://github.com/dstar55/100-words-design-patterns-java.git .

Move to builder folder:

$  cd /src/main/java/com/hundredwordsgof/builder
Factory Method
  • Story

Defines an interface for creating objects, but lets subclasses decides which class to instantiate. Plasticine is used for children's play. Plasticine is injected into predefined molds. The class of end product(ball, toy, sculpture, cake) is determined by the mold.

  • Image

alt text

Cake molds, Han people, metal - Museum of Vietnamese History - Ho Chi Minh City, By Daderot (Own work) [CC0], via Wikimedia Commons
  • Implementation

UML:

alt text

Source Code:

Clone repo:

$  git clone https://github.com/dstar55/100-words-design-patterns-java.git .

Move to factorymethod folder:

$  cd /src/main/java/com/hundredwordsgof/factorymethod
Abstract Factory
  • Story

Provides an interface for creating families of related objects, without specifying concrete classes.

This pattern is found in the cards stamping equipment used in the manufacture in order to produce playing cards. Cards stamping machine is an Abstract Factory which produces a cards. The same machine is used to stamp French, Italian or German cards.

  • Image

alt text

Poker Cards Back (CC 0)
  • Implementation

UML:

alt text

Source Code:

Clone repo:

$  git clone https://github.com/dstar55/100-words-design-patterns-java.git .

Move to abstractfactory folder:

$  cd /src/main/java/com/hundredwordsgof/abstractfactory
Adapter
  • Story

Allows that interface of an existing class to be used from another interface.

Adapters are often used in daily life, for example eletrical adapter is a device that converts attributes of one electrical device or system to those of an otherwise incompatible device or system. Some modify power or signal attributes, while others merely adapt the physical form of one electrical connector to another.

  • Image

alt text

  • Implementation

UML: Class Adapter

alt text

Object Adapter:

alt text

Source Code:

Clone repo:

$  git clone https://github.com/dstar55/100-words-design-patterns-java.git .

Move to adapter folder:

$  cd /src/main/java/com/hundredwordsgof/adapter
Bridge
  • Story

Decouple an abstraction from its implementation so that the two can vary independently.

Steering wheel is an example of the Bridge. The purpose of a steering wheel is to transmit driver's input to the steered wheels in order to dynamically change direction of the vehicle. There are different implementations of the steering wheels used in cars, buses, tracks, tractors and formulas.

  • Image

alt text

By Liftarn (Own work) [Public domain], via Wikimedia Commons
  • Implementation

UML:

alt text

Source Code:

Clone repo:

$  git clone https://github.com/dstar55/100-words-design-patterns-java.git .

Move to bridge folder:

$  cd /src/main/java/com/hundredwordsgof/bridge
Composite
  • Story

Compose objects into tree structures to represent part-whole hierarchies. Group of objects is to be treated in the same way as a single instance of an object.

Lego brick represents Composite pattern. A brick is a basic object, but on a same time brick is a container which can hold other bricks in order to construct complex objects.

  • Image

alt text

By Priwo (photo taken by de:Benutzer:Priwo) [Public domain], via Wikimedia Commons
  • Implementation

UML:

alt text

Source Code:

Clone repo:

$  git clone https://github.com/dstar55/100-words-design-patterns-java.git .

Move to composite folder:

$  cd /src/main/java/com/hundredwordsgof/composite
Decorator
  • Story

Attach additional responsibilities to an object dynamically.

The spoilers that are added to a car are examples of the Decorator. The spoilers do not change the car itself, but adds additional functionality which is to 'spoil' unfavorable air movement across a body of a vehicle in motion, usually described as turbulence or drag.

  • Image

alt text

  • Implementation

UML:

alt text

Source Code:

Clone repo:

$  git clone https://github.com/dstar55/100-words-design-patterns-java.git .

Move to decorator folder:

$  cd /src/main/java/com/hundredwordsgof/decorator
Facade
  • Story

Facade hides the complexity of the system and provides an interface to the client from where the client can access the system.

You want to organize a marriage reception with dinner for 100 people. So in order to organize such event, you need to find and decorate a hall where the event will happen, then you need to find a music, organize flowers, send invitations and so on and so on.

If this is to much for you than you can find event manager which will organize event for you.

This is a typical example for Facade.

  • Image

alt text

By WeMaxx1248 (Own work) [CC BY-SA 4.0], via Wikimedia Commons
  • Implementation

UML:

alt text

Source Code:

Clone repo:

$  git clone https://github.com/dstar55/100-words-design-patterns-java.git .

Move to facade folder:

$  cd /src/main/java/com/hundredwordsgof/facade
Flyweight
  • Story

Remove duplicates.

Flyweight pattern is used to reduce memory by loading only the data necessary to perform action. Database normalization is flyweight. Normalisation, is the process of organizing the columns (attributes) and tables (relations) of a relational database to minimize data redundancy.

  • Image

alt text

By 04hutts (Own work) [Public domain], via Wikimedia Commons
  • Implementation

UML:

alt text

Source Code:

Clone repo:

$  git clone https://github.com/dstar55/100-words-design-patterns-java.git .

Move to flyweight folder:

$  cd /src/main/java/com/hundredwordsgof/flyweight
Proxy
  • Story

Provide a surrogate or placeholder for another object to control access to it.

Envoy Extraordinary is a Proxy. He is an accredited messenger, agent, or representative who is sent by one government to represent it in dealing with another government.

  • Image

alt text

By John Watkins (Kenwoon Mengyee, Burmese ambassador, The Envoy Extraordinary and Minister Plenipotentiary) [Public domain or Public domain], via Wikimedia Commons
  • Implementation

UML:

alt text

Source Code:

Clone repo:

$  git clone https://github.com/dstar55/100-words-design-patterns-java.git .

Move to proxy folder:

$  cd /src/main/java/com/hundredwordsgof/proxy
Chain Of Responsibility
  • Story

The Chain of Responsibility allows an object to send a command without knowing which object will receive and handle it. The request is sent from one object to another making them parts of a chain and each object in this chain can handle the command, pass it on or do both.

Service request to call center is example of the Chain of Responsibility. Request can be handled at front desk level, supervisor level or any higher level. Correct handler of request is only known during execution of the request when request is traversing at various levels.

  • Implementation

UML:

alt text

Source Code:

Clone repo:

$  git clone https://github.com/dstar55/100-words-design-patterns-java.git .

Move to chainofresponsability folder:

$  cd /src/main/java/com/hundredwordsgof/chainofresponsibility
Command
  • Story

Issue requests to objects without knowing anything about the operation being requested or the receiver of the request.

When your car needs service you visit Car Service Center. On reception you explain a problem and you leave a car. The person at reception encapsulates the problem in to order for Car Technician. The order is queued internaly. Car Technician will receive a request and fix a problem.

  • Implementation

UML:

alt text

Source Code:

Clone repo:

$  git clone https://github.com/dstar55/100-words-design-patterns-java.git .

Move to command folder:

$  cd /src/main/java/com/hundredwordsgof/command
Interpreter
  • Story

A person who translates orally from one language into another.

  • Image

alt text

Polish Sign Language - letter C, By Tomt87 (Own work) [CC BY-SA 4.0 (http://creativecommons.org/licenses/by-sa/4.0)], via Wikimedia Commons
  • Implementation

UML:

alt text

Source Code:

Clone repo:

$  git clone https://github.com/dstar55/100-words-design-patterns-java.git .

Move to command folder:

$  cd /src/main/java/com/hundredwordsgof/interpreter
Iterator
  • Story

Book is a set of written, printed sheets bound together into a volume. You can browse through the book page by page, or quickly jump to interesting chapter. Process of browsing is example of Iterator pattern.

  • Image

alt text

Open Book by Dave Dugdale, on Flickr" (CC BY-SA 2.0) by  Dave Dugdale
  • Implementation

UML:

alt text

Source Code:

Clone repo:

$  git clone https://github.com/dstar55/100-words-design-patterns-java.git .

Move to iterator folder:

$  cd /src/main/java/com/hundredwordsgof/iterator
Mediator
  • Story

Defines an object that controls how a set of objects interact.

Radio Taxi is an example of the Mediator pattern. Taxi drivers communicate with the Mediator(Radio Taxi Call Center), rather than with each other.

When customer needs a taxi, he calls Radio Taxi Call Center. All taxis have a GPS unit which tells where the taxi is present right now, also there is a central information system which tells which taxi is available to serve the customer. The call center will contact the available taxi nearest to customer’s location and send them to serve the customer.

  • Image

alt text

Call Center Taxis Libres,By Jquemba (Own work) [Public domain], via Wikimedia Commons
  • Implementation

UML:

alt text

Source Code:

Clone repo:

$  git clone https://github.com/dstar55/100-words-design-patterns-java.git .

Move to mediator folder:

$  cd /src/main/java/com/hundredwordsgof/mediator
Memento
  • Story

Helps to restore an object’s state to it previous state.

Transactions are operations on the database that occur in an atomic, consistent, durable, and isolated fashion. A transaction can contain multiple operations on the database; each operation can succeed or fail, however a transaction guarantees that if all operations succeed, the transaction would commit and would be final. And if any operation fails, then the transaction would fail and all operations would rollback and leave the database as if nothing has happened.

This mechanism of rolling back uses the memento design pattern.

  • Image

alt text

States of transaction
  • Implementation

UML:

alt text

Source Code:

Clone repo:

$  git clone https://github.com/dstar55/100-words-design-patterns-java.git .

Move to memento folder:

$  cd /src/main/java/com/hundredwordsgof/memento
Observer
  • Story

Keep me updated.

Newslettter subscription demonstrate Observer pattern. A newsletter is a regularly distributed publication that is generally about one main topic of interest to its subscribers. Subscribers can subscribe or unsubscribe to the newsletters.

  • Image

alt text

  • Implementation

UML:

alt text

Source Code:

Clone repo:

$  git clone https://github.com/dstar55/100-words-design-patterns-java.git .

Move to observer folder:

$  cd /src/main/java/com/hundredwordsgof/observer
State
  • Story

Behavior depends on its state.

Pregnancy is time of great physical and emotional change for women. Everything from the size of her belly to the speed at which her heart beats will change over the nine months leading up to childbirth. Partly the result of hormonal fluctuations and partly the physical strain of carrying extra body weight, pregnant women can expect to buy new bras, search for ways to alleviate swollen ankles, gasp for breath after climbing a few stairs, and marvel at how quickly their nails grow.

  • Image

alt text

  • Implementation

UML:

alt text

Source Code:

Clone repo:

$  git clone https://github.com/dstar55/100-words-design-patterns-java.git .

Move to state folder:

$  cd /src/main/java/com/hundredwordsgof/state
Strategy
  • Story

Select an algorithm at runtime.

Payment options in a Shopping Cart is an example of Strategy. User can choose various payment options like Master Card, Amex or PayPal. Any of these payment options will pay items in Shopping Cart, and they can be used interchangeably. The user must choose the Strategy based on his possibilities, preferences.

  • Image

alt text

Credit Card (CC BY 2.0) by ThomasKohler
  • Implementation

UML:

alt text

Source Code:

Clone repo:

$  git clone https://github.com/dstar55/100-words-design-patterns-java.git .

Move to state folder:

$  cd /src/main/java/com/hundredwordsgof/strategy
TemplateMethod
  • Story

Defines a skeleton of an algorithm in an operation. Algorithm will have common and specialized part.

Daily routine is example of the Template method. Every day workers get up(common part), go to work(specialized part), go home and go to sleep. There are workers with different professions like enginners, teachers, etc. During work engineer will fix machines while teacher will teach childern how to read and write. At the end of the day worker go home, have a dinner and go to sleep.

  • Implementation

UML:

alt text

Source Code:

Clone repo:

$  git clone https://github.com/dstar55/100-words-design-patterns-java.git .

Move to template folder:

$  cd /src/main/java/com/hundredwordsgof/templatemethod
Visitor
  • Story

Allows for one or more operations to be applied to a set of objects at runtime, decoupling the operations from object structure.

Shopping in supermarket is example of the Visitor pattern. You pick a products and put them in shopping cart. When you get to the checkout, the cashier acts as a visitor, taking the disparate set of elements, some with prices and others that needs to be weighted, in order to provide you with total.

  • Image

alt text

Cashier in Supermarket, CC0 Public Domain
  • Implementation

UML:

alt text

Source Code:

Clone repo:

$  git clone https://github.com/dstar55/100-words-design-patterns-java.git .

Move to visitor folder:

$  cd /src/main/java/com/hundredwordsgof/visitor

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.