15

What are some of the most common Design Patterns that are used in Web/Enterprise Application and why they are used ?

Note: Answer to why part should be based upon listing the problems which they tend to solve ?

skaffman
  • 398,947
  • 96
  • 818
  • 769
Rachel
  • 100,387
  • 116
  • 269
  • 365
  • I think this is way too broad a question for somebody to compile a list. You might be better of selecting a number of specific, well-known applications and portals, and researching/asking for them specifically. – Pekka Jan 09 '10 at 21:44
  • Wow, this is a BIG question and I am very interested to watch for the answers – AUSteve Jan 09 '10 at 21:46
  • I am looking for generic answer for this questions so as to gain some knowledge about commonly used design patterns in Web/Enterprise Application and why are they used, aiming to understand the issues which they tend to solve. – Rachel Jan 09 '10 at 21:46
  • 3
    I recommend reading Martin Fowler's Patterns of Enterprise Application Architecure http://martinfowler.com/books.html#eaa – blank Jan 09 '10 at 23:22
  • Is this question about patterns in general or specifically design patterns? – Pascal Thivent Jan 09 '10 at 23:33
  • The State Patten, why? because most applications use the session scope extensively to keep track of the user's state between requests and the behavior of the program usually depends on the sessions state. So, if you find yourself repeatedly checking the session scope to check some value before performing an action (like checking if the user is logged in) then the state pattern can abstract out all those states into classes. This removes the hard to read, repetitious conditionals. – kiwicomb123 Mar 17 '17 at 20:03

9 Answers9

5

I use Inversion of Control a lot.

In particular, when persisting / loading objects. It helps when we're not sure if the data is going to come from a database, a web service, or some other mechanism.

By using an interface and allowing multiple sources to expose a simple API for save / retrieving and allowing the object itself to know which API calls to make we end up with a very easy to manage architecture.

One example of this is here.

Community
  • 1
  • 1
NotMe
  • 87,343
  • 27
  • 171
  • 245
  • @Chris: Can you explain your thoughts written in 3rd paragraph, describing working of this design pattern, in detail – Rachel Jan 09 '10 at 21:59
  • Dependency Injection is the more specific and more commonly used form of IOC. – crowne Jan 09 '10 at 22:16
  • I myself prefer using dependency injection as opposed to the singleton pattern where possible. – Corey Ballou Jan 09 '10 at 22:19
  • Dependency injection, combined with a framework such as Google Guice, can also help get rid of singletons -- for instance, to use zombat's example, rather than having a DatabaseManager singleton, you can ensure that Guice always injects the same DatabaseManager instance (or, if you prefer not to use Guice, always use the same DatabaseManager in the top level code) – Michael Williamson Jan 09 '10 at 22:20
2

The Singleton pattern is extremely common. It's primary use is to make sure you never instantiate more than a single object of a given type, which makes it a nice replacement for global variables, which have a decidedly evil reputation. There are varying degrees of arguments for and against the Singleton, with some people claiming that it is just as bad as global variables.

I tend to use it myself with a broad group of objects I generally call "Managers". For instance, in a large application that requires several databases, you don't want to be opening a lot of connections all the time. I will have a DatabaseManager class that is a Singleton, and it will internally manage connections to each database. A consuming object can call upon a DatabaseManager::getConnection() method, and it's the manager's job to ensure a single connection exists (opening it if it has to), and return it to the consuming object.

This solves the problem of passing around a global database connection all over the place, with the side benefit of efficient object use, as only one DatabaseManager is ever in existence. Static calls means that it is available to any consumer that needs it.

zombat
  • 92,731
  • 24
  • 156
  • 164
  • +1 for laying out the guideline for the kind of answers for this questions. Thanks for the input. Very informative answer. – Rachel Jan 09 '10 at 21:55
  • 1
    A singleton is just a global by another name. It still comes with the same set of problems. – Richard Levasseur Jan 09 '10 at 22:24
  • Singletons aren't always the best options, and are often considered harmful -- harder to test, harder to refactor, extra bolierplate code, etc. Steve Yegge has a particularly poignant article about it here: http://steve.yegge.googlepages.com/singleton-considered-stupid – Kaleb Brasee Jan 09 '10 at 22:28
  • 1
    And so the debate begins! :D @Kaleb - that article is pretty long on rhetoric, and pretty short on example. If your Singleton class doesn't do anything except have a single, static method, then yes, you're just using a glorified global. But if you have a class that actually has significant logic involved that can be internally managed while providing a simple external interface, then the Singleton makes more sense. – zombat Jan 09 '10 at 22:49
2

MVC

Model-View-Controller allows for low cohesion between business logic and presentation layer and this is its primary value.

Usually each Controller is a Servlet that handles GET/POST requests for a single page, responding to them by presenting a correct view or transferring jurisdiction to another Controller.

Viwer turns data the Controller passes, into Html, Xml, JavaScript, JSON or whatever technology you'd like. Viewer is most often a Servlet or a Servlet abstraction like JSP, ASP, etc.

Model is the domain-specific representation of the data upon which the application operates. It can also be coupled with domain logic that provides meaning to data (like calculating birthday, totals or shipping charges for cart shopping items). Model should encapsulate the data allowing easy access no matter the underlying storage facilities.

Due to its low cohesion with MVC you can change, develop and test each component independently.

ActiveRecord

This one is often used when underlying storage mechanism is a database. Basically what ActiveRecord means is that all your object properties correspond to columns in the underlying database and that each object includes functions such as Insert, Update, Delete (and Load).

So each class is translated into a table or a view and each object becomes a row in the said table.

The reason for this is simple by having your classes implement the way to access and edit the database you are spared of writing the extra boiler plate code. That coupled with popularity of databases is enough to keep this pattern interesting.

Pools

Another one often used is Pools. PoolManager is a Singleton that manages the Resource (be it database, Factory method or a connection). PoolManager keeps a set of initialized copies. Whenever another process or an object asks for resource via PoolManager.acquire() he gets one of the objects from a pool.
He then manipulates his copy of the resource and returns it when he is finished via Resource.release(). The object isn't destroyed however, it is merely returned to the pool.

Pools are used to increase performance.
For instance if there is a factory method that has a costly retrieval (i.e. it is slow to respond) it is often wrapped in a PoolManager and N instances are created on initialization of PoolManager. That way clients doesn't feel that that the underlying factory is slow since PoolManager takes the performance hit for them.

Daniel Fath
  • 16,453
  • 7
  • 47
  • 82
1

I think Facade & Adapter pattern are widely used by developpers but they don't know they actually do.

iChaib
  • 469
  • 4
  • 10
  • 17
  • Can you provide examples explaining what scenarios are dealt with using Facade and Adapter design patterns. – Rachel Jan 09 '10 at 23:04
1

One of the commonly used Enterprise design pattern is Front Controller. It requires centralized access point or entry point. Used by J2EE frameworks like struts, jersey etc, so developers might not notice it.

rai.skumar
  • 10,309
  • 6
  • 39
  • 55
0

Model View Controller, used to separate business logic from presentation layer to reduce unnecessary tight coupling.
See more on c2.com or wikipedia

crowne
  • 8,456
  • 3
  • 35
  • 50
0

Factory patterns, mostly factory_object and factory_method are very common,
e.g. DocumentFactory for xml docs.
The purpose of the factory pattern is to simplify object creation.

crowne
  • 8,456
  • 3
  • 35
  • 50
0

I don't know where to start as you'll find patterns everywhere (eventually, under the hood). But let's try:

  • I guess that most (all?) MVC frameworks use Front Controller and Command [GoF] patterns.
  • In distributed applications, it is/was very common to use the Session Facade (implemented with Session Beans in Java) which is based on the Facade [GoF] design pattern.
  • ORMs implement the Unit Of Work [PoEAA] pattern (Hibernate's Session, Toplink's UnitOfWork, JDO's PersistenceManager in the Java world).
  • It is still very frequent to use the Data Access Object pattern for the Data Access Layer. Abstract Factory [GoF] and the Factory Method [GoF] are related patterns.
  • etc, etc
Pascal Thivent
  • 562,542
  • 136
  • 1,062
  • 1,124
0

I found this usefull: http://misko.hevery.com/code-reviewers-guide/ it's not about the question you ask but it address some of the design patterns listed above. I strongly recommend to read the pdf book!

Hope this help

Plamen Paskov
  • 144
  • 3
  • 10