Пример 1
Window.java
public interface Window {
public void Draw();
public String getDescription();
}
SimpleWindow.java
public class SimpleWindow implements Window {
public void Draw() {
//TODO
return;
}
public String getDescription() {
return "Simple Window";
}
}
WindowDecorator.java
public abstract class WindowDecorator implements Window {
protected Window WindowToBeDecorated;
public abstract void Draw();
public abstract String getDescription();
public WindowDecorator(Window WindowToBeDecorated) {
this.WindowToBeDecorated = WindowToBeDecorated;
}
}
VerticalScrollDecorator.java
public class VerticalScrollDecorator extends WindowDecorator {
@Override
public void Draw() {
//TODO
return;
}
@Override
public String getDescription() {
return WindowToBeDecorated.getDescription()+" which has been decorated with vertical scrollbar";
}
public VerticalScrollDecorator(Window WindowToBeDecorated) {
super(WindowToBeDecorated);
}
}
HorizontalScrollDecorator.java
public class HorizontalScrollDecorator extends WindowDecorator {
@Override
public void Draw() {
//TODO
return ;
}
@Override
public String getDescription() {
return WindowToBeDecorated.getDescription() + " which has been decorated with horizontal scrollbar";
}
public HorizontalScrollDecorator(Window WindowToBeDecorated) {
super(WindowToBeDecorated);
}
}
BorderDecorator.java
public class BorderDecorator extends WindowDecorator {
@Override
public void Draw() {
//TODO
return ;
}
@Override
public String getDescription() {
return WindowToBeDecorated.getDescription() + " which has borders";
}
public BorderDecorator(Window WindowToBeDecorated) {
super(WindowToBeDecorated);
}
}
DecoratorTest.java
public class DecoratorTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
Window win = new SimpleWindow();
System.out.println(win.getDescription());
win = new VerticalScrollDecorator(new SimpleWindow());
System.out.println(win.getDescription());
win = new HorizontalScrollDecorator(new SimpleWindow());
System.out.println(win.getDescription());
win = new VerticalScrollDecorator(new HorizontalScrollDecorator(new SimpleWindow()));
System.out.println(win.getDescription());
win = new BorderDecorator(new SimpleWindow());
System.out.println(win.getDescription());
win = new VerticalScrollDecorator(new BorderDecorator(new SimpleWindow()));
System.out.println(win.getDescription());
}
}
Пример 2
UML
Calculator.java
package code.lalev.Calculator;
public interface Calculator {
public String Dobavi(String a, String b); //Добавя две числа
public String Umnoji(String a, String b); //Умножава две числа
public String Izvadi(String a, String b); //Изважда b от а
public String Razdeli(String a, String b); //Дели a на b
public void Zapameti(String a);
public String Vystanovi();
}
ProstCalculator.java
package code.lalev.Calculator;
public class ProstCalculator implements Calculator {
Double memory;
public ProstCalculator() {
memory = 0d;
}
//Добавя две числа
public String Dobavi(String a, String b) {
//Конвертира a от String в Double
Double newa = Double.parseDouble(a);
Double newb = Double.parseDouble(b);
Double result = newa+newb;
return result.toString();
}
//Умножава две числа
public String Umnoji(String a, String b) {
//Конвертира a от String в Double
Double newa = Double.parseDouble(a);
Double newb = Double.parseDouble(b);
Double result = newa*newb;
return result.toString();
}
//Изважда b от а
public String Izvadi(String a, String b) {
//Конвертира a от String в Double
Double newa = Double.parseDouble(a);
Double newb = Double.parseDouble(b);
Double result = newa-newb;
return result.toString();
}
//Дели a на b
public String Razdeli(String a, String b) {
//Конвертира a от String в Double
Double newa = Double.parseDouble(a);
Double newb = Double.parseDouble(b);
Double result = newa/newb;
return result.toString();
}
//Дели a на b
public void Zapameti(String a) {
memory = Double.parseDouble(a);
}
//Дели a на b
public String Vystanovi() {
return memory.toString();
}
}
ObshtCalculator.java
package code.lalev.Calculator;
public abstract class ObshtCalculator implements Calculator {
protected Calculator calc;
public ObshtCalculator(Calculator calc) {
this.calc = calc;
}
}
DvoichenCalculator.java
package code.lalev.Calculator;
public class DvoichenCalculator extends ObshtCalculator {
public DvoichenCalculator(Calculator calc) {
super(calc);
}
@Override
public String Dobavi(String a, String b) {
Integer newa = Integer.parseInt(a, 2);
Integer newb = Integer.parseInt(b, 2);
return Integer.toBinaryString(
(int) Double.parseDouble(
calc.Dobavi(newa.toString(),
newb.toString())));
}
@Override
public String Umnoji(String a, String b) {
Integer newa = Integer.parseInt(a, 2);
Integer newb = Integer.parseInt(b, 2);
return Integer.toBinaryString(
(int) Double.parseDouble(
calc.Umnoji(newa.toString(),
newb.toString())));
}
@Override
public String Izvadi(String a, String b) {
Integer newa = Integer.parseInt(a, 2);
Integer newb = Integer.parseInt(b, 2);
return Integer.toBinaryString(
(int) Double.parseDouble(
calc.Izvadi(newa.toString(),
newb.toString())));
}
@Override
public String Razdeli(String a, String b) {
return "NaN";
}
@Override
public void Zapameti(String a) {
Integer newa = Integer.parseInt(a, 2);
calc.Zapameti(newa.toString());
}
@Override
public String Vystanovi() {
return Integer.toBinaryString(
Integer.parseInt(
calc.Vystanovi().toString()));
}
}
Няма коментари:
Публикуване на коментар