Sunteți pe pagina 1din 11

Abstract Factory: Provides one level of interface higher than the factory pattern.

It is used to return one of several factories. Where to use & 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. Try to enforce a constraint. An alternative to Facade to hide platform-specific classes Easily extensible to a system or a family Related patterns include o Factory method, which is often implemented with an abstract factory. o Singleton, which is often implemented with an abstract factory. o Prototype, which is often implemented with an abstract factory. o faade, which is often used with an abstract factory by providing an interface for creating implementing class. Builder: Construct a complex object from simple objects step by step. Where to use & benefits: Make a complex object by specifying only its type and content. The built object is shielded from the details of its construction. Want to decouple the process of building a complex object from the parts that make up the object. Isolate code for construction and representation. Give you finer control over the construction process. Related patterns include o Abstract Factory, which focuses on the layer over the factory pattern (may be simple or complex), whereas a builder pattern focuses on building a complex object from other simple objects. o Composite, which is often used to build a complex object. Factory Method: Provides an abstraction or an interface and lets subclass or implementing classes decide which class or method should be instantiated or called, based on the conditions or parameters given. Where to use & benefits: Connect parallel class hierarchies. A class wants its subclasses to specify the object. A class cannot anticipate its subclasses, which must be created. A family of objects needs to be separated by using shared interface. The code needs to deal with interface, not implemented classes. Hide concrete classes from the client. Factory methods can be parameterized. The returned object may be either abstract or concrete object. Providing hooks for subclasses is more flexible than creating objects directly.

Follow naming conventions to help other developers to recognize the code structure. Related patterns include o Abstract Factory , which is a layer higher than a factory method. o Template method, which defines a skeleton of an algorithm to defer some steps to subclasses or avoid subclasses o Prototype, which creates a new object by copying an instance, so it reduces subclasses. o Singleton, which makes a returned factory method unique. Prototype: Cloning an object by reducing the cost of creation. Where to use & benefits: When there are many subclasses that differ only in the kind of objects, A system needs independent of how its objects are created, composed, and represented. Dynamic binding or loading a method. Use one instance to finish job just by changing its state or parameters. Add and remove objects at runtime. Specify new objects by changing its structure. Configure an application with classes dynamically. Related patterns include o Abstract Factory, which is often used together with prototype. An abstract factory may store some prototypes for cloning and returning objects. o Composite, which is often used with prototypes to make a part-whole relationship. o Decorator, which is used to add additional functionality to the prototype. Singleton: One instance of a class or one value accessible globally in an application. Where to use & benefits: Ensure unique instance by defining class final to prevent cloning. May be extensible by the subclass by defining subclass final. Make a method or a variable public or/and static. Access to the instance by the way you provided. Well control the instantiation of a class. Define one value shared by all instances by making it static. Related patterns include o Abstract factory, which is often used to return unique objects. o Builder, which is used to construct a complex object, whereas a singleton is used to create a globally accessible object. o Prototype, which is used to copy an object, or create an object from its prototype, whereas a singleton is used to ensure that only one prototype is guaranteed. Adapter: Convert the existing interfaces to a new interface to achieve compatibility and reusability of the unrelated classes in one application. Also known as Wrapper pattern. Where to use & benefits:

Try to match an interface(WindowAdapter, etc.) Make unrelated classes work together. Multiple compatibility. Increase transparency of classes. Make a pluggable kit. Delegate objects. Highly class reusable. Achieve the goal by inheritance or by composition Related patterns include o Proxy, which provides the same interface as its subject, whereas an adapter provides a different interface to the object it adapts. o Decorator, which focuses on adding new functions to an object, whereas an adapter coordinates two different objects. o Bridge, which tries to separate an interface from its implementation and make an object vary independently, whereas an adapter tries to change and cooperate the interface of an object. Bridge: Decouple an abstraction or interface from its implementation so that the two can vary independently. Where to use & benefits: Want to separate abstraction and implementation permanently Share an implementation among multiple objects Want to improve extensibility Hide implementation details from clients Related patterns include o Abstract Factory, which can be used to create and configure a particular bridge. o Adapter, which makes unrelated classes work together, whereas a bridge makes a clear-cut between abstraction and implementation. Composite: Build a complex object out of elemental objects and itself like a tree structure. Where to use & benefits: Want to represent a part-whole relationship like tree folder system Group components to form larger components, which in turn can be grouped to form still larger components. Related patterns include o Decorator, which is often used with composite pattern and with the same parent class. o Flyweight, which is used with composite pattern to share components. o Iterator, which is used to traverse the composites. o Visitor, which localizes operations across composite and leaf classes. Decorator: Attach additional responsibilities or functions to an object dynamically or statically. Also known as Wrapper. Where to use & benefits:

Provide an alternative to subclassing. Add new function to an object without affecting other objects. Make a responsibility easily added and removed dynamically. More flexibility than static inheritance. Transparent to the object. Related patterns include o Adapter pattern, which provides a different interface to the object it adapts, whereas a decorator changes an object's responsibilities, o Proxy pattern, which controls access to the object, whereas a decorator focuses on adding new functions to an object, o Composite pattern, which aggregates an object, whereas a decorator adds additional responsibilities to an object, and o Strategy pattern, which changes the guts of an object, whereas a decorator changes the skin of an object. o Facade pattern, which provides a way of hiding a complex class, whereas a decorator adds function by wrapping a class. Faade: Make a complex system simpler by providing a unified or general interface, which is a higher layer to these subsystems. Where to use & benefits: Want to reduce complexities of a system. Decouple subsystems , reduce its dependency, and improve portability. Make an entry point to your subsystems. Minimize the communication and dependency between subsystems. Security and performance consideration. Shield clients from subsystem components. Simplify generosity to specification. Related patterns include o Abstract Factory, which is often used to create an interface for a subsystem in an independent way, and can be used as an alternative way to a facade. o Singleton, which is often used with a facade. o Mediator, which is similar to facade, but a facade doesn't define new functionality to the subsystem. Proxy: Use a simple object to represent a complex one or provide a placeholder for another object to control access to it. Where to use & benefits: If creating an object is too expensive in time or memory. Postpone the creation until you need the actual object. Load a large image (time consuming). Load a remote object over network during peak periods. Access right is required to a complex system. Related patterns include

o Adapter pattern, which provides a different interface to the object it adapts, whereas a proxy provides the same interface as its subject, and o Decorator pattern, which focuses on adding new functions to an object, whereas a proxy controls access to the object. Chain of Responsibility: Let more than one object handle a request without their knowing each other. Pass the request to chained objects until it has been handled. Where to use & benefits: One request should be handled by more than one object. Don't know which object should handle a request, probably more than one object will handle it automatically. Reduce coupling. Flexible in handling a request. Related patterns include o Composite, which a chain of responsibility pattern is often applied in conjunction with. Command: Streamlize objects by providing an interface to encapsulate a request and make the interface implemented by subclasses in order to parameterize the clients. Where to use & benefits: One action can be represented in many ways, like drop-down menu, buttons and popup menu. Need a callback function, i.e., register it somewhere to be called later. Specify and execute the request at different time Need to undo an action by storing its states for later retrieving. Decouple the object with its trigger Easily to be extensible by not touching the old structure. Related patterns include o Composite, which aggregates an object. You may combine it into a composite command pattern. In general, a composite command is an instance of the composite. o Memento, which keeps state of an object. Command supports undo and redo. Interpreter: Provides a definition of a macro language or syntax and parsing into objects in a program. Where to use & benefits: Need your own parser generator. Translate a specific expression. Handle a tree-related information. Related patterns include o Composite, which is an instance in an interpreter. o Flyweight, which shows how to share symbols with abstract context. o Iterator, which is used to traverse the tree structure. o Visitor, which is used to maintain behavior of each note in tree structure. Iterator: Provide a way to move through a list of collection or aggregated objects without knowing its internal representations.

Where to use & benefits: Use a standard interface to represent data objects. Use s standard iterator built in each standard collection, like List, Sort, or Map. Need to distinguish variations in the traversal of an aggregate. Similar to Enumeration class, but more effective. Need to filter out some info from an aggregated collection. Related patterns include o Composite, which supports recursive structures, whereas an iterator is often applied on it. o Factory Method, which provides appropriate instances needed by an iterator subclasses. o Memento, which is often used with an Iterator. The iterator stores the memento internally. Mediator: Define an object that encapsulates details and other objects interact with such object. The relationships are loosely decoupled. Where to use & benefits: Partition a system into pieces or small objects. Centralize control to manipulate participating objects(a.k.a colleagues) Clarify the complex relationship by providing a board committee. Limit subclasses. Improve objects reusabilities. Simplify object protocols. The relationship between the control class and other participating classes is multidirectional. Related patterns include o Facade, which abstracts a subsystem to provide a more convenient interface, and its protocol is unidirectional, whereas a mediator enables cooperative behavior and its protocol is multidirectional. o Command, which is used to coordinate functionality. o Observer, which is used in mediator pattern to enhance communication. Memento: To record an object internal state without violating encapsulation and reclaim it later without knowledge of the original object. Where to use & benefits: Let some info in an object to be available by another object by using default access control. Save some info for later uses. Need undo/redo features. Used in database transaction. Related patterns include o Command, which supports undo or redo features, whereas a memento keeps state of an object.

o Iterator, which provides a way to access the elements of an aggregate object sequentially without exposing its underlying representation, whereas a memento can be used for iteration. Observer: One object changes state, all of its dependents are updated automatically. Where to use & benefits: One change affects one or many objects. Many object behavior depends on one object state. Need broadcast communication. AKA Publish-Subscribe. Maintain consistency between objects keep classes from becoming too tightly coupled, which would hamper reusability. Related patterns include o Singleton, which is used to make observable object unique and accessible globally. o Mediator, which is used to encapsulate updated objects. State: An object's behavior change is represented by its member classes, which share the same super class. Where to use & benefits: Need to control many states without using if-else or switch statements. Use a class to represent a state, not a constant or something else. Every state has to act in a similar manner. Every state must be a subclass of the same super class. Simplify and clarify the program. Related patterns include o Flyweight, which explains when and how a state object can be shared. o Singleton which is often used with state pattern to ensure some state change is shared with class itself, not instances of the class. Strategy: Group several algorithms in a single module to provide alternatives. Also known as policy. Where to use & benefits: Encapsulate various algorithms to do more or less the same thing. Need one of several algorithms dynamically. The algorithms are exchangeable and vary independently Configure a class with one of many related classes (behaviors). Avoid exposing complex and algorithm-specific structures. Data is transparent to the clients. Reduce multiple conditional statements. Provide an alternative to subclassing. Related patterns include o State, which can activate several states, whereas a strategy can only activate one of the algorithms. o Flyweight, which provides a shared object that can be used in multiple contexts simultaneously, whereas a strategy focuses on one context.

o Decorator, which changes the skin of an object, whereas a strategy changes the guts of an object. o Composite, which is used to combine with a strategy to improve efficiency. Template Method: Provide an abstract definition for a method or a class and redefine its behavior later or on the fly without changing its structure. Where to use & benefits: To make many similar operations template. From many specialized operations to a generalized operation. Refactor common behavior to simplify code. Algorithm related improvement. Need good coding skill to conquer it. May be hard to read for novice. Easy to produce ambiguity if not written well. Related patterns include o Factory Method, which is combined with template method. o Strategy, which is used to delegate or coordinate the template method. Visitor: Define a new operation to deal with the classes of the elements without changing their structures. Where to use & benefits: Add operations on a bunch of classes which have different interfaces. Traverse the object structure to gather related operations Easy to add new operations. Crossing class hierarchies may break encapsulation. Related patterns include o Composite, which may be applied in a visitor pattern. o Interpreter, which may be used to go through structure and define new operation in a visitor pattern. MVC: The Model/View/Controller(MVC) is an architecture design pattern. Model means data, View means representation and Controller works on data and representation. MVC focuses on decouple the triad relationships among data, representation and controller. Where to use & benefits: Application architecture design. Any data related design, including non-visual application. Decouple complex object to improve maintainability. Increase object reusability. Achieve design flexibility. Related patterns include o Almost all patterns can be used with MVC. Business Delegate: An intermediate class decouples between presentation-tier clients and business services. Where to use & benefits:

Simplify the complicated relationship. Reduce coupling. Cache results and references to remote business services. Cut potentially costly round trips Hide the underlying implementation details of business service. Related patterns include o Proxy combined to simplify the complexity. Composite Entity: Use a coarse-grained interface to manage interactions between finegrained or coarse-grained and dependent objects internally. The Composite Entity is a coarse-grained entity bean. It may be the coarse-grained object or hold a reference to the coarse-grained object. Also known as Aggregate Entity. Where to use & benefits: Combine coarse-grained object and its related dependent objects into a single entity bean. Multiple clients share persistent objects. Model a network of related business entities. In both local and distributed environment, use remote entity beans to model dependent business objects or fine-grained objects. Improve performance by eliminating the parameter and return value serialization and data transmission costs. Eliminate inter-entity relationships Improve manageability by reducing entity beans. Improve network performance Reduce database schema dependency Increase object granularity Facilitate composite transfer object creation. Overhead of multi-level dependent object graphs. Related patterns include o Transfer Object used to return to client and also used to serialize the coarse-grained and dependent objects tree, or part of the tree, as required. o Session Facade used to manage the inter-entity-bean relationships. Data Access Object: Adapt a uniform interface to access multiple databases like relational, unrelational, object-oriented, etc. Where to use & 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. Persistent storage APIs vary depending on the product vendor. Adapt and encapsulate all access to the data source. Hide the data source implementation details from its clients. More portable and less code dependencies in components. Solve differences in the APIs which is used to access different persistent storage mechanisms.

Not useful for container-managed persistence. Related patterns include o factory method -- used to deal with different data sources. o abstract factory -- used to make an abstract layer of access to data sources. o transfer object -- used to transport data to and from its clients. Front Controller: Using a single component to process application requests. Where to use & benefits: JSP or Servlet. Design request handling component. Channel all requests through a single location. Centralize request processing and view selection. Reduce business logic in a view Improve manageability of security Promote code reuse across requests Avoid code duplication Related patterns include o Decorator use doFilter to decorate or modify one or more requests. o Command combined with multiple requests. o Intercepting Filter both centralize control of certain types of request processing. o Page Controller -- an alternative way. Intercepting Filter: A pluggable component design to intercept incoming requests and outgoing responses, provide common services in a standard manner (independently) without changing core processing code. Where to use & benefits: Logging and authentication. Enhance security. Add additional function to existing web application. Decorate main process. Debug. Pre-processing or post-processing for specific clients. Uncompress incoming request. Convert input encoding schema. Being added or removed transparently or declaratively and triggered automatically Improve reusability Deployment-time composability Each filter is loosely coupled Inefficient for information sharing. Related patterns include o Front Control better suited to handling core processing. o Template good for template filter strategy

o Decorator providing for dynamically pluggable wrappers. Service Locator: Centralizing distributed service object lookups, providing a centralized point of control, acting as a cache that eliminates redundant lookups. Where to use & benefits: Lookup object in JNDI, RMI, JMS, etc. Encapsulate any vendor-specific features of lookup process Simplify the lookup process Improve the performance Related patterns include o Singleton combined with service locator to make sure only one lookup object exists. Transfer Object: Using a serializable class to act as data carrier, grouping related attributes, forming a composite value and working as a return type from remote business method. Also known as Value object. Where to use & benefits: Get related values from remote business method. Fetch multiple values in one trip. Decrease network traffic. Minimize latency and server resource usage. Related patterns include o Composite view

S-ar putea să vă placă și