Bläddra i källkod

init

Signed-off-by: Jean-Michel Batto <jean-michel.batto@eldarsoft.com>
Jean-Michel Batto 4 dagar sedan
förälder
incheckning
f3ec5ff27c

+ 132 - 0
23pattern-go/23pattern-go.go

@@ -0,0 +1,132 @@
+package main
+
+import (
+	"fmt"
+	"pattern"
+)
+
+func main() {
+	fmt.Println("--- Démonstration des 23 Patterns GoF ---")
+
+	// 1. Singleton
+	s := pattern.GetInstance()
+	fmt.Printf("[1] Singleton: %s\n", s)
+
+	// 2. Factory Method
+	dog := pattern.CreateAnimal("dog")
+	fmt.Printf("[2] Factory Method: Dog says %s\n", dog.Speak())
+
+	// 3. Abstract Factory
+	var factory pattern.GUIFactory = &pattern.WinFactory{}
+	btn := factory.CreateButton()
+	fmt.Print("[3] Abstract Factory: ")
+	btn.Paint()
+
+	// 4. Builder
+	builder := pattern.NewHouseBuilder()
+	house := builder.SetWindows(4).SetDoors(2).SetRoof("Ardoise").Build()
+	fmt.Printf("[4] Builder: Maison avec %d fenêtres\n", house.Windows)
+
+	// 5. Prototype
+	proto := &pattern.ConcretePrototype{Name: "Original"}
+	clone := proto.Clone().(*pattern.ConcretePrototype)
+	fmt.Printf("[5] Prototype: Clone name is %s\n", clone.Name)
+
+	// 6. Adapter
+	adapter := &pattern.PrinterAdapter{OldPrinter: &pattern.MyLegacyPrinter{}}
+	fmt.Printf("[6] Adapter: %s\n", adapter.PrintModern("Message"))
+
+	// 7. Bridge
+	red := &pattern.RedCircle{}
+	circle := pattern.Circle{X: 10, Y: 10, Radius: 5}
+	circle.SetAPI(red)
+	fmt.Print("[7] Bridge: ")
+	circle.Draw()
+
+	// 8. Composite
+	root := &pattern.Composite{}
+	root.Add(&pattern.Leaf{Name: "Feuille A"})
+	fmt.Printf("[8] Composite: %s\n", root.Operation())
+
+	// 9. Decorator
+	coffee := &pattern.SimpleCoffee{}
+	milkCoffee := &pattern.MilkDecorator{Coffee: coffee}
+	fmt.Printf("[9] Decorator: %s coûte %d\n", milkCoffee.Description(), milkCoffee.Cost())
+
+	// 10. Facade
+	facade := pattern.NewFacade()
+	fmt.Printf("[10] Facade: %s\n", facade.Operation())
+
+	// 11. Flyweight
+	flyFactory := pattern.NewCharacterFactory()
+	charA := flyFactory.GetCharacter('A')
+	fmt.Printf("[11] Flyweight: Character is %c\n", charA.Char)
+
+	// 12. Proxy
+	proxy := &pattern.Proxy{}
+	fmt.Printf("[12] Proxy: %s\n", proxy.Request())
+
+	// 13. Chain of Responsibility
+	h1 := &pattern.ConcreteHandler1{}
+	h2 := &pattern.ConcreteHandler2{}
+	h1.SetNext(h2)
+	fmt.Printf("[13] Chain: %s\n", h1.Handle("two"))
+
+	// 14. Command
+	light := &pattern.Light{}
+	cmd := &pattern.LightOnCommand{Light: light}
+	fmt.Printf("[14] Command: %s\n", cmd.Execute())
+
+	// 15. Interpreter
+	expr := &pattern.TerminalExpression{Data: "Hello"}
+	fmt.Printf("[15] Interpreter: Is valid? %v\n", expr.Interpret())
+
+	// 16. Iterator
+	coll := &pattern.Collection{Items: []interface{}{1, 2, 3}}
+	it := coll.CreateIterator()
+	fmt.Print("[16] Iterator: ")
+	for it.HasNext() {
+		fmt.Printf("%v ", it.Next())
+	}
+	fmt.Println()
+
+	// 17. Mediator
+	m := &pattern.ConcreteMediator{}
+	c1 := &pattern.Component1{Mediator: m}
+	m.SetComponent1(c1)
+	fmt.Print("[17] Mediator: ")
+	c1.Trigger()
+
+	// 18. Memento
+	originator := &pattern.Originator{State: "On"}
+	memento := originator.CreateMemento()
+	originator.State = "Off"
+	originator.RestoreMemento(memento)
+	fmt.Printf("[18] Memento: Restored state: %s\n", originator.State)
+
+	// 19. Observer
+	subject := &pattern.Subject2{}
+	obs := &pattern.ConcreteObserver{ID: 1}
+	subject.Attach(obs)
+	fmt.Print("[19] Observer: ")
+	subject.SetState("Updated")
+
+	// 20. State
+	ctx := &pattern.Context{State: &pattern.ConcreteStateA{}}
+	fmt.Printf("[20] State: %s\n", ctx.Request())
+
+	// 21. Strategy
+	contextStrat := &pattern.ContextStrat{Strategy: &pattern.ConcreteStrategyA{}}
+	fmt.Printf("[21] Strategy: %s\n", contextStrat.Execute())
+
+	// 22. Template Method
+	// En Go, on passe l'implémentation spécifique au "Template"
+	worker := pattern.NewWorker(&pattern.ConcreteWorker{})
+	fmt.Printf("[22] Template Method: %s\n", worker.Execute())
+
+	// 23. Visitor
+	element := &pattern.ConcreteElementA{}
+	visitor := &pattern.ConcreteVisitor{}
+	fmt.Print("[23] Visitor: ")
+	element.Accept(visitor)
+}

+ 7 - 0
23pattern-go/go.mod

@@ -0,0 +1,7 @@
+module 23pattern-go
+
+go 1.20
+
+require pattern v1.0.0
+
+replace pattern => ./pattern

+ 152 - 0
23pattern-go/pattern/behavioral.go

@@ -0,0 +1,152 @@
+// behavioral
+package pattern
+
+import "fmt"
+
+// 13. Chain of Responsibility
+type Handler interface {
+	SetNext(handler Handler)
+	Handle(request string) string
+}
+
+type BaseHandler struct {
+	next Handler
+}
+
+func (b *BaseHandler) SetNext(h Handler) { b.next = h }
+
+type ConcreteHandler1 struct{ BaseHandler }
+
+func (h *ConcreteHandler1) Handle(req string) string {
+	if req == "one" { return "Handled by 1" }
+	if h.next != nil { return h.next.Handle(req) }
+	return "Not handled"
+}
+
+type ConcreteHandler2 struct{ BaseHandler }
+
+func (h *ConcreteHandler2) Handle(req string) string {
+	if req == "two" { return "Handled by 2" }
+	if h.next != nil { return h.next.Handle(req) }
+	return "Not handled"
+}
+
+// 14. Command
+type Command interface { Execute() string }
+
+type Light struct{}
+func (l *Light) On() string { return "Light On" }
+
+type LightOnCommand struct { Light *Light }
+func (c *LightOnCommand) Execute() string { return c.Light.On() }
+
+// 15. Interpreter
+type Expression interface { Interpret() bool }
+type TerminalExpression struct { Data string }
+func (t *TerminalExpression) Interpret() bool { return len(t.Data) > 0 }
+
+// 16. Iterator
+type Iterator interface {
+	HasNext() bool
+	Next() interface{}
+}
+
+type Collection struct { Items []interface{} }
+func (c *Collection) CreateIterator() Iterator {
+	return &CollectionIterator{collection: c}
+}
+
+type CollectionIterator struct {
+	collection *Collection
+	index      int
+}
+
+func (i *CollectionIterator) HasNext() bool { return i.index < len(i.collection.Items) }
+func (i *CollectionIterator) Next() interface{} {
+	item := i.collection.Items[i.index]
+	i.index++
+	return item
+}
+
+// 17. Mediator
+type Mediator interface { Notify(sender interface{}, event string) }
+
+type ConcreteMediator struct {
+	c1 *Component1
+}
+
+func (m *ConcreteMediator) SetComponent1(c *Component1) { m.c1 = c }
+func (m *ConcreteMediator) Notify(s interface{}, e string) {
+	fmt.Printf("Mediator: reacting to %s\n", e)
+}
+
+type Component1 struct { Mediator Mediator }
+func (c *Component1) Trigger() { c.Mediator.Notify(c, "click") }
+
+// 18. Memento
+type Memento struct { State string }
+type Originator struct { State string }
+func (o *Originator) CreateMemento() *Memento { return &Memento{State: o.State} }
+func (o *Originator) RestoreMemento(m *Memento) { o.State = m.State }
+
+// 19. Observer
+type Observer interface { Update(string) }
+type Subject2 struct {
+	observers []Observer
+	state     string
+}
+
+func (s *Subject2) Attach(o Observer) { s.observers = append(s.observers, o) }
+func (s *Subject2) SetState(st string) {
+	s.state = st
+	for _, o := range s.observers { o.Update(st) }
+}
+
+type ConcreteObserver struct{ ID int }
+func (o *ConcreteObserver) Update(s string) { fmt.Printf("Observer %d: state is %s\n", o.ID, s) }
+
+// 20. State
+type State interface { Handle() string }
+type Context struct { State State }
+func (c *Context) Request() string { return c.State.Handle() }
+
+type ConcreteStateA struct{}
+func (s *ConcreteStateA) Handle() string { return "State A" }
+
+// 21. Strategy
+type Strategy interface { Execute() string }
+type ConcreteStrategyA struct{}
+func (s *ConcreteStrategyA) Execute() string { return "Strategy A" }
+
+type ContextStrat struct { Strategy Strategy }
+func (c *ContextStrat) Execute() string { return c.Strategy.Execute() }
+
+// 22. Template Method (Idiomatique Go)
+type WorkerInterface interface {
+	Work() string
+}
+
+type TemplateWorker struct {
+	provider WorkerInterface
+}
+
+func NewWorker(p WorkerInterface) *TemplateWorker { return &TemplateWorker{provider: p} }
+func (t *TemplateWorker) Execute() string {
+	return "Prep -> " + t.provider.Work() + " -> Clean"
+}
+
+type ConcreteWorker struct{}
+func (w *ConcreteWorker) Work() string { return "Specific Task" }
+
+// 23. Visitor
+type Visitor interface {
+	VisitA(*ConcreteElementA)
+}
+
+type Element interface { Accept(Visitor) }
+
+type ConcreteElementA struct{}
+func (e *ConcreteElementA) Accept(v Visitor) { v.VisitA(e) }
+
+type ConcreteVisitor struct{}
+func (v *ConcreteVisitor) VisitA(e *ConcreteElementA) { fmt.Println("Visited Element A") }

+ 113 - 0
23pattern-go/pattern/creational.go

@@ -0,0 +1,113 @@
+// creational
+package pattern
+
+import (
+	"sync"
+)
+
+// 1. Singleton Pattern
+type singleton struct {
+	Data string
+}
+
+var (
+	instance *singleton
+	once     sync.Once
+)
+
+func GetInstance() *singleton {
+	once.Do(func() {
+		instance = &singleton{Data: "I am singleton"}
+	})
+	return instance
+}
+
+// 2. Factory Method Pattern
+type Animal interface {
+	Speak() string
+}
+
+type Dog struct{}
+type Cat struct{}
+
+func (d *Dog) Speak() string { return "Woof!" }
+func (c *Cat) Speak() string { return "Meow!" }
+
+func CreateAnimal(animalType string) Animal {
+	switch animalType {
+	case "dog":
+		return &Dog{}
+	case "cat":
+		return &Cat{}
+	default:
+		return nil
+	}
+}
+
+// 3. Abstract Factory Pattern
+type Button interface {
+	Paint()
+}
+
+type WinButton struct{}
+type MacButton struct{}
+
+func (w *WinButton) Paint() { println("Windows button") }
+func (m *MacButton) Paint() { println("Mac button") }
+
+type GUIFactory interface {
+	CreateButton() Button
+}
+
+type WinFactory struct{}
+type MacFactory struct{}
+
+func (w *WinFactory) CreateButton() Button { return &WinButton{} }
+func (m *MacFactory) CreateButton() Button { return &MacButton{} }
+
+// 4. Builder Pattern
+type House struct {
+	Windows int
+	Doors   int
+	Roof    string
+}
+
+type HouseBuilder struct {
+	house *House
+}
+
+func NewHouseBuilder() *HouseBuilder {
+	return &HouseBuilder{house: &House{}}
+}
+
+func (b *HouseBuilder) SetWindows(count int) *HouseBuilder {
+	b.house.Windows = count
+	return b
+}
+
+func (b *HouseBuilder) SetDoors(count int) *HouseBuilder {
+	b.house.Doors = count
+	return b
+}
+
+func (b *HouseBuilder) SetRoof(style string) *HouseBuilder {
+	b.house.Roof = style
+	return b
+}
+
+func (b *HouseBuilder) Build() *House {
+	return b.house
+}
+
+// 5. Prototype Pattern
+type Prototype interface {
+	Clone() Prototype
+}
+
+type ConcretePrototype struct {
+	Name string
+}
+
+func (p *ConcretePrototype) Clone() Prototype {
+	return &ConcretePrototype{Name: p.Name}
+}

+ 3 - 0
23pattern-go/pattern/go.mod

@@ -0,0 +1,3 @@
+module 23pattern-go/pattern
+
+go 1.20

+ 161 - 0
23pattern-go/pattern/structural.go

@@ -0,0 +1,161 @@
+// structural
+package pattern
+
+import (
+	"fmt"
+)
+
+// 6. Adapter Pattern
+type LegacyPrinter interface {
+	Print(s string) string
+}
+
+type MyLegacyPrinter struct{}
+
+func (p *MyLegacyPrinter) Print(s string) string {
+	return fmt.Sprintf("Legacy: %s", s)
+}
+
+type ModernPrinter interface {
+	PrintModern(s string) string
+}
+
+type PrinterAdapter struct {
+	OldPrinter LegacyPrinter
+}
+
+func (p *PrinterAdapter) PrintModern(s string) string {
+	return p.OldPrinter.Print(s)
+}
+
+// 7. Bridge Pattern
+type DrawAPI interface {
+	DrawCircle(x, y, radius int)
+}
+
+type RedCircle struct{}
+type GreenCircle struct{}
+
+func (r *RedCircle) DrawCircle(x, y, radius int) {
+	fmt.Printf("Drawing red circle at (%d,%d) radius %d\n", x, y, radius)
+}
+
+func (g *GreenCircle) DrawCircle(x, y, radius int) {
+	fmt.Printf("Drawing green circle at (%d,%d) radius %d\n", x, y, radius)
+}
+
+type Circle struct {
+	api    DrawAPI
+	X, Y, Radius int
+}
+
+func (c *Circle) SetAPI(api DrawAPI) { c.api = api }
+func (c *Circle) Draw() { c.api.DrawCircle(c.X, c.Y, c.Radius) }
+
+// 8. Composite Pattern
+type Component interface {
+	Operation() string
+}
+
+type Leaf struct {
+	Name string
+}
+
+func (l *Leaf) Operation() string { return l.Name }
+
+type Composite struct {
+	children []Component
+}
+
+func (c *Composite) Add(child Component) {
+	c.children = append(c.children, child)
+}
+
+func (c *Composite) Operation() string {
+	result := "Branch("
+	for i, child := range c.children {
+		result += child.Operation()
+		if i < len(c.children)-1 {
+			result += ", "
+		}
+	}
+	return result + ")"
+}
+
+// 9. Decorator Pattern
+type Coffee interface {
+	Cost() int
+	Description() string
+}
+
+type SimpleCoffee struct{}
+
+func (s *SimpleCoffee) Cost() int           { return 5 }
+func (s *SimpleCoffee) Description() string { return "Simple coffee" }
+
+type MilkDecorator struct {
+	Coffee Coffee
+}
+
+func (m *MilkDecorator) Cost() int           { return m.Coffee.Cost() + 2 }
+func (m *MilkDecorator) Description() string { return m.Coffee.Description() + ", milk" }
+
+// 10. Facade Pattern
+type SubsystemA struct{}
+func (s *SubsystemA) OperationA() string { return "SubA" }
+
+type SubsystemB struct{}
+func (s *SubsystemB) OperationB() string { return "SubB" }
+
+type Facade struct {
+	a *SubsystemA
+	b *SubsystemB
+}
+
+func NewFacade() *Facade {
+	return &Facade{&SubsystemA{}, &SubsystemB{}}
+}
+
+func (f *Facade) Operation() string {
+	return f.a.OperationA() + " + " + f.b.OperationB()
+}
+
+// 11. Flyweight Pattern
+type CharacterFlyweight struct {
+	Char rune
+}
+
+type CharacterFactory struct {
+	chars map[rune]*CharacterFlyweight
+}
+
+func NewCharacterFactory() *CharacterFactory {
+	return &CharacterFactory{chars: make(map[rune]*CharacterFlyweight)}
+}
+
+func (f *CharacterFactory) GetCharacter(c rune) *CharacterFlyweight {
+	if _, ok := f.chars[c]; !ok {
+		f.chars[c] = &CharacterFlyweight{Char: c}
+	}
+	return f.chars[c]
+}
+
+// 12. Proxy Pattern
+type Subject interface {
+	Request() string
+}
+
+type RealSubject struct{}
+
+func (s *RealSubject) Request() string { return "RealSubject active" }
+
+type Proxy struct {
+	realSubject *RealSubject
+}
+
+func (p *Proxy) Request() string {
+	if p.realSubject == nil {
+		p.realSubject = &RealSubject{}
+	}
+	return "Proxy: " + p.realSubject.Request()
+}

BIN
Cuicui.pdf


BIN
Exemple-presentation-chiffrage.xlsx


BIN
cours8-PPCS-JMB-20252026-Compte Azure.pdf


BIN
cours8-PPCS-JMB-20252026.pdf


BIN
support bibliographique/IYSM.-Thirty-years-of-IFPUG.-Software-Economics-and-Function-Point-Metrics-Capers-Jones.pdf