Design Pattern Strategy Si Adapter.docx

  • Uploaded by: Alexandra Lajtrik
  • 0
  • 0
  • October 2019
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Design Pattern Strategy Si Adapter.docx as PDF for free.

More details

  • Words: 1,412
  • Pages: 9
Exemple de Design Pattern Strategy si Adapter Problema1.O persoana(Person) este caracterizata de nume(name) si de varsta(age). Un Student e o persoana ce are in plus in atribut suplimentar medie (average). 1. Folosind design pattern-ul Strategy sa se ordoneze un sir de persoane (ce va contine persoane si studenti) dupa:  Varsta  Nume in ordine alfabetica  Dupa nume in ordine alfabetica, dar studentii vor aparea inaintea persoanelor ce nu sunt studenti 2. Folosind design pattern-ul Strategy sa se ordoneze un sir de studenti dupa medie. Implementarea design pattern-ul Strategy va utiliza si clasele interioare anonime; nu se vor folosi clasele prametrizate. Rezolvare. Forma generala a design pattern-ului Strategy:

In cazul nostru:

class Person{ String name; int age; public Person(String name, int age) { this.name = name; this.age = age; } @Override public String toString() {return "Person [name=" + name + ", age=" + age + "]";} final public static void sort(Person[] p, Comparator comp){ Person aux; for(int i=0;i{ boolean before(Person p1, Person p2); } class Student extends Person{ double average; public Student(String name, int age, double average) { super(name, age); this.average = average; } @Override public String toString() {return "Student [name=" + name + ", age=" + age + ", average=" + average + "]";} public static void sort(Student[] s,final Comparator<Student> comp){ Person.sort(s, new Comparator(){ public boolean before(Person p1, Person p2) { return comp.before((Student)p1,(Student)p2); } });} } public class TestPerson{ public static void main(String[] args){ Person[] p = { new Person("Mihai",16) , new Student("Ilie", 20, 9.0), new Person("Alex",34) , new Student("Ioana", 21, 9.50), new Person("Elena",20) , new Student("Maria", 18, 10.0)}; Person.sort(p, new Comparator(){ @Override public boolean before(Person p1, Person p2) { return p1.age
System.out.println("Persons sorted by Age:"); for(Person pers:p) System.out.println(pers); Person.sort(p, new Comparator(){ @Override public boolean before(Person p1, Person p2) { return p1.name.compareTo(p2.name)<0; } }); System.out.println("\nPersons sorted by Name:"); for(Person pers:p) System.out.println(pers); Person.sort(p, new Comparator(){ @Override public boolean before(Person p1, Person p2) { if(p1 instanceof Student && !(p2 instanceof Student)) return true; return p1.name.compareTo(p2.name)<0; } }); System.out.println("\nPersons sorted by Name(students first):"); for(Person pers:p) System.out.println(pers); } }

Rezultate: Persons sorted by Age: Person [name=Mihai, age=16] Student [name=Maria, age=18, average=10.0] Person [name=Elena, age=20] Student [name=Ilie, age=20, average=9.0] Student [name=Ioana, age=21, average=9.5] Person [name=Alex, age=34] Persons sorted by Name: Person [name=Alex, age=34] Person [name=Elena, age=20] Student [name=Ilie, age=20, average=9.0] Student [name=Ioana, age=21, average=9.5] Student [name=Maria, age=18, average=10.0] Person [name=Mihai, age=16] Persons sorted by Name(students first): Student [name=Ilie, age=20, average=9.0] Student [name=Ioana, age=21, average=9.5] Student [name=Maria, age=18, average=10.0] Person [name=Alex, age=34] Person [name=Elena, age=20] Person [name=Mihai, age=16]

Folosind design pattern-ul Strategy sa se ordoneze un sir de studenti dupa medie Adaugam urmatorul cod in clasa TestStudent: public class TestStudent{ public static void main(String[] args){ Student[] s ={new Student("George",21, 8.25),new Student("Ilie", 20,9),

new Student("Andreea",20, 5.75) , new Student("Ioana", 21, 9.50), new Student("Loana",20, 6) , new Student("Maria", 18, 10.0)}; Student.sort(s, new Comparator<Student>(){ @Override public boolean before(Student s1, Student s2) { return s1.average<s2.average; } }); System.out.println("\nStudents sorted by average:"); for(Student stud:s) System.out.println(stud); } }

Rezultate: Students sorted by average: Student [name=Andreea, age=20, average=5.75] Student [name=Loana, age=20, average=6.0] Student [name=George, age=21, average=8.25] Student [name=Ilie, age=20, average=9.0] Student [name=Ioana, age=21, average=9.5] Student [name=Maria, age=18, average=10.0]

Problema2. Fie un joc in care apar Luptatori. Fiecare luptator in parte are o cantitate de munitie. Luptatorul dispune de o Arma ce poate fi de diferite tipuri (mitraliera, pusca, pusca automata) si cu ea se poate trage consumand munitia mai incet sau mai repede (30 de unitati la tragerea cu mitraliera, 1 unitate la tragerea cu pusca si 10 unitati la tragerea cu pusca automata). Pe parcursul jocului luptatorul isi poate schimba arma. Sa se implementeze acest comportament folosind design pattern-ul Stragegy. Sa se utilizeze design pattern-ul Adapter pentru integrarea ca arma a luptatorului a unor Lansatoare (de Rachete sau de Misile) dintr-o biblioteca externa asupra careia nu avem controlul. Lansatorul nu dispune de metoda trage, ci de metoda lanseaza. Lansatoarele de rachete folosesc 50 de unitati de munitie iar cele de misile 100. Sa se testeze functionalitatile intr-un program de Test. Rezolvare. 1. Cream ierarhia de clase ce contine diferitele tipuri de Arma si implementam o prima solutie a problemei folosing design patternul Stretegy. Forma generala a design pattern-ului Strategy:

In cazul nostru:

abstract class Arma { protected int consumMunitie; public void trage(Luptator l){ if(l.getMunitie()>=consumMunitie){ System.out.print(l + " trage cu " + this); l.setMunitie(l.getMunitie() - consumMunitie); System.out.println("(munitie ramasa:" + l.getMunitie()+")"); } else{System.out.println(l + "nu are munitie suficienta pentru a trage cu " + this);} } @Override public String toString() {return this.getClass().getName();} } class Mitraliera extends Arma{ {consumMunitie = 30;} } class Pusca extends Arma{ {consumMunitie = 1;} } class PuscaAutomata extends Arma{ {consumMunitie = 10;} } class Luptator { private String nume; private int munitie; private Arma arma = new Pusca(); public Luptator(String nume, Arma arma, int munitie){ this.nume = nume; this.arma = arma; this.munitie = munitie; } public void trage(){arma.trage(this);} public void schimbaArma(Arma arma){ System.out.println( "\n" + this + " schimba " + this.arma +" cu " + arma); this.arma = arma; }

public int getMunitie() {return munitie;} public void setMunitie(int munitie) {this.munitie = munitie;} @Override public String toString() {return " " + nume + " ";} } public class TestLuptator { public static void main(String[] args) { Luptator mihai = new Luptator("Mihai", new Pusca(), 100); mihai.trage();mihai.trage();mihai.trage(); mihai.schimbaArma(new PuscaAutomata()); mihai.trage();mihai.trage(); mihai.schimbaArma(new Mitraliera()); for(int i=0;i<5; i++) mihai.trage(); mihai.schimbaArma(new Pusca()); for(int i=0;i<5; i++) mihai.trage(); } }

Rezultat: Mihai Mihai Mihai

trage cu Pusca (munitie:99) trage cu Pusca (munitie:98) trage cu Pusca (munitie:97)

Mihai Mihai Mihai

schimba Pusca cu PuscaAutomata trage cu PuscaAutomata (munitie:87) trage cu PuscaAutomata (munitie:77)

Mihai schimba PuscaAutomata cu Mitraliera Mihai trage cu Mitraliera (munitie:47) Mihai trage cu Mitraliera (munitie:17) Mihai nu are munitie suficienta pentru a trage cu Mitraliera Mihai nu are munitie suficienta pentru a trage cu Mitraliera Mihai nu are munitie suficienta pentru a trage cu Mitraliera Mihai Mihai Mihai Mihai Mihai Mihai

schimba Mitraliera cu Pusca trage cu Pusca (munitie:16) trage cu Pusca (munitie:15) trage cu Pusca (munitie:14) trage cu Pusca (munitie:13) trage cu Pusca (munitie:12)

2. Folosim design patternul Adapter pentru a adapta clasele din ierarhia lansatoarelor Forma generala a design pattern-ului Adapter:

In cazul nostru:

Obtinand urmatoarea diagrama globala:

Vom adauga acum clasele din ierarhia lansatoarelor si adaptorul interfetei acestora. public abstract class Lansator { public abstract String lanseaza(); @Override public String toString() {return this.getClass().getName();} } class LansatorRachete extends Lansator{ @Override public String lanseaza() { return " lanseaza racheta ... " ; } } class LansatorMisile extends Lansator{ @Override public String lanseaza() { return " lanseaza misil ... " ; } } class LansatorAdapter extends Arma{ Lansator lansator; public LansatorAdapter(Lansator lansator, int consumMunitie){ this.lansator = lansator; super.consumMunitie = consumMunitie; } @Override public void trage(Luptator l) { if(l.getMunitie()>=consumMunitie) System.out.print(lansator.lanseaza()); super.trage(l); } @Override public String toString() {return lansator.toString();} }

In programul de test se adauga urmatoarele linii: public class TestLuptator { public static void main(String[] args) { Luptator mihai = new Luptator("Mihai", new Pusca(), 100); mihai.trage();mihai.trage();mihai.trage(); mihai.schimbaArma(new PuscaAutomata()); mihai.trage();mihai.trage(); mihai.schimbaArma(new Mitraliera()); for(int i=0;i<5; i++) mihai.trage(); mihai.schimbaArma(new Pusca()); for(int i=0;i<5; i++) mihai.trage(); Luptator dan = new Luptator("Dan", new Pusca(), 500); dan.trage();

// Dan schimba arma cu Lansatoare si foloseste tot functia // trage care este adaptata pentru acestea dan.schimbaArma(new LansatorAdapter(new LansatorRachete(),50)); dan.trage();dan.trage(); dan.schimbaArma(new LansatorAdapter(new LansatorMisile(),100)); for(int i=0;i<5; i++) dan.trage(); } }

Rezultate: Dan

trage cu Pusca (munitie:499)

Dan schimba Pusca cu LansatorRachete lanseaza racheta ... Dan trage cu LansatorRachete (munitie:449) lanseaza racheta ... Dan trage cu LansatorRachete (munitie:399) Dan schimba LansatorRachete cu LansatorMisile lanseaza misil ... Dan trage cu LansatorMisile (munitie:299) lanseaza misil ... Dan trage cu LansatorMisile (munitie:199) lanseaza misil ... Dan trage cu LansatorMisile (munitie:99) Dan nu are munitie suficienta pentru a trage cu LansatorMisile Dan nu are munitie suficienta pentru a trage cu LansatorMisile

Problema propusa spre rezolvare. Sa se implementeze un algoritm de sortare folosind design paternul Strategy si interfata Comparable cu metoda compare pentru compararea obiectelor. Algoritmul de sortare va fi scris de catre voi; nu se va folosi functia sort din Java Collections. Se va aplica sortarea pe obiecte de tip Copaci. Copacii au doua atribute: numele speciei (mar, par, stejar, etc...) si o inaltime. Copacii care au fost tratati vor aparea in sortare inaintea celor care nu au fost. La inceput toti copacii sunt netratati, iar apoi devin Tratati prin efectuarea unui tratament. In programul de test, se va crea o lista de copaci, copacii find afisati la inceput in ordinea descrescatoare a inaltimii, iar, mai apoi, ei vor fi afisati crescator dupa numele speciei.

Related Documents

Design Pattern
November 2019 18
Design Pattern
November 2019 26
Design Pattern
July 2020 5
Design Pattern
November 2019 17

More Documents from ""

Scenariu - Tema Erd.docx
October 2019 9
October 2019 12
Hist.docx
June 2020 0