Server component: it is an actual implementation of code which is providing services to client
Client component:
Client component uses the services of the server component
if we implemented the calculator functionaity as server component.Client component is the one which is using this calculator functionality.
Design pattern with real usecase scenarios:
What is the purpose of Object Oriented Design?
Objective of the Object Oriented Design is to create the loosely coupled client and server components, so that it
can allow us to add features with minimal changes, allows us to extend the features.Every design has its own merits and demerits.Mostly merits and demerits are unknown to the designer based on his exposure or intelligence.
Design patterns are proven design with known merits and demerits. Objective of the design pattern is to create loosely coupled design components.
Design pattern has 3 different categories:
1) Creational design pattern
2) Structural design pattern
3) Behavioral design pattern
1)Situations to use creational design pattern I) when we need to simplify the object creation logic
II) Decoupling client logic regardless of frequent changes in Object creation logic
2) Situations to use Structural design pattern:
I) building new types or exending existing types
II) new types addition with simple design
III) Overcome problems like class explosion and machine resource limitations
3)Situation to use behavioral design pattern: I)object communication with loosely coupled design
II)scalable design with respect to addition/modification of algorithms
III) division of responsibility to classes
Creational design patterns:
1) Abstract Factory pattern: creates family of related classes. without changing client logic, we can provide different services from server.
It is mainly used in frameworks.Multimedia framework uses Plugins for decoder/encoder/parsers. Example:
Directshow MMF filters,GStreamer,OpenCORE plugins,Media Foundation transform plugins
2)Builder pattern - Separates object construction from its representation
Sample application from Wiki is confusing...
3) Factory method -creates instance of several derived classes
- Similar to abstract factory pattern
4) Prototype: copying or cloning an instance
Ex:If the client is accessing elements via Iterator pattern, if it wants to clone an object, it can do it by this prototype pattern.
whenever client is having access to interface if it wants to copy or clone an object, it can make use of the clone.
class IEncoder
{
public :
virtual IEncoder* clone() = 0;
};
class CJpegEncoder: public IEncoder
{
public:
CJpegEncoder(){}
~CJpegEncoder(){}
virtual IEncoder* clone() { return new CJpegEncoder();}
};
Client code:
IEncoder *pEncoder = new CJpegEncoder();
IEncoder* pEncoder2 == pEncoder->clone(); //creating the copy of an instance or cloning of an instance
5)Singleton-class of which only a single instance can exist
Example: Single window manager instance to manage multiple windows in GUI
note:use private constructor and private copy constructor without definition to disable others to create/copy the object
Structural design patterns:
6)Adapter or wrapper or translator -match interfaces of different classes.
Ex: From UI to database updation, we can directly add UI data to database. Instead of it, add intermediate class. so UI validation will be done in upper layer,only database required fields are passed as a structure or class object to database.
while reading from database also, read as an object or structure and interpret it according to UI. We will be able to add UI or database changes with minimal changes.
7)Bridge Pattern- decouples an abstraction from implementation.Graphical User Interface Frameworks use the bridge pattern to separate abstractions from platform specific implementation.For example GUI frameworks separate a Window abstraction from a Window implementation for Linux or Mac OS using the bridge pattern.
8)Composite pattern- A tree structure of simple and composite objects
Ex: Filesystem - file system contains files,folders in a tree structure
Graphics Editor can have the one shape or more shapes inside another shape or group of shapes
9)Decorator- add responsibilities to objects dynamically
Ex:Implement the window draw and develop the derived class HorizontalScrollbar window by overriding draw with drawing horizontal bar to window.
10) Facade - single class represents the entire subsystem.A facade is an object that provides a simplified interface to a larger body of code, such as a class library.Ex:Computer is a single class that represents the CPU,memory,OS entire subsystem.
11)Flyweight pattern- This is mainly used to reduce memory footprint
Ex:whenever we want to display the Verdana font 'a', it will have more bytes to represent the pixels in verdana font 'a'. Instead of using all the pixels, just assign some ASCII character and map it to the predefined verdana font 'a' pixels.
12)Proxy - an object representing another object
this is used to minimize the dependencies. if the class is accessing more header files, instead of accesing this class from client, provide the proxy class with minimal dependancies, so the client can use it without any problem
Ex: StageFrightPlayer and AwesomePlayer. All AwesomePlayer calls are being invoked from StagefrightPlayer.
This StagefrightPlayer proxy class is used in mediaplayerservice.cpp which contains many player creations.
Ex 2: proxy class between remoting server and client
Behavioral design patterns:
13)Chain of Responsibility: -a way of passing request between chain of objects
- base class will have a sequence of pointers to the derived classes. Default window have the capability to handle UI operations.
From this window, Button,OptionButton,checkbox windows are created in Windows SDK. If Button clicks are not handled, the default
window will handle the scenarios. Ex: Quit or closing the window If the user/application doesnt provide implementation, the default window handles it.
Ex: Item purchase order,the purchase request is sent to multiple managers/admins. when any one has approved it,
proceed to process the purchase request further.
Ex2:whenever mouse clicks or UI clicks are not handled by application, the default window handles it . if the application overrides/handles it, then the default/framework window wont handle the clicks in Windows or GUI OS.
14)Command - Encapsulates a command request as an object
Ex1:Enqueueing the commands to the queue and processing it in event loop
Ex2:Implementation example: passing AMessage request object to ALooper class in android
Ex3:Adding cut,copy,paste command operations, parameters as an object to queue and processing it with appropriate cut,copy and paste operation in word or graphics editor
15) Interpreter - this is used to interpret the language.Difficult to understand...
16) Iterator -sequentially access the server elements as collection.It will gives the total number of elements and get functions to access the individual elements Examples: Iterators in C++ STL, Iterators in C#/java
17) Mediator -defines an object that encapsulates how a set of objects interact.Ex: Dialog which can hold combo box control or label control or textbox control. The value changes are updated/controlled by dialog.The controls are not aware of the existence of other controls .
18) Memento - provides ability to restore object's previous state.
Ex:Undo operation in Word/Graphics Editor, rollback operation in Database
19) Observer - Ex: register the callback function to the server components and server will notify the client by calling this function on any error or failure or any important events.
this is also called as publish-subscriber pattern.
20) State pattern:
- provides cleaner way to provide an object to change its behavior at runtime. This state based pattern is used in Jellybean's ACodec implementation which will be used in NuPlayer on HLS scenario.
21)strategy pattern - wrapper class around a runtime polymorphism.This wrapper class will be used by clients.
22) template pattern- Ex: C++ template class
23) Visitor pattern - little bit confusing..
Labels: Design Pattern