0% found this document useful (0 votes)
53 views10 pages

Observer (Comportamental)

The document discusses three design patterns: Observer, Composite, and Factory Method. 1) The Observer pattern allows objects to notify registered observers of any changes to their state. This is implemented using a Subject interface and concrete Subject/Observer classes. 2) The Composite pattern allows objects to be part of a tree structure and accessed uniformly. It defines interfaces for individual and composite components. 3) The Factory Method pattern provides a way to create objects without specifying the exact class. It defines a creation method that returns different product classes. Concrete creator and product classes implement an abstract creator interface.

Uploaded by

Andreea Matei
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
53 views10 pages

Observer (Comportamental)

The document discusses three design patterns: Observer, Composite, and Factory Method. 1) The Observer pattern allows objects to notify registered observers of any changes to their state. This is implemented using a Subject interface and concrete Subject/Observer classes. 2) The Composite pattern allows objects to be part of a tree structure and accessed uniformly. It defines interfaces for individual and composite components. 3) The Factory Method pattern provides a way to create objects without specifying the exact class. It defines a creation method that returns different product classes. Concrete creator and product classes implement an abstract creator interface.

Uploaded by

Andreea Matei
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Principiu GRASP: Creator

Rezolva problema:

 crearii unui obiect


 crearii unei noi instante a unei clase
 alegerii clasei responsabile pentru crearea unei noi instante a acesteia

Prin:
 decide cine poate fi creator pe baza asocierii obiectelor si interactiunii lor

Sabloane:

1. Observer (comportamental)

Definirea problemei:

 Angajatul sa stie ca statusul unui ticket s-a modificat cu succes.

Solutie:

 Mecanism care sa anunte angajatul ca statusul unui ticket s-a modificat sau nu cu succes.

Dezavantaj:

 Dependenta crescuta intre Angajat si Ticket.

Diagrama de clasa:
Secvente de cod:

Step1: Creating the Subject interface


namespace ObserverDesignPattern
{
public interface ISubject
{
void RegisterObserver(IObserver observer);
void RemoveObserver(IObserver observer);
void NotifyObservers();
}
}

Step2: Creating Concrete Subject


using System;

using System.Collections.Generic;

namespace ObserverDesignPattern

public class Subject : ISubject

private List<IObserver> observers = new List<IObserver>();

private string AngajatName { get; set; }

private string Availability { get; set; }

public Subject(string angajatName, string availability)

AngajatName = angajatName;

Availability = availability;

public string getAvailability()

return Availability;

}
public void setAvailability(string availability)

this.Availability = availability;

Console.WriteLine("Availability changed.");

NotifyObservers();

public void RegisterObserver(IObserver observer)

Console.WriteLine("Observer Added : " + ((Observer)observer).UserName );

observers.Add(observer);

public void AddObservers(IObserver observer)

observers.Add(observer);

public void RemoveObserver(IObserver observer)

observers.Remove(observer);

public void NotifyObservers()

Console.WriteLine("Angajat Name :"

+ AngajatName +

" is Now available. So notifying all Registered users ");

Console.WriteLine();

foreach (IObserver observer in observers)

observer.update(Availability);

}
}

Step3: Creating Observer Interface:


namespace ObserverDesignPattern

public interface IObserver

void update(string availability);

Step4: Creating Concrete Observer


using System;

namespace ObserverDesignPattern

public class Observer : IObserver

public string UserName { get; set; }

public Observer(string userName, ISubject subject)

UserName = userName;

subject.RegisterObserver(this);

public void update(string availabiliy)

Console.WriteLine("Hello " + UserName + ", Angajat is now " + availabiliy + ");

}
}

2. Composite (structural)

Definirea problemei:

 Se va afla responsabilul pentru cautarea clientilor care au modificat statusul unui ticket.

Solutie:

 Delegare responsabilitati de cautare a unui client.

Dezavantaj:

 Volum ridicat de responsabilitati.

Diagrama de clasa:

Secvente de cod:

Step1: Create an interface


namespace CompositeDesignPattern
{
public interface IComponent
{
void DisplayClient();
}
}
Step2: Creating Leaf class
using System;

namespace CompositeDesignPattern

public class Leaf : IComponent

public string Client { get; set; }

public string Name { get; set; }

public Leaf(string name, string client)

this.Client = client;

this.Name = name;

public void DisplayClient()

Console.WriteLine(Name +" : "+ Client);

}
Step3: Creating Composite class
namespace CompositeDesignPattern

public class Composite : IComponent

public string Name { get; set; }

List<IComponent> components = new List<IComponent>();

public Composite(string name)

this.Name = name;

public void AddComponent(IComponent component)

components.Add(component);

public void DisplayClient()

Console.WriteLine(Name);

foreach (var item in components)

item.DisplayClient();

}
3. Factory method (creational)

Definirea problemei:

 Se va afla responsabilul pentru crearea instantelor de adaugare sau selectare client.

Solutie:

 Prin atribuirea clasei Cauta client, responsabilitatea de a trimite cererea de actiune client catre
interfata AdaugareSelectareClient care va gestiona situatia.

Dezavantaj:

 Lipsa securitatii.

Diagrama de clase:

Secvente de cod:

Step1: Creating Product Interface


namespace FactoryMethodDesignPattern
{
public interface AdaugareSelectareClient
{
string GetClient();
}
}
Step3: Creating Abstract Creator
namespace FactoryMethodDesignPattern
{
public abstract class AdaugareSelectareClient Factory
{
protected abstract AdaugareSelectareClient MakeProduct();
public AdaugareSelectareClient CreateProduct()
{
return this.MakeProduct();
}
}
}

public interface AdaugareSelectareClient {

String execute();

public class Adaugare_Client implements Cautare_Client

private Client client;

    @Override

public String execute() {

     return client.open();

}   

public class Selectare_Client implements Cautare_Client {

@Override

public String execute() {

return client.save();

public String open() {

     return "Afisare client " + client;


}

public String save() { 

     return "Salvare client " + client;

You might also like