Skip to content

This repository contains S.O.L.I.D principles brief explanation and example of each principle and its benefits, Also contains famous antipatterns to avoid while coding, Atlast it, contains an explanation, benefits, drawbacks, UML diagram, and implementation in java for multiple design patterns with different types as (Creational , Behavioral , S…

Notifications You must be signed in to change notification settings

ahmedmaged3k1/DesignPatterns

Repository files navigation

Creational Design Patterns

“Creational design patterns are design patterns that deal with object creation mechanisms, trying to create objects in a manner suitable to the situation. The basic form of object creation could result in design problems or added complexity to the design. Creational design patterns solve this problem by somehow controlling this object creation.”

Factory Design Pattern

Uses

• When a class can't anticipate which kind of class of object it should create
• When the classes to be created are derived from the same superclass/interface
• When you want to insulate the client from the actual type that is being instantiated

Examples

java.util.Calendar, java.util.ResourceBundle and java.text.NumberFormat

DrawBacks

• Requires keeping factory methods in sync with domain classes
• No way to change an implementing class without recompiling

UML

image


Abstract Factory Design Pattern

Uses

• Is often used when there is a need to use different sets of objects and where those objects could be added or changed some time during the lifetime of an application

Examples

javax.xml.parsers.DocumentBuilderFactory and javax.xml.xpath.XPathFactory

DrawBacks

the possibility of unnecessary complexity and extra work during the initial implementation

UML

image


Builder Pattern

Uses

• Is often used when you want to hide the actual creation process from clients • The construction process should allow different representations of the resulting object

Examples

java.lang.StringBuilder#append and java.lang.StringBuffer#append

DrawBacks

• “It does create more code (and could introduce more complexity) in the DTO than if you had for example contructor arguments and/or setters/getters.”1

UML

image


Prototype Pattern

Uses

• When a system needs to be independent of how the objects are created • Adding and removing objects at runtime • Specifying new objects by changing an existing structure

Examples

• There are no real implementation of the Prototype Pattern in JDK, but Object.clone() is a good candidate which follows the design

DrawBacks

• Each subclass of Prototype must implement the Clone operation • Could be difficult with existing classes which have some internal objects with circular references, or which do not support copying

UML

image


Singelton Pattern

Uses

• When only one instance or a specific number of instances of a class are permitted

Examples

java.lang.Runtime#getRuntime()and java.awt.Desktop#getDesktop()

DrawBacks

• Are generally used as a global instances à Try to avoid having global variables

UML

image


About

This repository contains S.O.L.I.D principles brief explanation and example of each principle and its benefits, Also contains famous antipatterns to avoid while coding, Atlast it, contains an explanation, benefits, drawbacks, UML diagram, and implementation in java for multiple design patterns with different types as (Creational , Behavioral , S…

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages