Struktūriniai šablonai

M.Gžegoževskis ... 2021-10-01 Java
  • Programavimas
  • Java
About 3 min

Adapter Pattern, Bridge Pattern, Composite Pattern, Decorator Pattern, Facade Pattern, Flyweight Pattern, Proxy Pattern.

# Adapter šablonas

interface Player {
public void play(String type, String fileName);
}
interface AudioPlayer {  
public void playAudio(String fileName);
}
interface VideoPlayer {  
public void playVideo(String fileName);
}
class MyAudioPlayer implements AudioPlayer {
@Override
public void playAudio(String fileName) {
System.out.println("Playing. Name: "+ fileName);    
}
}
class MyVideoPlayer implements VideoPlayer {
@Override
public void playVideo(String fileName) {
System.out.println("Playing. Name: "+ fileName);    
}
}

class MyPlayer implements Player {

AudioPlayer audioPlayer = new MyAudioPlayer();
VideoPlayer videoPlayer = new MyVideoPlayer();

public MyPlayer(){      
}
@Override
public void play(String audioType, String fileName) {
if(audioType.equalsIgnoreCase("avi")){
videoPlayer.playVideo(fileName);
}else if(audioType.equalsIgnoreCase("mp3")){
audioPlayer.playAudio(fileName);
}
}
}
public class Main{
public static void main(String[] args) {
MyPlayer myPlayer = new MyPlayer();

      myPlayer.play("mp3", "h.mp3");
      myPlayer.play("avi", "me.avi");
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46

# Bridge šablonas

interface Printer {
public void print(int radius, int x, int y);
}
class ColorPrinter implements Printer {
@Override
public void print(int radius, int x, int y) {
System.out.println("Color: " + radius +", x: " +x+", "+ y +"]");
}
}
class BlackPrinter implements Printer {
@Override
public void print(int radius, int x, int y) {
System.out.println("Black: " + radius +", x: " +x+", "+ y +"]");
}
}
abstract class Shape {
protected Printer print;
protected Shape(Printer p){
this.print = p;
}
public abstract void draw();  
}
class Circle extends Shape {
private int x, y, radius;

public Circle(int x, int y, int radius, Printer draw) {
super(draw);
this.x = x;  
this.y = y;  
this.radius = radius;
}

public void draw() {
print.print(radius,x,y);
}
}
public class Main {
public static void main(String[] args) {
Shape redCircle = new Circle(100,100, 10, new ColorPrinter());
Shape blackCircle = new Circle(100,100, 10, new BlackPrinter());

      redCircle.draw();
      blackCircle.draw();
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45

# Composite šablonas

// Design pattern Composite medžio struktūros realizacija
// Composite pattern is structural pattern since it creates a tree structure of group of objects. Composite pattern treats a group of objects as a single object. Composite pattern uses one class to represent a tree structure. In Composite pattern we create a class contains group of its own objects.
interface Component{
void showPrice();
}
class Leaf implements Component{
private String name;
private int price;

    public Leaf(String name, int price) {
        this.name = name;
        this.price = price;
    }
    @Override
    public void showPrice() {
        System.out.println(name+" : "+price);
    }
}
class Composite implements Component{
private String name;
List<Component> components = new ArrayList<>();

    public Composite(String name) {
        this.name = name;
    }

    public void addComponent(Component component){
        components.add(component);
    }

    @Override
    public void showPrice() {
        System.out.println(name);
        for (Component c: components) {
            c.showPrice();
        }
    }
}
public class Main {
public static void main(String[] args) {

        // Komponentai PC
        Component hdd = new Leaf("HDD", 120);
        Component mouse = new Leaf("Mouse", 15);
        Component monitor = new Leaf("Monitor",150);
        Component ram = new Leaf("Ram", 55);
        Component cpu = new Leaf("CPU", 220);
        Component printer = new Leaf("Printer", 180);

        // Komponentų klasifikavimas
        Composite peripherals = new Composite("Peripherals");
        Composite cabinet = new Composite("Cabinet");
        Composite motherBoard = new Composite("MotherBoard");
        Composite computer = new Composite("Computer");

        // Į mb įdedam ram ir cpu
        motherBoard.addComponent(cpu);
        motherBoard.addComponent(ram);

        // Išoriniai prietaisai
        peripherals.addComponent(mouse);
        peripherals.addComponent(monitor);
        peripherals.addComponent(printer);

        // Kompiuterio dėžė
        cabinet.addComponent(motherBoard);
        cabinet.addComponent(hdd);

       // Kompiuteris ir prijungtais išoriniais prietaisais
        computer.addComponent(peripherals);
        computer.addComponent(cabinet);
       
        computer.showPrice();
    }
}
// REZULTATAS ĮVYKDŽIUS PROGRAMĄ
Computer
Peripherals
Mouse : 15
Monitor : 150
Printer : 180
Cabinet
MotherBoard
CPU : 220
Ram : 55
HDD : 120
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86

# Composite 2 pavyzdys

// Composite design pattern 2 pavyzdys
class Employee {
private String name;
private String title;
private List<Employee> subordinates;

public Employee(String name,String title) {
this.name = name;
this.title = title;
subordinates = new ArrayList<Employee>();
}

public void add(Employee e) {
subordinates.add(e);
}

public void remove(Employee e) {
subordinates.remove(e);
}

public List<Employee> getSubordinates(){
return subordinates;
}

public String toString(){
return "Employee :[ Name : "+ name
+", dept : "+ title +subordinates +" ]";
}   
}

public class Main {
public static void main(String[] args) {
Employee CEO = new Employee("John","CEO");

      Employee headSales = new Employee("Rob","Sales");

      Employee headMarketing = new Employee("Mike","Marketing");

      Employee programmer1 = new Employee("Lili","Programmer");
      Employee programmer2 = new Employee("Bob","Programmer");

      Employee tester1 = new Employee("Jack","Tester");
      Employee tester2 = new Employee("Tom","Tester");

      CEO.add(headSales);
      CEO.add(headMarketing);

      headSales.add(tester1);
      headSales.add(tester2);

      headMarketing.add(programmer1);
      headMarketing.add(programmer2);

      //print all employees of the organization
      System.out.println(CEO); 
      for (Employee headEmployee : CEO.getSubordinates()) {
         System.out.println(headEmployee);
         for (Employee employee : headEmployee.getSubordinates()) {
            System.out.println(employee);
         }
      }    
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63

# Decorator šablonas

interface Printer {
void print();
}
class PaperPrinter implements Printer {
@Override
public void print() {
System.out.println("Paper Printer");
}
}
class PlasticPrinter implements Printer {
@Override
public void print() {
System.out.println("Plastic Printer");
}
}
abstract class PrinterDecorator implements Printer {
protected Printer decoratedPrinter;
public PrinterDecorator(Printer d){
this.decoratedPrinter = d;
}
public void print(){
decoratedPrinter.print();
}  
}
class Printer3D extends PrinterDecorator {
public Printer3D(Printer decoratedShape) {
super(decoratedShape);    
}
@Override
public void print() {
System.out.println("3D.");
decoratedPrinter.print();         
}
}
public class Main {
public static void main(String[] args) {
Printer plasticPrinter = new PlasticPrinter();
Printer plastic3DPrinter = new Printer3D(new PlasticPrinter());
Printer paper3DPrinter = new Printer3D(new PaperPrinter());
plasticPrinter.print();
plastic3DPrinter.print();
paper3DPrinter.print();
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44

# Facade šablonas


class ShapeFacade {
interface Shape {
void draw();
}
class Rectangle implements Shape {
@Override
public void draw() {
System.out.println("Rectangle::draw()");
}
}
class Square implements Shape {
@Override
public void draw() {
System.out.println("Square::draw()");
}
}
class Circle implements Shape {
@Override
public void draw() {
System.out.println("Circle::draw()");
}
}
private Shape circle = new Circle();
private Shape rectangle = new Rectangle();
private Shape square = new Square();

public ShapeFacade() {
}
public void drawCircle() {
circle.draw();
}
public void drawRectangle() {
rectangle.draw();
}
public void drawSquare() {
square.draw();
}
}
public class Main {
public static void main(String[] args) {
ShapeFacade shapeFacade = new ShapeFacade();
shapeFacade.drawCircle();
shapeFacade.drawRectangle();
shapeFacade.drawSquare();
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47

# Proxy šablonas

interface Printer {
void print();
}
class ConsolePrinter implements Printer {
private String fileName;

public ConsolePrinter(String fileName){
this.fileName = fileName;
}
@Override
public void print() {
System.out.println("Displaying " + fileName);
}
}
class ProxyPrinter implements Printer{
private ConsolePrinter consolePrinter;
private String fileName;

public ProxyPrinter(String fileName){
this.fileName = fileName;
}

@Override
public void print() {
if(consolePrinter == null){
consolePrinter = new ConsolePrinter(fileName);
}
consolePrinter.print();
}
}
public class Main {

public static void main(String[] args) {
Printer image = new ProxyPrinter("test");
image.print();   
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
Last update: September 20, 2021 06:35
Contributors: Marius Gžegoževskis