Design Patterns

Design Pattern is a solution to a general software problem within a particular context. Design patterns allow us to reuse the knowledge of experienced software designers.

Design patterns utilized in Java APIs:

  1. MVC is used extensively throughout Swing API.
  2. The classes java.lang.System and java.sql.DriverManager are examples of the Singleton pattern, although they are not implemented using the approach recommended in the GoF book with static methods.
  3. The Adapter pattern is used extensively by the adapter classes in java.awt.event.
  4. The proxy pattern is used extensively in the implementation of Java’sRemoteMethod Invocation and Interface Definition Language (IDL) features.
  5. The Bridge pattern can be found in the separation of the components in java.awt and their counterparts in java.awt.peer.

How do you make sure only one instance of my class is ever created?

This is an instance where the Singleton desing pattern would be used. You need to make the constructor private and provide a static method to get the sole instance, where the first time the instance is retrieved it is created.

When will you used delegation pattern instead of inheritance to extedn a class’s behaviour?

Since inheritance relationship is defined at compile time, a class cant change its superclass dunamically during program execution. Modifications to a superclass automatically propagate to the subclass. Inheritance creates a strong static coupling between a superclass and its subclasses.

Delegation can be viewed as a relationship between objects where on object forwards certain method calls to another object, called its delegate. The primary advantage of delegation is runtime flexibility- the delgate can easily be changed at runtime. But unlike inheritance, delegation is not directly supported by most popular OO languages, and it doesnt facilitate dynamic polymorphism.

What is data access layer? It receives requests from business layer and speaks to DB, get the result and return to business object. All SQLs and stored procedures will be placed.

Creational patterns: Abstract Factory, Builder, Factory method, Prototyp, Singleton

Structural patterns: Adapter, Bridge, Composite, Decorator, Facade, Flyweight, Proxy

Behavioral patterns: Chain of responsibility, command, iterator

J2EE patterns: MVC, Business Delegate, composite entity, DAO, Front Controller, Service Locator

AbstractFactory: provides one level of interface higher thatn the factory pattern. It is used to return one of several factories. (Uses and benefits: Creates families of related or dependent objects like Kit. Provides a class library of products, exposing interface not implementation. needs to isolate concrete classes from their super classes. A system needs independent of how its products are created, composed and represented.

Singleton: One instance of aclass or one value accessible globally in an application. (uses and benefits: ensure unique instance by defining class final to prevent cloning, may be extensible by the subclass by defining subclass final. make a variable or method public or/and static.

Adapter: convert the existing interfaces to a new interface to achieve compatibility and reusability of the related classes in one application. also known as wrapper pattern. Use and Befits: make unrelated classes owrk together. multiple compatibility. increase transparency of classes. make a pluggable kit. high class reusability.

Bridge: Decouple an abstraction or interface from its implementation so that the two can vary independently. uses and Benefits: want to separate abstraction and implementation permanently. share an implementation among multiple objects. want to improve extensibility. hide implementation details from clients.

Facade: Make a complex system simpler by providing a unified or genreal interface, which is a higher layer to these subsuystems. uses and beneftis: want to reduce complexities of a system. decouple subsystems, reduce its dependency, and improve protability. make an entry point to your subsystems. minimize the communication and dependency between subsystems. security and performance consideration. shield clients from subsystem components.

MVC: architecture design pattern. model means data, view means representation and controller means representation and controller works on data and representation. MVC focuses to decouple the triad relationship between model, controller and view. improves maintainability, increase object reusabnility, achieve design flexibility.

Business delegate: intermediate class decouples between presentatio-tier clients and business services. Uses and benefits: simplify the complicated relationship, reduce coupling, cache results and references to remote busines services.

DAO: adapt a uniform interface to access multiple db like relational, unrelational, object oriented etc. Uses and benefits: need to access multiple data sources like legacy systems, B2B, LDAP, and so forth. Lack of uniform APIs to address the requirements to access disparate systems. Persisten storage APIs vary depending on the product verndor. Adapt and encapsulate all access to the DS. hide DS implementation details from its clients. more portable and less code dependencies in components.

Leave a comment