Wednesday, December 16, 2015

HeadFirst Design Pattern CH12: MVC Pattern

We know the MVC Pattern

The Controller will receive the request, call the appropriate Model, then pass the control to the view.



an example could be

Here we have the Student Class as the Model


The view is defined like this


The Controller is defined like this



NOTE: in the previous example, the controller calls the Model, sometimes the Model might update the controller about its state.

NOTE: int the MVC, we can consider the Model as Observer Pattern, the Controller as Strategy Pattern (You can change the controller whenever you want) and the View as a Composite pattern (consider the view as a page with multiple elements inside)



Sunday, December 13, 2015

Headfirst Design Patter Ch 11: Proxy Pattern

In this chapter we will talk about proxy pattern, the given example is not from the book

we have different types of proxy pattern,
Remote Proxy: you use this proxy to access remote objects, you define a stub and skeleton and start calling the remote object.

the other type is Virtual Proxy, in Virtual proxy you define as a proxy for another class, usually this class is expensive to create, so we use the proxy instead

example, an image class, the image needs to be loaded from the disk, this class will be considered as heavy, so we will define a proxy to be used instead of the class.


as you can see the RealImage class load the file from the disk

now we define a ProxyImage, which implements the same Image class.


The ProxyImage firstly load the image using RealImage object, after loading for the first time we start using this image through realImage.display().

now to use the code


NOTE: there is a difference between Facade and Proxy, in proxy you and the object has the same interface, however in facade you have totally a different interface.


Thursday, December 10, 2015

Head First Design Pattern Ch10: State Pattern

State pattern is about keeping state, It is very  similar to strategy pattern we will talk about that later.

we will start by an example to understand the problem that state patters solves

lets take an example of a Gum Ball machine. the gumball machine is a state machine which could have the one of the following states

you can represent these states in java by using final static vairables


the gumball machine has the following actions


now if you want to write code to implement this you will write something like this

as you can see a lot of else if, and if any change happens then we should change in many places.

the solution is to use the state pattern 


as you can see for each state you have a class and you do the implementation in that class.



as you can see above, an implementation for one of the states, also you can see that the state has a reference to the GumballMachine, and in insertQuarter() we change the state to something else.


as you can see above in GumballMachine, we have an instance of each state and we actually pass (this) to all these states.


Strategy Pattern vs State Pattern
in general they are similar, however we use them for different things, State pattern is like a state machines, so you define the way you move from one state to another. sure you can change the behaviour in Strategy Pattern but you change it in a different way.




Wednesday, December 9, 2015

Head First Design Pattern Ch9: Iterator Pattern & Composite Pattern

Iterator pattern is easy, you hide your internal implementation and provide a global way to iterate over elements:


Composite Pattern
Composite pattern is used when you want to have a tree representation,
for example the relation between employee is like a tree, a manager and employee


the implementation and usage is simple:


the usage is like this:



Monday, December 7, 2015

Head Fist Design Pattern CH8: Template Method Pattern and Hollywood Principle

template method define the skeleton of algorithm in a method, 
for example making coffee has the follwoing steps: 
1- boil water, 2- brewCoffeeGrinds 3- pourInCup, 4- addSugarAndMilk.

making tea has the following steps:
1- boil water, 2- steepTeaBag, 3- pourInCup, 4- addlemon

ass you can see that the method can be abstracted in a template method


as you can see we defined the template method prepareRecipe() as final.

brew() method, represent brewCoffeeGrinds and steepTeaBag, is defined as abstract so it can be implemented by the subclass.

same for addCondiments

now you can implement the class like this

in addition in template method you can use something called hook methods


hook methods has an empty implementation in the base class, the subclass can decide if it wants to implement it or not

OO Principle
Hollyowood Principle: dont call me i will call you. 
you can see it here in Template Method, the super class is calling the sub class when it needs anything.

Template Method vs Strategy Pattern
Notice the difference between Template Method and Strategy Pattern, 
1- Strategy Pattern depends on composition, here we have inheritance
2- In Strategy Pattern you can change the algorithm on runtime 
3- Template Method defines just the skeleton of the algorithm, Strategy Pattern is responsible for the algorithm itself.



Sunday, December 6, 2015

Head Fist Design Pattern CH7: Adapter and Facade Pattern and Principle of Least Knowledge

Adapter Pattern

lets say you have a Duck interface

and here is the implementation


also we have another interface which is Turkey 

as you can see in Turkey interface we have gobble() not quack.
here is the implementation.


the problem here, as WildTurkey and MallardDuck implement different interfaces you cannot use them interchangeably. 

in order to use them in a polymorphic style we should define an adapter to make the Turkey looks like Duck

the adapter looks like this


as you can see the Adapter implements the Duck interface, and it uses a Turkey instance.
the quack() method implementation is simple a call to the gobble() 

now to use the previous code


Facade Pattern
lets say that we have a home theater system 


as you can see, many classes are involved in this system, in order to watch a movie you should do all this:


which means if you want to use the system, you will make a lot of call, 

in case you have such a system, and you want to simplify the call you can define a Facade class, that simplify the call,
 

the Facade pattern hide all the internal complexity of a system, it is responsible for doing all the calls.



The Principle of Least Knowledge
it means that classes should not know much about each other.

in order to achieve that, each object can invoke only the following methods:

The Facade pattern is a good example of this Principle


Tuesday, November 17, 2015

Head First Design Pattern CH6: Command Pattern


in case you see something like this

public void actionPerformed(ActionEvent e)
{
  Object o = e.getSource();
  if (o = fileNewMenuItem)
    doFileNewAction();
  else if (o = fileOpenMenuItem)
    doFileOpenAction();
  else if (o = fileOpenRecentMenuItem)
    doFileOpenRecentAction();
  else if (o = fileSaveMenuItem)
    doFileSaveAction();
  // and more ...
}

then sure there is a problem, as you can see you are checking the instance and then you call an action method, this means that all these instances should implement a single interface which is the Command interface

// the Command Pattern in Java
public interface Command
{
  public void execute();
}

this interface should be implemented by all the types mentioned in actionPerformed

public class FileOpenMenuItem extends JMenuItem implements Command
{
  public void execute()
  {
    // your business logic goes here
  }
}

now simply you can write actionPerformed like this


public void actionPerformed(ActionEvent e)
{
  Command command = (Command)e.getSource();
  command.execute();
}

sure you can extend this pattern to do something more, for example maybe e.getSource() returns a list, you can simple loop over the list and call execute()

also you can add undo() function to the command interface and implement this function.