Object Oriented Software Engineering   View all facts   Glossary   Help
subject > pattern > design pattern
Next patternmultiplicity pattern    Uppattern    Previous patternarchitectural pattern   

design pattern comparison table
Subject is part of is a kind of is a subtopic of have example have antipatterns is an instance of have problem have context use has definition have related patterns have solution have forces have references
abstraction-occurrence  6.2 - The Abstraction-Occurrence Pattern design patternWhat is the best way to represent sets of occurrences in a class diagram?class diagrams that form part of a system domain model A pattern in which two classes are related by an association, and one of the classes represents an abstraction of the otherAbstraction-Occurrence Square pattern if the abstraction is an aggregate (and the occurrences are also aggregates)
  1. Create an «Abstraction» class that contains the data that is common to all the members of a set of occurrences.
  2. Then create an «Occurrence» class representing the occurrences of this abstraction.
  3. Connect these classes with a one-to-many association
  • You want to represent the members of each set of occurrences without duplicating the common information
  • You want a solution that maximizes the flexibility of the system
a generalization of the Title-Item pattern of Eriksson and Penker
adapter  6.8 - The Adapter Pattern zero or more antipatterns - solutions that are inferior or do not work in this context with the reason for their rejectiondesign patternHow do you obtain the power of polymorphism when reusing a class whose methods have the same function but do not have the same signature as the other methods in the hierarchy? A pattern found in class diagrams in which you are able to reuse an 'adaptee' class by providing a class, (the adapter) that delegates to the adapteefacade, read-only interface, proxy
  1. Rather than directly incorporating the reused class into your inheritance hierarchy, instead incorporate an «Adapter» class.
  2. The «Adapter»is connected by an association to the reused class, which we will call the «Adaptee».
  3. The polymorphic methods of the «Adapter» delegate to methods of the «Adaptee».
  4. The delegate method in the «Adaptee» may or may not have the same name as the delegating polymorphic method.
You do not have access to multiple inheritance or you do not want to use it.one of the Gang of Four patterns.
antipatterndesign patterndesign pattern6.1 - Introduction to Patterns zero or more antipatterns - solutions that are inferior or do not work in this context with the reason for their rejection a sentence or two explaining the main difficulty being tackleda context A solution to a design problem that is inferior or does not work in this contextzero or more related design patterns one or more forcesone or more references which indicate who developed or inspired a pattern
composite  6.3 - The General Hierarchy Pattern zero or more antipatterns - solutions that are inferior or do not work in this context with the reason for their rejectiondesign patterna sentence or two explaining the main difficulty being tackleda context A specialization of the general hierarchy pattern, that uses an aggregation instead of an ordinary associationzero or more related design patterns one or more forcesone or more references which indicate who developed or inspired a pattern
delegation  6.7 - The Delegation Pattern 
  • Overusing generalization and inheriting the method that is to be reused
  • Instead of creating a single method in the «Delegator» that does nothing other than call a method in the «Delegate», you might consider having many different methods in the «Delegator» call the delegate's method? It would be better to ensure that only one method in the «Delegator» calls the method in the «Delegate». If many methods access the method in the «Delegate», then when it changes, you may have to change every method that accesses it
  • a method must only accesses neighbouring classes
design patternHow can you most effectively make use of a method that already exists in the other class? A pattern in which one procedure does nothing more than call another in a neighbouring class; this reduces total coupling in the systemAdapter and Proxy patterns
  1. Create a method in the «Delegator» class that does only one thing: it calls a method in a neighbouring «Delegate» class, allowing reuse of the method for which the «Delegate» has responsibility
  2. By 'neighbouring', we mean the «Delegate» is connected to the «Delegator» by an association.
  3. Normally, in order to use delegation an association should already exist between the «Delegator» and the «Delegate».
  4. This association needs only to be unidirectional from «Delegator» to «Delegate».
  5. However it may sometimes be appropriate to create a new association just so you can use delegation - provided this does not increase the overall complexity of the system.
  • You want to minimize development cost and complexity by reusing methods
  • You want to reduce the linkages between classes
  • You want to ensure that work is done in the most appropriate class
one or more references which indicate who developed or inspired a pattern
facade  6.9 - The Facade Pattern zero or more antipatterns - solutions that are inferior or do not work in this context with the reason for their rejectiondesign patternHow do you simplify the view that programmers have of a complex package?html>
  • Often, an application contains several complex packages.
  • A programmer working with such packages has to manipulate many different classes
 A pattern in which you create a class that provides a simplified interface to a packagezero or more related design patterns
  1. Create a special class, called a «Facade», which will simplify the use of the package.
  2. The «Facade» will contain a simplified set of public methods such that most other subsystems do not need to access the other classes in the package.
  3. The net result is that the package as a whole is easier to use and has a reduced number of dependencies with other packages.
  4. Any change made to the package should only necessitate a redesign of the «Facade»class.
html>
  • It is hard for a programmer to understand and use an entire subsystem - in particular, to determine which methods are public.
  • If several different application classes call methods of the complex package, then any modifications made to the package will necessitate a complete review of all these classes.
one of the Gang of Four patterns.
general hierarchy  6.3 - The General Hierarchy Pattern modelling a hierarchy of categories using a hierarchy of classesdesign patternHow do you draw a class diagram to represent a hierarchy of objects, in which some objects cannot have subordinates?many class diagrams where you often find a set of objects that have a naturally hierarchical relationship. A pattern in which two classes are related both by a generalization and by a one to many association, such that the generated graph of instances forms a hierarchythe Reflexive Association pattern, the Composite pattern (a specialization of the General Hierarchy pattern)
  1. Create an abstract «Node» class to represent the features possessed by each object in the hierarchy.
  2. Then create at least two subclasses of the «Node» class.
  3. One of the subclasses, «SuperiorNode», must be linked by a «subordinates» association to the superclass; whereas at least one subclass, «NonSuperiorNode», must not be.
  4. The subordinates of a «SuperiorNode» can thus be instances of either «SuperiorNode» or «NonSuperiorNode».
  • You want a flexible way of representing the hierarchy that naturally prevents certain objects from having subordinates
  • You also have to account for the fact that all the objects share common properties and operations
one or more references which indicate who developed or inspired a pattern
immutable  6.10 - The Immutable Pattern zero or more antipatterns - solutions that are inferior or do not work in this context with the reason for their rejectiondesign patternHow do you create a class whose instances are immutable? A pattern in which the instances of a class cannot change state after creationRead-Only Interface pattern provides the same capability as immutable, except that certain privileged classes are allowed to make changes to instances.
  1. Ensure that the constructor of the immutable class is the only place where the values of instance variables are set or modified.
  2. If a method that would otherwise modify an instance variable must be present, then it has to return a new instance of the class.
html>
  • The immutability must be enforced.
  • There must be no loopholes that would allow 'illegal' modification of an immutable object.
one or more references which indicate who developed or inspired a pattern
observer  6.6 - The Observer Pattern 
  • connecting an observer directly to an observable so that they both have references to each other
  • making the observers subclasses of the observable
design patternHow do you reduce the interconnection between classes, especially between classes that belong to different modules or subsystems?
  • When you create a two-way association between two classes, the code for the classes becomes inseparable
  • When you compile one, the other one has to be available since the first one explicitly refers to it
  • This means if you want to reuse one of the classes, you also have to reuse the other; similarly, if you change one, you probably have to change the other
to separate the model^2 from the controller in the Model-View-Controller architectureA pattern found in class diagrams in which instances of one class are informed of changes to instances of a second classzero or more related design patterns
  1. Create an abstract class «Observable» that maintains a collection of «Observer» instances.
  2. The «Observable» class is very simple; it merely has a mechanism to add and remove observers, as well as a method notifyObservers that sends an update message to each «Observer».
  3. Any application classes can declare itself to be a subclass of «Observable».
  4. «Observer» is an interface, defining only an abstract update method.
  5. Any class can thus be made to observe an Observable by declaring that it implements the interface, and asking to be a member of the observer list of the «Observable».
  6. The «Observer» can then expect calls to its update method whenever the «Observable» changes.
  7. Using this pattern, the «Observable» neither has to know the nature of the classes that will be interested in receiving the update messages, nor what they will do with this information.
  • You want to maximize the flexibility of the system to the greatest extent possible.
one of the Gang of Four patterns.
player-role  6.4 - The Player-Role Pattern merging all the properties and behaviours into the Player class which however, creates an overly complex class - much of the power of object orientation is lostdesign patternHow do you best model players and roles so that a player can change roles or possess multiple roles?
  • A role is a particular set of properties associated with an object in a particular context.
  • An object may play different roles in different contexts.
 A pattern in found in class diagrams in which one class (the player) has several associated role classes. Instances of the role classes can change over the lifetime of a playerAbstraction-Occurrence pattern
  1. Create a «Player» class to represent the object that plays different roles.
  2. Create an association from this class to an abstract «Role» class, which is the superclass of a set of possible roles.
  3. The subclasses of this «Role» class encapsulate all the properties and behaviours associated with the different roles.
  4. If the «Player» can only play one role at a time, the multiplicity between «Player» and «Role» can be one-to-one, otherwise it will be one-to-many.
one or more references which indicate who developed or inspired a pattern
proxy  6.12 - The Proxy Pattern Instead of using proxy objects, beginner designers often scatter complex code around their application to load objects from databases. A strategy that only works for very small systems is to load the whole database into memory when the program starts.design patternHow can you reduce the need to create instances of a heavyweight class? In particular, how can you reduce the need to load large numbers of them from a database or server, when not all of them will be needed? A related problem is this: If you load one object from a database or server, how can you avoid loading all the other objects that are linked to it.
  • There may be a time-delay and a complex mechanism involved in creating instances of heavyweight classes.
 A pattern found in which a lightweight object stands in place of a heavyweight object that has the same interface. It transparently loads the heavyweight object when neededseveral patterns that obtain their power from delegating responsibilities to other classes, hence it uses the Delegation pattern
  1. Create a simpler version of the «HeavyWeight» class.
  2. We will call this simpler version a «Proxy».
  3. The «Proxy» has the same interface as the «HeavyWeight», so programmers can declare variables without caring whether a «Proxy» or its «HeavyWeight» version will be put in the variable.
  • You want all the objects in a domain model to be available for programs to use when they execute a system's various responsibilities
  • It is important for many objects to persist from run to run of the same program
  • in a large system it would be impractical for all the objects to be loaded into memory whenever a program starts
  • It would be ideal to be able to program the application as if all the objects were located in memory
one of the Gang of Four patterns
read-only interface  6.11 - The Read-Only Interface Pattern making the read only class a subclass of the «Mutable» class, overriding all methods that modify properties, such that they throw an exceptiondesign patternHow do you create a situation where some classes see a class as read-only (i.e. the class is immutable) whereas others are able to make modifications?
  • You sometimes want certain privileged classes to be able to modify attributes of objects that are otherwise immutable.
 A pattern in which an interface is used to restrict which classes have privileges to call update methods of a classzero or more related design patterns
  1. Create a «Mutable» class as you would create any other class, except make sure it is not public; this ensures that it can be accessed only from its package.
  2. All classes that need to modify the class, often called «Mutator» classes, must be put in this package.
  3. Then create a public interface we will call the «ReadOnlyInterface», that has only the read-only operations of «Mutable» - i.e. only operations that get its values.
  4. The «Mutable» class implements the «ReadOnlyInterface».
one or more references which indicate who developed or inspired a pattern
related patterndesign patterndesign pattern6.1 - Introduction to Patterns zero or more antipatterns - solutions that are inferior or do not work in this context with the reason for their rejection a sentence or two explaining the main difficulty being tackleda context A pattern that are similar to another patternzero or more related design patterns one or more forcesone or more references which indicate who developed or inspired a pattern
singleton  6.5 - The Singleton Patternthe Company or University classes in systems that run the business of that company or universityzero or more antipatterns - solutions that are inferior or do not work in this context with the reason for their rejectiondesign patternHow do you ensure that it is never possible to create more than one instance of a singleton class? A pattern that ensures that a certain class can have only one instancezero or more related design patternsIn a singleton class, create the following:one of the Gang of Four patterns.

Next patternmultiplicity pattern    Uppattern    Previous patternarchitectural pattern