Find, describe, execute, and discuss 2 examples of each design pattern: ?strategy, observer, decorator, factory, singleton, adapter, and template.
[1] Find, describe, execute, and discuss 2 examples of each design pattern: ?iterator, composite, and state.
[2] Find, describe, execute, and discuss a group chatting program made up of Java RMI.
Summary Lectures (Detail Version)
OO Principles
Identify the aspects of your?application that vary and separate?them from what stays the same.
Favor composition over inheritance.
Program to an interface, not an implementation. -?HAS-A can be better than IS-A
Strive for loosely coupled designs?between objects that interact. -?Loose Coupling Principle
Classes should be open?for extension, but closed for?modification. -?Open-closed Principle
Depend upon abstractions. Do not?depend upon concrete classes. – ?Dependency Inversion Principle
Talk only to your immediate friends. -?Principle of Least Knowledge
Don’t call us, we’ll call you. -?The Hollywood Principle
A class should have only one?reason to change. -?Single Responsibility
Design Patterns
Strategy Pattern
Definition
“The Strategy Pattern defines a family of algorithms, encapsulates each one, and makes them interchangeable. Strategy lets the algorithm vary independently from clients that use it.“
Technique, Tools
super class,interface
Terms
inheritance, composition
Observer Pattern
Definition
“The Observer Pattern defines a one-to-many dependency between objects so that when one object changes state, all of its dependents are notified and updated automatically.“
Technique, Tools
subject interface
registerObserver()
removeObserver()
notifyObservers()
observer interface
update()
Terms
loose coupling(opp. tight coupling)
subject, observer
subscribe/register ↔ unsubscribe/remove
one-to-many relationship
The Decorator Pattern
Definition
“The Decorator Pattern attaches additional responsibilities to an object dynamically. Decorators provide a flexible alternative to subclassing for extending functionality.“
Singleton Pattern
Command Pattern
Adapter and Facade Patterns
Template Method Patterns
Iterator Pattern
Composite Patterns
Definition
The Composite Pattern allows you to compose objects into tree structures to represent part-whole hierarchies. Composite lets clients treat individual objects and compositions of objects uniformly.
Implementation
class Element
fields
List<Element> childElements
add(Element child)
remove(Element child)
getChildElements()
State Pattern
Definition
The State Pattern allows an object to alter its behavior when its internal state changes. The object will appear to change its class.
Implementation
interface State
class ConcreteState implement State
action()
class Context
State state
setState(State state)
this. state = state
action()
state.action()
Proxy Pattern
Definition
The Proxy Pattern provides a surrogate or placeholder for another object to control access to it.
Implementation
class Object
class ObjectProxy
Compound Patterns (MVC Pattern)
Definition
A Compound Pattern combines two or more patterns into a solution that solves a recurring or general problem.
The Model View Controller Pattern (MVC) is a compound pattern consisting of the Observer, Strategy and Composite patterns.
Observer pattern: Views listen messages from Control .
Strategy: Control reacts differently depending Model.
Composite:?Model
Implementation
Model class
Model represent an object carrying data. It can also have methods to update controller if its data changes.
getField()
setField()
View class
View represents the visualization of the data that model contains.
display(Model model)
Controller class
Controller acts on both model and view. It controls the data flow into model object and updates the view whenever data changes. It keeps view and model separate.
Model mode
View view
Bridge Pattern
Definition
The Bridge Pattern allows you to vary the implementation and the abstraction by placing the two in separate class hierarchies.
Implementation
interface Feature (has a) – features
class ConcreteFeature implements Feature
class abstract?AbstractObject?(is a) – species
Feature feature
class ConcreteObject
this.feature = new ConcreteFeature()
Builder Pattern
Definition
The Bridge Pattern allows you to vary the implementation and the abstraction by placing the two in separate class hierarchies.
The Chain of Responsibility Pattern creates a chain of objects that examine a request. Each object in turn examines the request and handles it, or passes it on to the next object in the chain.
Implementation
handler class
Handler?nextHandler
handle(Request request)
check availability to handle or not. If available, handle the request.
If not, check the next handler exists or not.
If the next exists, forward the request.
class Request
Mediator Pattern
Definition
The Mediator Pattern centralizes complex communications and control between related objects.
Implementation
Mediator class
send() method
Requester class
Mediator mediator
sendToMediator() method
Visitor Pattern
Definition
The Visitor Pattern is used when you want to add capabilities to a composite of objects and encapsulation is not important.