background preloader

Abstract Factory Pattern

Facebook Twitter

Collection of webpages conveying the idea of Abstract Factory Pattern

Abstract Factory Pattern. Motivation Modularization is a big issue in today's programming. Programmers all over the world are trying to avoid the idea of adding code to existing classes in order to make them support encapsulating more general information. Take the case of a information manager which manages phone number. Phone numbers have a particular rule on which they get generated depending on areas and countries. If at some point the application should be changed in order to support adding numbers form a new country, the code of the application would have to be changed and it would become more and more complicated. In order to prevent it, the Abstract Factory design pattern is used.

Intent Abstract Factory offers the interface for creating a family of related objects, without explicitly specifying their classes. Implementation The pattern basically works as shown below, in the UML diagram: The classes that participate to the Abstract Factory pattern are: Applicability & Examples Phone Number Example Hot Points: Abstract Factory Design Pattern. Here is my Abstract Factory design pattern tutorial. I also take a second look at the Factory design pattern here as well. This is considered a hard pattern to understand, but I consider it to be a combination of all you have learned previously. You just need to understand that each step is separated by either a abstract class or an interface. This makes it possible to make extremely flexible objects.

All of the code follows the video and is heavily commented. If you like videos like this, it helps if you tell Google by clicking here Sharing is nice If you want the Alien Spaceship diagram, it as at the bottom of the page. Code from the Video EnemyShipTesting.java EnemyShipBuilding.java UFOEnemyShipBuilding.java EnemyShipFactory.java UFOEnemyShipFactory.java UFOBossEnemyShipFactory.java EnemyShip.java UFOEnemyShip.java UFOBossEnemyShip.java ESEngine.java ESWeapon.java ESUFOGun.java ESUFOEngine.java ESUFOBossGun.java ESUFOBossEngine.java Abstract Factory Design Pattern Diagram. Abstract Factory. See AbstractFactoryPattern. Question: Can somebody illustrate how it is advantageous to Create objects using a Factory as against directly creating them?

Check this out: the page ... The dynamic class loading technique provides developers with a great deal of flexibility in their designs. Dynamic class loading is a means of providing extensibility without sacrificing robustness. In this article, we will design a simple application that defines a single class, a shape class we wish to use in a drawing package. Question: that "common method signature" can be a stumbling block.

Suppose these classes are sufficiently similar that you might substitute one for the other at runtime (viz. the call to action()), but not so similar that they can have signature-compatible ctors. Such a solution is more verbose, of course. OK, here's a real-world example. CategoryPatternFactory. Design Patterns---JavaCamp.org. Abstract Factory. Using references to interfaces instead of references to concrete classes is an important way of minimizing ripple effects. The user of an interface reference is always protected from changes to the underlying implementation. The Abstract Factory pattern is one example of this technique. Users of an Abstract Factory can create families of related objects without any knowledge of their concrete classes. (A typical business application would usually not need to use this technique, at least as applied to Data Access Objects.)

Example An Abstract Factory is a major part of the full Data Access Object (DAO) scheme. There are two distinct families of items here: the various datastore implementations (in this case, text files, or a relational database)the various business objects which need persistence (in this case, User and Device) Let's take the example of storing Device objects. ...with an implementation for a file system: ...and an implementation for a relational database: