Visitor
Let’s take an example of an application which manages Router in different environments. Router should be able to send and receive data from other nodes. Application should have provision to configure routers in different environment. Also, there should be a flexibility of configuring router for new environments without major changes in the source code.
We have different Routers which are configured for Windows
and Linux. Now if we want to configure the router for Apple then we just need
to create an AppleConfigurator class and inherit the IRouterVisitor to make the
router accessible for Apple environment.
Chain of responsibility
This patterns deals with the request coming from an event
and it passes to different handlers till the appropriate handler process the
request. The simplest example I can take is of vending machine where we insert
the coin and machine checks the coin till it is appropriately placed to its
proper container.
Let’s say a user inserted one dollar to the vending machine.
The handler checks for each handler to validate the coin. FiveCentHandler
checks the coin and it did not match the value and it passes back to the
handler and then again handler will look for DimeHandler, and then One Quarter
handler till it reaches the correct handler i.e. One Dollar Handler to process
the coin.
Active Record
Active Record pattern in general used in the data driven
application. Each table row is modeled by an instance of a class. The class
property will map to the column of table or database object.
This type of pattern is used by ORM like EntityFramework,
Genomes etc.
Let say we have Employee class which is a table in database.
In this class there are properties like EmployeeID, EmployeeName,
EmployeeSalary which correspond to the columns in the database Emplyee table.
And not the least there are some methods which will perform some Action on the
database table.
Strategy
Strategy pattern allow the function to choose the algorithm at the runtime. They are encapsulated to each other making them interchangeable at any time.
Let us take an example of Navigation device, where the
driver is driving a car and want to reach his destination. From a start point
there are multiple routes and in case the driver takes a different direction or
wrong direction Navigation device has to adjust to choose the best possible
route for a driver.
Singleton
We use the singleton patter to create an object which has
got only one instance and provide global point of access to it.
Let take an example where we want to use the configuration
for logging which is used by all the clients.
Command
When we want to encapsulate a request as an object and then
we use this object later to call a method. This may contain different request.
Let’s take an example of multiple button place on a single
page. Each button performs different action like save, delete, and update. Now,
after clicking on Update client wants to retrieve the old values then he has to
UNDO the stuff and if he wants to move to the last updated values then he has
to REDO it. For single UNDO and REDO, the task seems to be easy but what happen
when the user has done multiple updates and want to REDO – UNDO multiple times
to reach or verify the values which has got updated. The same scenario may come
for other operations also.
To achieve above, we will be wrapping each command into an
object and add the object to a list when a command is executed. When the user
want to do the UNDO-REDO, we will scroll the list which we are maintain. This
object may contain log information which can be used for the results.
Below is a typical design for command pattern. And let see
how the above example fits in below UML.
Client
is my Application where we have buttons. My Application will create an instance
of “ConcreteCommand” which is a request made of Receiver. Here we can say the
client has clicked on “Update” Button. This “ConcreteCommand” object will be
initiated by Application i.e Client. This “ConcreteCommand’ object is passed to
an Invoker through a buton_click event. In the button click event, we call the
Execute() method from Command Class. This Execute() method will create the list
of events and log the data. Now, if UNDO needs to be done then the previous
data is retrieved and adding a pointer to one item up.
No comments:
Post a Comment