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
- Chain Of Responsibility
- Command
- Interpreter
- Iterator
- Mediator
- Memento
- Observer
- State
- Strategy
- Template Method
- Visitor
- 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
Brick Lane Graffiti Usain Bolt (CC BY 2.0) by Martin Pettitt
- Implementation
UML:
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
- Known uses
- Story
Clone itself.
Sheep Dolly is the first mammal to be cloned, so Dolly is a duplicate.
- Image
Geni, Dolly the sheep 2016, CC BY-SA 4.0
- Implementation
UML:
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
- Known uses
- 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
By DonES (Own work) [GFDL or CC-BY-SA-3.0], via Wikimedia Commons
- Implementation
UML:
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
- Known uses
- java.lang.StringBuilder.append()
- java.lang.StringBuffer.append()
- java.lang.Appendable
- javax.swing.GroupLayout.group.addComponent()
- java.nio.ByteBuffer.put()
- java.nio.CharBuffer.put()
- java.nio.ShortBuffer.put()
- java.nio.IntBuffer.put()
- java.nio.LongBuffer.put()
- java.nio.FloatBuffer.put()
- java.nio.DoubleBuffer.put()
- 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
Cake molds, Han people, metal - Museum of Vietnamese History - Ho Chi Minh City, By Daderot (Own work) [CC0], via Wikimedia Commons
- Implementation
UML:
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
- Known uses
- 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
Poker Cards Back (CC 0)
- Implementation
UML:
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
- Known uses
- 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
- Implementation
UML: Class Adapter
Object Adapter:
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
- 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
By Liftarn (Own work) [Public domain], via Wikimedia Commons
- Implementation
UML:
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
- 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
By Priwo (photo taken by de:Benutzer:Priwo) [Public domain], via Wikimedia Commons
- Implementation
UML:
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
- 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
steve lyon from los angeles, ca, usa, 2013 Porsche 911 Carrera S (8233337583), CC BY-SA 2.0
- Implementation
UML:
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
- 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
By WeMaxx1248 (Own work) [CC BY-SA 4.0], via Wikimedia Commons
- Implementation
UML:
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
- 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
By 04hutts (Own work) [Public domain], via Wikimedia Commons
- Implementation
UML:
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
- 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
By John Watkins (Kenwoon Mengyee, Burmese ambassador, The Envoy Extraordinary and Minister Plenipotentiary) [Public domain or Public domain], via Wikimedia Commons
- Implementation
UML:
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
- 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:
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
- 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:
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
- Story
A person who translates orally from one language into another.
- Image
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:
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
- 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
Open Book by Dave Dugdale, on Flickr" (CC BY-SA 2.0) by Dave Dugdale
- Implementation
UML:
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
- 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
- Implementation
UML:
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
- 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
- Implementation
UML:
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
- 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
- Implementation
UML:
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
- 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
- Implementation
UML:
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
- 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
Credit Card (CC BY 2.0) by ThomasKohler
- Implementation
UML:
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
- 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:
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
- 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
- Implementation
UML:
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