Encapsulation
Encapsulation conceals the functional details of a class from objects that send messages to it.
Intro
| Encapsulation | Encapsulation conceals the functional details of a class from objects that send messages to it. |
| Law of Demeter | A method should only use: - Instance fields
- Method Parameters
- Objects constructed with new
|
| 5 C's of Class Design | - Cohesion
Class is a single concept. Operations fit together to support that concept - Completeness
Class should support all operations of the concept - Convenience
Interface might be complete, but must be convenient - Clarity
Interface of a class should not be confusing. - Consistency
Operations should be consistent with respect to names, parameters, return values and behavior. |
Liskov Principle | You can use a subclass object whenever a superclass object is expected. |
Patterns
Prototype

The Prototype pattern teaches how a system can instantiate classes that are not known when the system is built.
Context | Solution |
- A system needs to create several kinds of objects whose classes are not known when the system is built.
- You do not want to require a seperate class for each kind of object.
- You want to avoid a separate hierarchy of classes whose responsibility it is to create the objects.
| - Define a prototype interface that is common to all created objects.
- Supply a prototype object for each kind of object that the system creates.
- Clone the prototype object whenever a new object of the given kind is required.
|
Iterator
.png?size=webview)
The iterator pattern teaches how to acces elements of an aggregate object.
Context | Solution |
- An oject (called aggregate) contains other objects (called elements)
- Clients (methods using aggregate) need acces to elements.
- The aggregate should not expose the internal structure
- There may be multiple clients that need simultaneous acces.
| - Define an iterator class that fetches one element at a time.
- Each iterator object needs to keep track of the position of the next element to fetch.
- If there are several variation of the aggregate and the iterator classes, it is best if they implement common interface types.
Then the client only needs to know the interface types, not the concrete classes. |
Observer

The Observer pattern teaches how an object can tell other objects about events.
Context | Solution |
- An object (called subject) is the source of events.
- One or more objects (called observers) want to know when an event occurs.
| - Define an observer interface type. Observer classes must implement this interface type.
- The subject maintains a collection of observer objects
- The subject class supplies methods for attaching observers.
- Whenever an event occurs, the subject nofifies all observers.
|
Strategy

The Strategy pattern teaches how to supply variants of an algorithm.
Context | Solution |
- A Class (called context) can benefit from different variations of an algorithm.
- Clients of the context class sometimes want to supply custom versions of the algorithm
| - Define an interface type that is an abstraction for the algorithm. We'll call this interface type the strategy.
- Concrete strategy classes implement the strategy interface type. Each strategy class implements a version of the algorithm.
- The clients supplies a concrete strategy object to the context class.
- Whenever the algorithm needs to be executed, the context class calss the appropriate methods of the strategy object.
|
Composite

The Composite pattern teaches how to combine several objects into an object that has the same behavior as its parts.
Context | Solution |
- Primitive objects can be combined into composite objects.
- Clients treat a composite object as a primitive object
| - Define an interface type that is an abstraction for the primitive objects.
- A composite object contains a collection of primitive objects.
- Both primitive classes and composite classes implement that interface type.
|
Decorator

The Decorator pattern teaches how to form a class that adds functionality to another class while keeping its interface.
A decorator enhances the behavior of a single component, whereas a composite collects multiple components.
Context | Solution |
- You want to enhance the behavior of a class (called component class).
- A decorated component can be used in the same way as a plain component.
- The component class does not want to take on the responsbility of the decoration.
- There may be an open-ended set of possible decorations.
| - Define an interface type that is an abstraction for the component.
- Concrete component classes implement this interface type.
- Decorator classes also implement this interface type.
- A decorator object manages the component object that it decorates.
- When implementing a method from a component interface type, the decorator class applies the method to the decorated component and combines the result with the effect of the decoration.
|
Template Method

The Template Method pattern teaches how to supply an algorithm for multiple types, provided that the sequence of steps does not depend on the type.
Context | Solution |
- An algorithm is applicable for multiple types.
- The algorithm can be broken down into primitive operations.
- The primitive operations can be different for each type.
- The order of the primitive operations does not depend on the type.
| - Define superclass wit:
- one method for the algorithm
- abstract methods for the primitive operations.
- Implement the algorithm to call the primitive operations in the appropriate order.
- Do not define the primitive operations in the superclass, or define them to have appropriate default behavior.
- Each subclass defines the primitive operations (but not the algorithm).
|
Adapter

The Adapter pattern teaches how to use a class in a context that requires a different interface.
Context | Solution |
- You want to use an existing class without modifying it. Class adaptee
- The context in which you want to use the class requires conformance to a target interface that is different from that of the adaptee.
- The target interface and the adaptee interface are conceptually related.
| - Define an adapter class that implements the target interface
- The adapter class holds a reference to the adaptee. It translates target methods to adaptee methods.
- The client wraps the adaptee into an adapter class object.
|
Command

The Command pattern teaches how to implement commands as objects whenever a command has both behavior and state.
Context | Solution |
- You want to implement commands that behave like objects, either because you need to store additional information with commands, or becahse you want to collect commands.
| - Define a command interface type with a method to execute the command
- Supply methods in the command interface type to manipulate the state of command objects.
- Each concrete command class implements the command interface type.
- To invoke the command, call the execute method.
|
Factory Method

The Factory Method pattern teaches how to supply a method that can be overridden to create objects of varying types.
Context | Solution |
- A type (creator) creates objects of another type (product),
- Subclasses of the creator type need to create different kinds of product objects.
- Clients do not need to know the exact type of product objects.
| - Define a creator type that expresses the commonality of all creators.
- Define a product type that expresses the commonalityof all products.
- Define a method, called the factory method, in the creator type. The factory method yields a product object.
- Each concrete creator class implements the factory method so that it returns an object of a concrete product class.
|
Proxy

The Proxy pattern teaches how an object can be a placeholder for another object,
Context | Solution |
- A class (real subject) provides a service that is specified by an interface type (subject) type.
- There is a need to modify the service in order to make it more versatile
- Neither the client nor the real subject should be affected by the modification.
| - Define a proxy class that implements the subject interface type. The proxy holds a reference to the real subject, or otherwise knows how to locate it.
- The client uses a proxy object.
- Each proxy method invokes the same method on the real subject and provides the necessary modifications.
|
Singleton
A singleton class has exactly one instance. The pattern teaches how to implement a class that has exactly one instance.
(Has a private constructor, and a static method to get the instance of the class.)
Context | Solution |
- All clients need to acces a single shared instance of a class.
- You want to ensure that no additional instances can be created accidentally.
| - Define a class with a private constructor.
- The class constructs a single instance of itself.
- Supply a static method that returns a reference to the single instance.
|
Visitor

The Visitor pattern teaches how to support an open-ended set of operations on an object structure with a fixed set of element types.
Context | Solution |
- An object structure contains element classes of multiple types, and you want to carry out operations that depend on the object types.
- The set of operations should be extensible over time.
- The set of element classes is fixed.
| - Define a visitor interface type that has methods for visiting elements of each of the given types.
- Each element class defines an accept method that invokes the matching element visitation method on the visitor parameter.
- To implement an operation, define a class that implements the visitor interface type and supplies the operation's action for each element type.
|
Comments