Objektų kūrimo šablonai

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

Šie projektavimo šablonai: Factory Pattern, Abstract Factory Pattern, Singleton Pattern, Prototype Pattern, Builder Pattern yra skirti objektams sukurti.

# Factory šablonas

// Factory design pattern (projektavimo šablonas skirtas
// nuslėpti objekto kūrimą naudojant getInstance metodą).

interface Shop {
void details();
}

class Maxima implements Shop{
@Override
public void details() {
System.out.println("Maxima prekybos centras");
}
}
class IKI implements Shop{
@Override
public void details() {
System.out.println("Iki prekybos centras.");
}
}
class Norfa implements Shop{
@Override
public void details() {
System.out.println("Norfa prekybos centras.");
}
}
class NezinomaParduotuve implements Shop{
@Override
public void details() {
System.out.println("Nežinomas prekybos centras.");
}
}
class ShopFactory{
public Shop getInstance(String obj){
switch (obj) {
case "Maxima":
return new Maxima();
case "IKI":
return new IKI();
case "Norfa":
return new Norfa();
default:
return new NezinomaParduotuve();
}
}
}

public class Main {
public static void main(String[] args) {
ShopFactory shopFactory = new ShopFactory();
Shop shop = shopFactory.getInstance("Maxima");
shop.details();
}
}
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

# Abstract Factory šablonas

// AbstractFactory projektavimo šablonas
abstract class AbstractFactory {
abstract Printer getPrinter(String type);
abstract Shape getShape(String shape) ;
}
/* Implementacija AbstractFactory klasės.
Kaip matome privalome įgyvendinti visus abstrakčius metodus.
Priklausomai, kuri klasė paveldi AbstactFactory pasirenkame
tik tai klasei būdingus metodus ir juos perašome.
*/
class ShapeFactory extends AbstractFactory {

@Override
public Shape getShape(String shapeType){
if(shapeType == null){
return null;
}    
if(shapeType.equalsIgnoreCase("CIRCLE")){
return new Circle();
} else if(shapeType.equalsIgnoreCase("RECTANGLE")){
return new Rectangle();
} else if(shapeType.equalsIgnoreCase("SQUARE")){
return new Square();
}
return null;
}

@Override
Printer getPrinter(String type) {
return null;
}
}
class PrinterFactory extends AbstractFactory {

@Override
public Shape getShape(String shapeType){
return null;
}

@Override
Printer getPrinter(String type) {
if(type == null){
return null;
}    
if(type.equalsIgnoreCase("paper")){
return new PaperPrinter();
} else if(type.equalsIgnoreCase("web")){
return new WebPrinter();
} else if(type.equalsIgnoreCase("Screen")){
return new ScreenPrinter();
}
return null;
}
}
The following code shows how to use abstract factory pattern.

We are going to create shapes and Printers. For shapes, we would have circle, rectangle and square. For printers we will have paper Printer, Web Printer and screen Printer.

For shape we will create Shape interface, as follows

interface Shape {
void draw();
}

class Rectangle implements Shape {

@Override
public void draw() {
System.out.println("Inside Rectangle::draw() method.");
}
}
class Square implements Shape {

@Override
public void draw() {
System.out.println("Inside Square::draw() method.");
}
}
class Circle implements Shape {

@Override
public void draw() {
System.out.println("Inside Circle::draw() method.");
}
}
After that we create an interface for Printers.

interface Printer{
void print();
}
Then we create concrete classes implementing the Printer interface.

class PaperPrinter implements Printer{

@Override
public void print() {
System.out.println("paper");
}
}
class WebPrinter implements Printer{

@Override
public void print() {
System.out.println("web");
}
}
class ScreenPrinter implements Printer{

@Override
public void print() {
System.out.println("screen");
}
}


Create a Factory generator/producer class to get factories by passing an information such as Shape or Printer

class FactoryProducer {
public static AbstractFactory getFactory(String choice){
if(choice.equalsIgnoreCase("SHAPE")){
return new ShapeFactory();
} else if(choice.equalsIgnoreCase("Printer")){
return new PrinterFactory();
}
return null;
}
}
public class Main {
public static void main(String[] args) {
//get shape factory
AbstractFactory shapeFactory = FactoryProducer.getFactory("SHAPE");
//get an object of Shape Circle
Shape shape1 = shapeFactory.getShape("CIRCLE");
//call draw method of Shape Circle
shape1.draw();
//get an object of Shape Rectangle
Shape shape2 = shapeFactory.getShape("RECTANGLE");
//call draw method of Shape Rectangle
shape2.draw();
//get an object of Shape Square
Shape shape3 = shapeFactory.getShape("SQUARE");
//call draw method of Shape Square
shape3.draw();
//get printer factory
AbstractFactory printerFactory = FactoryProducer.getFactory("printer");

    Printer printer1 = printerFactory.getPrinter("Paper");
    printer1.print();
    Printer printer2 = printerFactory.getPrinter("Web");
    printer2.print();
    Printer printer3 = printerFactory.getPrinter("Screen");
    printer3.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
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154

# Singleton šablonas


class MainWindow {
//create an object of MainWindow
private static MainWindow instance = new MainWindow();

//make the constructor private so that this class cannot be
//instantiated by other class
private MainWindow(){}

//Get the only object available
public static MainWindow getInstance(){
return instance;
}

public void showMessage(){
System.out.println("Hello World!");
}
}

public class Main {
public static void main(String[] args) {
//Get the only object available
MainWindow object = MainWindow.getInstance();

      //show the message
      object.showMessage();
}
}
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

# Prototype šablonas


class ShapeProtoType{

private static Hashtable<String, Shape> shapeMap
= new Hashtable<String, Shape>();

public static Shape getShape(String shapeId) {
Shape cachedShape = shapeMap.get(shapeId);
return (Shape) cachedShape.clone();
}
public static void loadCache() {
Circle circle = new Circle();
circle.setId("1");
shapeMap.put(circle.getId(),circle);

      Square square = new Square();
      square.setId("2");
      shapeMap.put(square.getId(),square);

      Rectangle rectangle = new Rectangle();
      rectangle.setId("3");
      shapeMap.put(rectangle.getId(),rectangle);
}
}
public class Main{
public static void main(String[] args) {
ShapeProtoType.loadCache();

      Shape clonedShape = (Shape) ShapeProtoType.getShape("1");
      System.out.println("Shape : " + clonedShape.getType());    

      Shape clonedShape2 = (Shape) ShapeProtoType.getShape("2");
      System.out.println("Shape : " + clonedShape2.getType());    

      Shape clonedShape3 = (Shape) ShapeProtoType.getShape("3");
      System.out.println("Shape : " + clonedShape3.getType());    
}
}
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

# Builder šablonas


// Design pattern Builder (projektavimo šablonas skirtas objektams kurti)

class Computer{

    private String name;
    private String os;
    private int ram;
    private String processor;
    private int powerSupply;

    @Override
    public String toString() {
        return "Computer{" + "name='" + name + '\'' + ", os='" + os + '\'' + ", ram=" + ram + ", processor='" + processor + '\'' + ", powerSupply=" + powerSupply + '}';
    }
    // Vidinė klasė Builder
    public static class Builder {

        private Computer computer = new Computer();

        public Builder name(String name){
            computer.name = name;
            return this;
        }
        public Builder os(String os){
            computer.os = os;
            return this;
        }
        public Builder ram(int ram){
            computer.ram = ram;
            return this;
        }
        public Builder processor(String processor){
            computer.processor = processor;
            return this;
        }
        public Builder powerSupply(int powerSupply){
            computer.powerSupply = powerSupply;
            return this;
        }

        public Computer build(){
            // Šiame metode atliekame tikrinimą pagal tam tikrus kriterijus.
            // Jeigu norite galite apbriboti nuo tuščių objekto laukų, priklausomai pagal poreikį.
            // Apribojimai nėra privalomi!!!
            if(computer.powerSupply == 0){
                System.out.println(computer+"\033[31m\033"+" - Atsiprašome bet kompiuteris be energijos bloko negali egzistuoti!"+"\033[0m");
            }
            else{
                System.out.println(computer+"\033[31;1m\033[0m, \033[32;1;2m"+" - Kompiuteris sėkmingai surinktas!!!"+"\033[0m");
                return computer;
            }
            return null;
        }

    }
}

public class Main {
public static void main(String[] args) {

        Computer dell = new Computer.Builder()
                .name("Dell Inspiron N7110")
                .os("Microsoft Windows")
                .processor("Intel CORE I7")
                .ram(16)
                .build();

        Computer lenovo = new Computer.Builder()
                .name("Lenovo Y700")
                .os("Linux")
                .processor("Intel CORE I7")
                .ram(8)
                .powerSupply(150)
                .build();

        Computer msi = new Computer.Builder()
                .powerSupply(180)
                .ram(16)
                .name("MSI GE72VR")
                .processor("Intel CORE I7")
                .build();
    }
}
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
Last update: September 20, 2021 06:35
Contributors: Marius Gžegoževskis