In this project we present the well-known OOP design patterns from the GoF Book in Nomnoml
For the Javascript implementation see: js-design-patterns
Images were generated by Nomnoml from the sources. The sequence diagrams were generated by JS Sequence Diagrams.
Everyone is welcome to improve on this and provide implementation in various programming languages.
[<abstract>AbstractFactory
|
| +createProductA()
+createProductB()
]
[AbstractFactory]<:-[ConcreteFactory1
|
| +createProductA()
+createProductB()
]
[AbstractFactory]<:-[ConcreteFactory2
|
| +createProductA()
+createProductB()
]
[AbstractFactory]<-[Client
(from design_patterns)
|
|
]
[Client]->[<abstract>AbstractProductA
|
|
]
[AbstractProductA]<:-[ProductA1
|
|
]
[AbstractProductA]<:-[ProductA2
|
|
]
[<abstract>AbstractProductB
|
|
]
[AbstractProductB]<:-[ProductB1
|
|
]
[AbstractProductB]<:-[ProductB2
|
|
]
[ConcreteFactory1]-->[ProductA1]
[ConcreteFactory1]-->[ProductB1]
[ConcreteFactory2]-->[ProductA2]
[ConcreteFactory2]-->[ProductB2]
[Builder
|
|+buildPart()
]
[Director |
+builder: Builder
|+construct()
+new Director()
]o-[Builder]
[Director]--[<note>this.builder.buildPart();]
[Builder]<:-[ConcreteBuilder
|
|+buildPart()
+getResult():Product
+new ConcreteBuilder()
+buildPartA()
+buildPartB()
+buildPartC()
]
[ConcreteBuilder]--><<create>>[Product]
participant Client
participant Director
Client-->ConcreteBuilder:<<create>>\nnew ConcreteBuilder
Client-->Director:<<create>>\nnew Director(ConcreteBuilder)
Client->Director:construct()
Director->ConcreteBuilder:buldPartA()
Director->ConcreteBuilder:buldPartA()
Director->ConcreteBuilder:buldPartA()
Client->ConcreteBuilder:getResult()
[<abstract>Creator | |+FactoryMethod()
+AnOperation()
]
[<abstract>Product ||]<:-[ConcreteProduct||]
[Creator]<:-[ConcreteCreator||+FactoryMethod()]
[ConcreteCreator]-->[ConcreteProduct]
[Creator]--[<note>product= FactoryMethod()]
[ConcreteCreator]--[<note>return new ConcreteProduct]
[Client
|
|+Operation()
]--[<note>p = prototype->Clone()]
[Client]->+prototype[<abstract>Prototype||+Clone()]
[Prototype]<:-[ConcretePrototype1 ||+Clone()]
[ConcretePrototype1]--[<note>return copy of self]
[Prototype]<:-[ConcretePrototype2 ||+Clone()]
[ConcretePrototype2]--[<note>return copy of self]
[Singleton
|+static uniqueInstance
+singletonData
|static Instance()
+SingletonOperation()
+GetSingletonData()
] -- [<note>return uniqueInstance]
[Client
(from design_patterns)
|
|
]->[Target ||+Request()]
[Target]<:-[Adapter ||+Request()]
[Adapter]-:>[Adaptee ||+SpecificRequest()]
[Adapter]--[<note>SpecificRequest()]
[Client
(from design_patterns)
|
|
]->[<abstract>Abstraction ||+Operation()]
[Abstraction]<:-[RefinedAbstraction ||]
[Abstraction]--[<note>imp->OperationImp()]
[Abstraction]o- +imp[Implementor ||+OperationImp()]
[Implementor]<:-[ConcreteImplementorA ||+OperationImp()]
[Implementor]<:-[ConcreteImplementorB ||+OperationImp()]
[Client
(from design_patterns)
|
|
]->[<abstract>Component
|
|+Operation()
+Add(Component)
+Remove(Component)
+GetChild(int)
]
[Component]<:-[Leaf||+Operation()]
[Component]<:-[Composite|
|+Operation()
+Add(Component)
+Remove(Component)
+GetChild(int)
]
[Composite]<:-[RefinedAbstraction ||]
[Composite]o- +children[Component]
[Composite]--[<note>forall g in children
g.Operation()]
[<abstract>Component|| operation()]
[Client] depends --> [Component]
[Decorator|- next: Component]
[Decorator] decorates -- [ConcreteComponent]
[Component] <:- [Decorator]
[Component] <:- [ConcreteComponent]
[<package>Subsystem Classes |
[Class1]
[Class2]
[Class3]
[Class4]
[Class5]
[Class6]
[Class7]
]
[<abstract>Facade
|
|
]
[Class2]-->[Class1]
[Facade]->[Class2]
[Facade]->[Class3]
[Facade]->[Class4]
[Facade]->[Class6]
[Facade]->[Class7]
[Class5]<:-[Class6]
[Class5]<:-[Class7]
[Class1]<:-[Class3]
[FlyweightFactory |
|+GetFlyweight(key)
]--[<note>if (flyweight.key exists) {
return existing flyweight
} else {
create new flyweight
add it to pool of flyweights
return the new flyweight
}
]
[FlyweightFactory]o- +flyweights [<abstract> Flyweight ||+Operation(extrinsicState)]
[Flyweight]<:-[ConcreteFlyweight]
[FlyweightFactory]<-[Client
(from design_paterns) |
|
]
[Client]->[ConcreteFlyweight|+instrinsicState|+Operation(extrinsicState)]
[Client]->[UnsharedConcreteFlyweight|+allState|+Operation(extrinsicState)]
[Client
(from design_patterns)
|
|
]->[<abstract>Subject
|
|+Request()
]
[Subject]<:-[RealSubject ||+Request()]
[Subject]<:-[Proxy ||+Request()]
[Proxy]-->[RealSubject]
[Proxy]--[<note>realSubject->Request()]
[Client
(from design_patterns)
|
|
]->[<abstract>Handler
|
|+HandleRequest()
]
[Handler]<:-[ConcreteHandler1||+HandleRequest()]
[Handler]<:-[ConcreteHandler2||+HandleRequest()]
[Handler]-> +successor [Handler]
[Client
(from design_patterns)
|
|
]->[<abstract>Receiver
|
|+Action()
]
[Receiver]<- +receiver [ConcreteCommand |+state|+Execute()]
[Invoker ||+StoreCommand()]o-[Command ||+Execute()]
[Command]<:-[ConcreteCommand]
[ConcreteCommand]--[<note>receiver->Action()]
[Client]-->[ConcreteCommand]
participant Receiver
participant Client
participant ConcreteCommand
participant Invoker
Client-->ConcreteCommand: <<create>>\nnew Command(Receiver)
Client->Invoker: StoreCommand(Command)
Invoker->ConcreteCommand: Execute()
ConcreteCommand->Receiver: Action()
[Client
(from design_patterns)
|
|
]->[<abstract>AbstractExpression
|
|+Interpret(Context)
]
[Client]->[Context]
[AbstractExpression]<:-[TerminalExpression ||+Interpret(Context)]
[AbstractExpression]<:-[NonterminalExpression ||+Interpret(Context)]
[AbstractExpression]-o[NonterminalExpression]
[Client
(from desing_patterns)|
|
]->[Agregate]
[Client]->[<abstract>Iterator |
|+First()
+Next()
+IsDone()
+CurrentItem()
]
[Iterator]<:-[ConcreteIterator]
[ConcreteAgregate]--[<note>return new ConcreteIterator(this)]
[ConcreteAgregate]-->[ConcreteIterator]
[<abstract>Agregate |
|+CreateIterator()
]<:-[ConcreteAgregate |
|+CreateIterator()
]
[ConcreteAgregate]<-[ConcreteIterator||]
[<abstract>Mediator||]<:-[ConcreteMediator||]
[ConcreteMediator]->[ConcreteColleague1]
[ConcreteMediator]->[ConcreteColleague2]
[Mediator]<- +mediator[<abstract>Colleague]
[Colleague]<:-[ConcreteColleague1]
[Colleague]<:-[ConcreteColleague2]
[Originator |
+state
|+SetMemento(Mememnto m)
+CreateMemento()
]
[Originator]--[<note>return newMemento(state)]
[Originator]--[<note>state = m->GetState()]
[Originator]-->[Memento |+state
|+GetState()
+SetState()
]
[Memento]-o +memento [Caretaker]
Caretaker->Originator: CreateMemento()
Originator-->Memento: <<create>>\n new Memento
Originator->Memento: SetState()
Caretaker->Originator: SetMemento(Memento m)
Originator->Memento: GetState()
[<abstract>Subject |
|+Attach(Observer)
+Detach(Observer)
+Notify()
]<:-[ConcreteSubject
|+subjectState
|+GetState()
+SetState()
]
[Subject]--[<note>for all o in observers {o->Update()}]
[ConcreteSubject]--[<note>return subjectSate]
[Subject]-o +observers [<abstract>Observer||+Update()]
[Observer]<:-[ConcreteObserver|+observerState|+Update()]
[ConcreteSubject]<- +subject [ConcreteObserver]
[ConcreteObserver]--[<note>observerState = subject->GetState()]
participant ConcreteSubject
participant ConcreteObserver1
participant ConcreteObserver2
ConcreteObserver1->ConcreteSubject: SetState()
ConcreteSubject->ConcreteSubject: Notify()
ConcreteSubject->ConcreteObserver1: Update()
ConcreteObserver1->ConcreteSubject: GetState()
ConcreteSubject->ConcreteObserver2: Update()
ConcreteObserver2->ConcreteSubject: GetState()
[Context |
|+Request()
]--[<note>state->Handle()]
[Context]o- +state [<abstract> State | |+Handle()]
[State]<:-[ConcreteStateA||+Handle()]
[State]<:-[ConcreteStateB||+Handle()]
[Client] o- [<abstract>Strategy| Operation()]
[Strategy] <:-- [StrategyA]
[Strategy] <:-- [StrategyB]
[<abstract>AbstractClass|
|+TemplateMethod()
+PrimitiveOperation1()
+PrimitiveOperation2()
]<:-[ConcreteClass||+PrimitiveOperation1()
+PrimitiveOperation2()
]
[AbstractClass]--[<note>...
PrimitiveOperation1()
...
PrimitiveOperation2()
...
]
[Client
(from design_patterns)
|
|
]->[<abstract>Visitor
|
|+VisitConcreteElementA(ConcretElementA)
+VisitConcreteElementB(ConcretElementB)
]
[Visitor]<:-[ConcreteVisitor1 |
|+VisitConcreteElementA(ConcretElementA)
+VisitConcreteElementB(ConcretElementB)
]
[Visitor]<:-[ConcreteVisitor2 |
|+VisitConcreteElementA(ConcretElementA)
+VisitConcreteElementB(ConcretElementB)
]
[Client]->[ObjectStructure||]
[ObjectStructure]->[Element ||]
[Element]<:-[ConcreteElementA ||+Accept(Visitor v)
+OperationA()
]
[ConcreteElementA]--[<note>v->VisitConcreteElementA(this)]
[Element]<:-[ConcreteElementB ||+Accept(Visitor v)
+OperationB()
]
[ConcreteElementB]--[<note>v->VisitConcreteElementB(this)]
ObjectStructure->ConcreteElementA: Accept(Visitor v)
participant ConcreteElementB
ConcreteElementA->ConcreteVisitor1: VisitConcreteElementA(ConcretElementA)
ConcreteVisitor1->ConcreteElementA: OperationA()
ObjectStructure->ConcreteElementB: Accept(Visitor v)
ConcreteElementB->ConcreteVisitor1: VisitConcreteElementA(ConcretElementA)
ConcreteVisitor1->ConcreteElementB: OperationA()