Design Patterns | |
Creational: | Singleton, Factory, Abstract Factory, Builder, Prototype, ... |
Structural: | Adapter, Composite, Decorater, Facade, Proxy, ... |
Behavioral: | Observer, Strategy, State, Command, Iterator, ... |
public class Connection {
private static Connection instance;
private static int counter = 0;
// La visibilité du constructeur est privé pour intérdire
// une instanciation directe en dehors la classe Connection
private Connection() {
counter++;
}
public static Connection getInstance() {
if(instance == null) {
instance = new Connection();
}
return instance;
}
public void description() {
System.out.println("Connection........");
System.out.println("Nombre d'objets de type Connection instanciés : "+counter);
}
}
public class Client {
public static void main(String[] args) {
Connection cnx1 = Connection.getInstance();
Connection cnx2 = Connection.getInstance();
cnx1.description();
cnx2.description();
}
}
Connection........
Nombre d'objets de type Connection instanciés : 1
Connection........
Nombre d'objets de type Connection instanciés : 1
public class ThreadConnection {
private static ThreadConnection instance;
private static int counter = 0;
private ThreadConnection() {
counter++;
}
public static ThreadConnection getInstance() {
if(instance == null) {
//Thread.currentThread();
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
instance = new ThreadConnection();
}
return instance;
}
public void description() {
System.out.println("Connection........");
System.out.println("Nombre d'objets de type Connection instanciés : "+counter);
}
}
public class ImpThread extends Thread{
@Override
public void run() {
ThreadConnection.getInstance().description();
}
}
public class Client {
public static void main(String[] args) {
ImpThread t1 = new ImpThread();
ImpThread t2 = new ImpThread();
ImpThread t3 = new ImpThread();
t1.start();
t2.start();
t3.start();
}
}
Connection........
Nombre d'objets de type Connection instanciés : 1
Connection........
Nombre d'objets de type Connection instanciés : 2
Connection........
Nombre d'objets de type Connection instanciés : 3
public class ThreadConnection {
private static ThreadConnection instance;
private static int counter = 0;
private ThreadConnection() {
counter++;
}
public static synchronized ThreadConnection getInstance() {
if(instance == null) {
//Thread.currentThread();
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
instance = new ThreadConnection();
}
return instance;
}
public void description() {
System.out.println("Connection........");
System.out.println("Nombre d'objets de type Connection instanciés : "+counter);
}
}
Connection........
Connection........
Connection........
Nombre d'objets de type Connection instanciés : 1
Nombre d'objets de type Connection instanciés : 1
Nombre d'objets de type Connection instanciés : 1
public class ThreadConnection {
private static ThreadConnection instance;
private static int counter = 0;
private ThreadConnection() {
counter++;
}
public static ThreadConnection getInstance() {
// Ce n'est pas une redondance ;)
if(instance == null) {
synchronized(ThreadConnection.class) {
if(instance == null) {
//Thread.currentThread();
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
instance = new ThreadConnection();
}
}
}
return instance;
}
public void description() {
System.out.println("Connection........");
System.out.println("Nombre d'objets de type Connection instanciés : "+counter);
}
}
public interface Vehicule {
public void accelerate();
public void sound();
}
public class Car implements Vehicule{
@Override
public void accelerate() {
System.out.println("Speeeed");
}
@Override
public void sound() {
System.out.println("Bip Bip");
}
}
public class Bycicle {
public void pedal() {
System.out.println("pedal");
}
public void horn() {
System.out.println("rin rin");
}
}
public class BycicleAdapter implements Vehicule{
private Bycicle bycicle;
public BycicleAdapter(Bycicle bycicle) {
this.bycicle = bycicle;
}
@Override
public void accelerate() {
bycicle.pedal();
}
@Override
public void sound() {
bycicle.horn();
}
}
public class Client {
public static void main(String[] args) {
Vehicule car = new Car();
car.accelerate();
car.sound();
Bycicle b = new Bycicle();
Vehicule bycicle= new BycicleAdapter(b);
bycicle.accelerate();
bycicle.sound();
}
}
public abstract class Component {
private String type;
private String name;
public void add(Component component) {
}
public void remove(Component component) {
}
public Component getChild(int k) {
return null;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public abstract void description();
}
public class PdfFile extends Component{
public PdfFile(String name){
super.setType("pdf");
super.setName(name);
}
@Override
public void description() {
System.out.println("type : "+super.getType()+", name : "+super.getName());
}
}
public class TxtFile extends Component{
public TxtFile(String name) {
super.setType("txt");
super.setName(name);
}
@Override
public void description() {
System.out.println("type : "+super.getType()+", name : "+super.getName());
}
}
public class Folder extends Component{
private List<Component> components;
public Folder(String name) {
super.setType("folder");
super.setName(name);
this.components = new ArrayList<>();
}
@Override
public void add(Component component) {
this.components.add(component);
}
@Override
public void remove(Component component) {
this.components.remove(component);
}
@Override
public Component getChild(int k) {
return this.components.get(k);
}
@Override
public void description() {
System.out.println("type : "+super.getType()+", name : "+super.getName());
// Afficher les noms de tous les composants du dossier
System.out.print("Les composants : ");
for(Component c: components) {
System.out.print(c.getName()+", ");
}
System.out.println();
}
}
public class Client {
public static void main(String[] args) {
Folder folder1 = new Folder("folder1");
folder1.add(new TxtFile("txtFile1"));
folder1.add(new TxtFile("txtFile2"));
folder1.add(new PdfFile("pdfFile1"));
folder1.getChild(0).description();
System.out.println("-----------------------------");
folder1.description();
System.out.println("-----------------------------");
Folder folder2 = new Folder("folder2");
folder2.add(new PdfFile("pdfFile2"));
folder1.description();
folder1.add(folder2);
System.out.println("-----------------------------");
folder1.getChild(3).add(new TxtFile("txtFile3"));
folder2.description();
}
}
type : txt, name : txtFile1
-----------------------------
type : folder, name : folder1
Les composants : txtFile1, txtFile2, pdfFile1,
-----------------------------
type : folder, name : folder1
Les composants : txtFile1, txtFile2, pdfFile1,
-----------------------------
type : folder, name : folder2
Les composants : pdfFile2, txtFile3,
public interface SortStrategy {
public void algorithm();
}
public class InsertionSort implements SortStrategy{
@Override
public void algorithm() {
System.out.println("Insertion Sort");
}
}
public class SelectionSort implements SortStrategy{
@Override
public void algorithm() {
System.out.println("Selection Sort");
}
}
public class Quicksort implements SortStrategy{
@Override
public void algorithm() {
System.out.println("Quick Sort");
}
}
public class Context {
private SortStrategy sortStrategy;
public Context(SortStrategy sortStrategy) {
this.sortStrategy = sortStrategy;
}
public void setSortStrategy(SortStrategy sortStrategy) {
this.sortStrategy = sortStrategy;
}
public void operation() {
System.out.println("------ Starting Algorithm:");
this.sortStrategy.algorithm();
System.out.println("------ Ending Algorithm\n");
}
}
public class Client {
public static void main(String[] args) {
SortStrategy sortStrategy = new InsertionSort();
Context context = new Context(sortStrategy);
context.operation();
sortStrategy = new SelectionSort();
context.setSortStrategy(sortStrategy);
context.operation();
sortStrategy = new Quicksort();
context.setSortStrategy(sortStrategy);
context.operation();
}
}
------ Starting Algorithm:
Insertion Sort
------ Ending Algorithm
------ Starting Algorithm:
Selection Sort
------ Ending Algorithm
------ Starting Algorithm:
Quick Sort
------ Ending Algorithm
public interface Subject {
void subscribe(Observer observer);
void remove(Observer observer);
void notifyAllObservers();
}
public interface Observer {
public void update(Currency currency);
}
public class Person implements Observer{
private String name;
public Person(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public void update(Currency currency) {
System.out.println("Notification to "+this.name+": currency "+currency.getName()+" points is changed to "+currency.getPoints());
}
}
public class Currency {
private String name;
private int points;
public Currency(String name, int points) {
super();
this.name = name;
this.points = points;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPoints() {
return points;
}
public void setPoints(int points) {
this.points = points;
}
}
public class CurrencySub implements Subject{
private Currency currency;
private List<Observer> observers;
public CurrencySub(Currency currency) {
this.currency = currency;
this.observers = new ArrayList<>();
}
public Currency getCurrency() {
return currency;
}
public void setCurrency(Currency currency) {
this.currency = currency;
notifyAllObservers();
}
@Override
public void subscribe(Observer observer) {
this.observers.add(observer);
}
@Override
public void remove(Observer observer) {
this.observers.remove(observer);
}
@Override
public void notifyAllObservers() {
for(Observer observer: observers) {
observer.update(this.currency);
}
}
}
public class Client {
public static void main(String[] args) {
Currency dollards = new Currency("dollards", 1254);
CurrencySub currency = new CurrencySub(dollards);
Person yassine = new Person("yassine");
Person sami = new Person("sami");
currency.subscribe(yassine);
currency.subscribe(sami);
dollards.setPoints(1255);
currency.setCurrency(dollards);
}
}
Notification to yassine: currency dollards points is changed to 1255
Notification to sami: currency dollards points is changed to 1255
public class Person implements Observer {
private String name;
public Person(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public void update(Observable o, Object c) {
Currency Currency = (Currency) c;
System.out.println("Notification to "+this.name+": currency "+Currency.getName()+" points is changed to "+Currency.getPoints());
}
}
public class Currency {
private String name;
private int points;
public Currency(String name, int points) {
super();
this.name = name;
this.points = points;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPoints() {
return points;
}
public void setPoints(int points) {
this.points = points;
}
}
public class CurrencySub extends Observable{
private Currency currency;
public CurrencySub(Currency currency) {
this.currency = currency;
}
public Currency getCurrency() {
return currency;
}
public void setCurrency(Currency currency) {
this.currency = currency;
setChanged();
notifyObservers(currency);
}
}
public class Client {
public static void main(String[] args) {
Currency dollards = new Currency("dollards", 1254);
CurrencySub currency = new CurrencySub(dollards);
Person yassine = new Person("yassine");
Person sami = new Person("sami");
currency.addObserver(yassine);
currency.addObserver(sami);
dollards.setPoints(1253);
currency.setCurrency(dollards);
}
}
Notification to sami: currency dollards points is changed to 1253
Notification to yassine: currency dollards points is changed to 1253
public abstract class Sandwich {
public abstract int cost();
public abstract String description();
}
public class BasicSandwich extends Sandwich{
@Override
public int cost() {
return 20;
}
@Override
public String description() {
return "Sandwich";
}
}
public abstract class SandwichDecorator extends Sandwich{
private Sandwich sandwich;
public SandwichDecorator(Sandwich sandwich) {
this.sandwich = sandwich;
}
public Sandwich getSandwich() {
return sandwich;
}
public void setSandwich(Sandwich sandwich) {
this.sandwich = sandwich;
}
@Override
public int cost() {
return this.sandwich.cost();
}
@Override
public String description() {
return this.sandwich.description();
}
}
public class ChickenDecorator extends SandwichDecorator{
public ChickenDecorator(Sandwich sandwich) {
super(sandwich);
}
@Override
public int cost() {
return super.getSandwich().cost()+15;
}
@Override
public String description() {
return super.getSandwich().description()+", Chiken";
}
}
public class CheeseDecorator extends SandwichDecorator{
public CheeseDecorator(Sandwich sandwich) {
super(sandwich);
}
@Override
public int cost() {
return super.getSandwich().cost()+5;
}
@Override
public String description() {
return super.getSandwich().description()+", Cheese";
}
}
public class Client {
public static void main(String[] args) {
Sandwich sandwich1 = new ChickenDecorator(new CheeseDecorator(new BasicSandwich()));
Sandwich sandwich2 = new ChickenDecorator(new BasicSandwich());
System.out.println("------------- Sandwich1 :");
System.out.println("Ingredients : " + sandwich1.description());
System.out.println("Cost : " + sandwich1.cost());
System.out.println("\n------------- Sandwich2 :");
System.out.println("Ingredients : " + sandwich2.description());
System.out.println("Cost : " + sandwich2.cost());
}
}
------------- Sandwich1 :
Ingredients : Sandwich, Cheese, Chiken
Cost : 40
------------- Sandwich2 :
Ingredients : Sandwich, Chiken
Cost : 35
public interface ISP {
public void serviceSites(String url);
}
public class Internet implements ISP{
@Override
public void serviceSites(String url) {
System.out.println("http://"+url);
}
}
public class InternetProxy implements ISP{
private Internet internet;
private List<String> forbidenSites = Arrays.asList("facebook.com", "twitter.com");
public InternetProxy(Internet internet) {
this.internet = internet;
}
@Override
public void serviceSites(String url){
if(forbidenSites.contains(url)) {
System.out.println("Site "+url+" is forbidden!");
}else {
internet.serviceSites(url);
}
}
}
public class Client {
public static void main(String[] args) {
Internet internet = new Internet();
ISP internetProxy = new InternetProxy(internet);
internetProxy.serviceSites("facebook.com"); //Forbidden
internetProxy.serviceSites("google.com"); //Allowed
}
}
Site facebook.com is forbidden!
http://google.com