Design Patterns Flashcards
What is Design Patterns ?
Design patterns are solutions for problems faced during software development , they are best practices used by developers.
What is GOF (Gang Of Four) ?
GOF are 4 authors Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides published a book titled Design Patters - Elements of Reusable Object-Oriented Software which initiated concept of Design Patterns in software development.
What are design patterns based on?
*Program to an interface not implementation
*Favor object composition over inheritance
What is usage of design patterns ?
*Common platform for developers: Standard terminology and are specific to a particular scenario.
*Best practices
How many Design Patterns we have ?
23
Types of Design Patterns
*Creational Patterns
*Structural Patterns
*Behavioral Patterns
*J2EE Patterns
What are Creational Patterns ?
Creational Patterns provides a way to create object while hiding creation logic rather than using instantiation with new operator.
=>Benefits: Increase flexibility + reusable code
What are Structural Patterns ?
Structural Patterns consist on assembling objects and classes to larger structures , while keeping these structures flexible and efficient
What are Behavioral Patterns ?
Behavioral patterns are concerned with communication between objects.
What are J2EE Patterns ?
J2EE Patterns are concerned with the presentation tier
What is Factory Pattern ?
Factory is a creational pattern that provides an interface for creating objects in a super class, but allows subclasses to alter the type of objects that will be created.
Extends Abstract Factory class (not yet a certain factory , its true nature not revealed yet )
_________________________________________________
Examples:
*We have a Shape Factory we created Circle , customer want a Rectangle that’s where Factory pattern come , it creates the alter the shape independently of the other using Shape interface.
_________________________________________________
IS this correct ??? :
*We have a burger without knowing it’s ingredients
_________________________________________________
What is Abstract Factory Pattern ?
is a creational pattern for producing families of related objects without specifying their concrete classes.
it’s working around a super factory which creates other factories.
________________________________________
Example:
Creating an AbstractFactory containing RoundedShape and SimpleShape Factories , RoundedShape Factory to create
products with rounded shape ,same for SimpleShape Factory, which construct shape (interface) that contains all shapes.
What is Builder Pattern ?
Builder is a creactional pattern, for creating complex objects step by step. To produce different types and representations of an object with the same construction code.
*We have control over ingredients used to construct our burger
What is Singleton Pattern ?
Singleton is a creational pattern, that let you ensure that a class has only one instance while providing global access to this instance (Single Responsibility Principle), without the need to instantiate it from outside.
=>instantiate one time the only object inside class , if it change all components that use it change there object value
What is Observer Pattern ?
Observer is a behavioral pattern, that let’s you define a subscription mechanism to notify multiple objects about any events that happen to the object they’re observing ( when publisher make updates).
=> one to many relationship between publisher and subscribers.
=> Comes to picture when Singleton updates all its components in real time
*PubSub : when a user publish a video on youtube , youtube notify all subscribers
__________________________________________
Observer uses three main actors : Publisher(Subject) , Subcriber/Observer(Interface/abstract class), Concrete subscribers (clients).
What is Iterator Pattern ?
Iterator is a behavioral pattern that lets you traverse elements of a collection in a sequential manner without exposing its underlying representation (list,stack,tree…)
Iterator has two main actors : Iterator and Container
What is Strategy Pattern ?
Strategy is a behavioral pattern,that let’s you define a family of algorithms , put each one of them in a separate class , and make their objects interchangeable.
=>Strategy pattern has three main actors : Context ( use a strategy ) , Strategy (an interface to group all concrete strategies ), concrete strategies.
*OCP (open closed principle ):open to extensibility , closed to modification
What is Adapter Pattern ?
Adapter is a structural pattern,that allows objects with incompatibles interfaces to collaborate(works as a bridge between two incompatible interfaces).
This pattern involves a single class which is responsible to join functionnalities of independant of incompatible interfaces
_________________________________________
Examples:
*When we have an XML data and we need to analyze it using a library that accept only json data , an adapter can convert XML to JSON ( and can do it also on inverse sense).
*When a Usb port can t enter a computer we use an adapter so it is convenient for Usb port
What is Facade Pattern ?
Facade is a structural pattern, that provides an interface to the client to access a system , in order to hide complexities of the system.
*conceal unpleasant / credible reality (complex process) with outward appearance (simplified and easy use and command).
_____________________________________________
Examples:
*A client need a shop , he contact an agent in the shop (Facade class) who manage shopping operations(Interface provided) like packing , delivry , payment process,taxes…