Codinko- Java Coding Tutorials

Category: design patterns

Java – Object Oriented Design Principles

Object Oriented Design Principles

Nicely explained here :

http://www.codeproject.com/Articles/567768/Object-Oriented-Design-Principles

Software architecture

like MVC, 3-Tier, MVP tells us how overall projects are going to be structured.

Design pattern

allows us to reuse the experience or rather, provides reusable solutions to commonly occurring problems. Example – an object creation problem, an instance management problem, etc.

Principles

tell us, do these and you will achieve this. How you will do it is up to you.

object oriented design

In the same way, object oriented design is filled with many principles which let us manage the problems with software design.

  1. Class Design principles – Also called SOLID

  2. Package Cohesion Principles

  3. Package Coupling principle

SOLID

It’s an acronym of five principles [introduced by Mr. Robert Martin (commonly known as Uncle Bob) i.e.,

  • Single responsibility,
  • Open-closed,
  • Liskov substitution,
  • Interface segregation and
  • Dependency inversion.

It’s said (Wikipedia) when all five principles are applied together intend to make it more likely that a programmer will create a system that is easy to maintain and extend over time

Another important Concept: Programming to an Interface

Read from here : http://stackoverflow.com/questions/383947/what-does-it-mean-to-program-to-an-interface

 

 Notes: 

http://searchstorage.techtarget.com/definition/orthogonal
orthogonal design means there is only one way to change the property of the system you are controlling — executing Operation A exerts no impact on Operation B

References:

http://www.codeproject.com/Articles/567768/Object-Oriented-Design-Principles

 

 

Java Design Patterns – Proxy,delegate,decorator

Proxy design pattern

  • A proxy, in its most general form, is a class functioning as an interface to something else.
  • In short, a proxy is a wrapper or agent object that is being called by the client to access the real serving object behind the scenes.

example-1

See example on https://en.wikipedia.org/wiki/Proxy_pattern

“The idea of the Proxy pattern is that you have an object that stays in front of another object and has the same interface as this object. So, if some operation has to be performed before another operation, you can make use of a proxy, to abstract the caller and simplify its code. For example, we have an object that has a getter method that retrieves a list of Strings, but we have to go to the database to retrieve this list. If there’s a proxy, the caller simply invokes this getter method on the proxy object, and the proxy verifies whether the target object already has the list. If not, it goes to the database, retrieves the list, sets it on the target object and returns to the caller. Therefore, the proxy object can execute code before and after the code of the actual targeted object”.

Credits for the last paragraph:  RobertoPerillo@http://www.coderanch.com/t/637844/patterns/Difference-Proxy-Delegate

example-2

I would say another good example of Proxy pattern in action is in Spring Declarative transaction management when you use @Transactional.
Declarative transaction management is enabled with the support of AOP Proxies.

See this diagram

http://docs.spring.io/spring/docs/2.5.x/reference/transaction.html#transaction-declarative

spring-declarative-tx-aop-proxy

Delegate pattern

The delegation pattern is a design pattern in object-oriented programming where an object, instead of performing one of its stated tasks, delegates that task to an associated helper object. There is an Inversion of Responsibility in which a helper object, known as a delegate, is given the responsibility to execute a task for the delegator.

example-1

See example on https://en.wikipedia.org/wiki/Delegation_pattern

Decorator Pattern

It’s a complex case of Delegate pattern.

The more complex case is a Decorator Pattern that by using interfaces, delegation can be made more flexible and typesafe

example-1

See example on https://en.wikipedia.org/wiki/Delegation_pattern

Other design patterns

Simple Factory,Factory method,Abstract Factory

Design patterns – Simple Factory,Factory method,Abstract Factory

Design patterns – Simple Factory,Factory method,Abstract Factory

Simple Factory and Factory method patterns

This is one of the pattern not born from GOF and most of the people considers this as the default Factory method pattern.

Here, we will just take out object creation process out of the client code and put into some other class.

1) Factory method pattern

The factory method pattern is a creational pattern that uses factory methods to deal with the problem of creating objects without having to specify the exact class of the object that will be created. This is done by creating objects by calling a factory method—either specified in an interface and implemented by child classes, or implemented in a base class and optionally overridden by derived classes—rather than by calling a constructor.

How does it differ from the simple factory?

The factory method pattern is the best-known use of factories and factory methods, but not all uses of factory methods are examples of the factory method pattern – only when inheritance is involved (a class implementing an interface, or derived class, implements a factory method) is it an example of the factory method pattern
More basic uses of factories are not examples of the factory method pattern, and may instead be referred to as the factory patternor a simple factory;these are discussed at factory

A Simple Factory is just a public static factory method creating objects based on some input parameters and returning the object. The return type could be as simple as a Class of the same type as the objects created. [When the return type is Interface or Abstract class, it becomes somewhat similar to factory method pattern. You use an interface as in first two examples, and you use Abstract class when derived class implements the abstract method of the abstract class – third example]

example1

example2

example3

 

2. Abstract Factory Pattern

517px-Abstract_factory.svg

https://en.wikipedia.org/wiki/Abstract_factory_pattern

code example

References:

Simple Factory pattern , Factory method pattern:
codeproject.com /Articles/716413/ Factory-Method-Pattern-vs-Abstract-Factory-Pattern

https://en.wikipedia.org/wiki/Factory_method_pattern

Abstract Factory Pattern:
https://en.wikipedia.org/wiki/Abstract_factory_pattern

 

 

Copyright © 2020 Codinko- Java Coding Tutorials

Theme by Anders NorenUp ↑