├── .gitignore └── src └── pl └── javadevmatt ├── builder ├── classic │ ├── CarBuilder.java │ ├── CarDirector.java │ ├── Client.java │ ├── MaluchBuilder.java │ ├── RaceCarBuilder.java │ └── entities │ │ ├── Car.java │ │ ├── Engine.java │ │ └── Tires.java └── simple │ ├── Cat.java │ ├── Client.java │ └── ProblemCat.java └── observer ├── Client.java ├── YTChannel.java ├── YTUser.java └── interfaces ├── Observer.java └── Subject.java /.gitignore: -------------------------------------------------------------------------------- 1 | *.class 2 | .classpath 3 | .project 4 | -------------------------------------------------------------------------------- /src/pl/javadevmatt/builder/classic/CarBuilder.java: -------------------------------------------------------------------------------- 1 | package pl.javadevmatt.builder.classic; 2 | 3 | import pl.javadevmatt.builder.classic.entities.Car; 4 | 5 | public interface CarBuilder { 6 | 7 | public void buildTires(); 8 | 9 | public void buildEngine(); 10 | 11 | public Car getCar(); 12 | 13 | } 14 | -------------------------------------------------------------------------------- /src/pl/javadevmatt/builder/classic/CarDirector.java: -------------------------------------------------------------------------------- 1 | package pl.javadevmatt.builder.classic; 2 | 3 | import pl.javadevmatt.builder.classic.entities.Car; 4 | 5 | public class CarDirector { 6 | 7 | private CarBuilder carBuilder; 8 | 9 | public CarDirector(CarBuilder carBuilder){ 10 | this.carBuilder = carBuilder; 11 | } 12 | 13 | public void makeCar(){ 14 | carBuilder.buildTires(); 15 | carBuilder.buildEngine(); 16 | } 17 | 18 | public Car getCar(){ 19 | return this.carBuilder.getCar(); 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /src/pl/javadevmatt/builder/classic/Client.java: -------------------------------------------------------------------------------- 1 | package pl.javadevmatt.builder.classic; 2 | 3 | import pl.javadevmatt.builder.classic.entities.Car; 4 | 5 | public class Client { 6 | 7 | public static void main(String[] args){ 8 | 9 | CarBuilder carBuilder = new RaceCarBuilder(); 10 | CarDirector carDirector = new CarDirector(carBuilder); 11 | carDirector.makeCar(); 12 | 13 | Car car = carDirector.getCar(); 14 | System.out.println(car); 15 | } 16 | 17 | 18 | } 19 | -------------------------------------------------------------------------------- /src/pl/javadevmatt/builder/classic/MaluchBuilder.java: -------------------------------------------------------------------------------- 1 | package pl.javadevmatt.builder.classic; 2 | 3 | import pl.javadevmatt.builder.classic.entities.Car; 4 | import pl.javadevmatt.builder.classic.entities.Engine; 5 | import pl.javadevmatt.builder.classic.entities.Tires; 6 | 7 | public class MaluchBuilder implements CarBuilder{ 8 | 9 | private Car car; 10 | 11 | public MaluchBuilder(){ 12 | this.car = new Car(); 13 | } 14 | 15 | @Override 16 | public void buildTires() { 17 | Tires tire = new Tires(); 18 | tire.setDurability(100); 19 | tire.setType("Maluch Tires"); 20 | 21 | car.setTires(tire); 22 | } 23 | 24 | @Override 25 | public void buildEngine() { 26 | Engine engine = new Engine(); 27 | engine.setType("Maluch Engine"); 28 | 29 | car.setEngine(engine); 30 | } 31 | 32 | @Override 33 | public Car getCar() { 34 | return car; 35 | } 36 | 37 | } 38 | -------------------------------------------------------------------------------- /src/pl/javadevmatt/builder/classic/RaceCarBuilder.java: -------------------------------------------------------------------------------- 1 | package pl.javadevmatt.builder.classic; 2 | 3 | import pl.javadevmatt.builder.classic.entities.Car; 4 | import pl.javadevmatt.builder.classic.entities.Engine; 5 | import pl.javadevmatt.builder.classic.entities.Tires; 6 | 7 | public class RaceCarBuilder implements CarBuilder{ 8 | 9 | private Car car; 10 | 11 | public RaceCarBuilder(){ 12 | this.car = new Car(); 13 | } 14 | 15 | @Override 16 | public void buildEngine() { 17 | Engine engine = new Engine(); 18 | engine.setType("v8"); 19 | 20 | car.setEngine(engine); 21 | } 22 | 23 | @Override 24 | public void buildTires() { 25 | Tires tire = new Tires(); 26 | tire.setDurability(50); 27 | tire.setType("Slicks"); 28 | 29 | car.setTires(tire); 30 | } 31 | 32 | @Override 33 | public Car getCar() { 34 | return car; 35 | } 36 | 37 | } 38 | -------------------------------------------------------------------------------- /src/pl/javadevmatt/builder/classic/entities/Car.java: -------------------------------------------------------------------------------- 1 | package pl.javadevmatt.builder.classic.entities; 2 | 3 | 4 | public class Car { 5 | 6 | private Tires tires; 7 | 8 | private Engine engine; 9 | 10 | public Tires getTires() { 11 | return tires; 12 | } 13 | 14 | public void setTires(Tires tires) { 15 | this.tires = tires; 16 | } 17 | 18 | public Engine getEngine() { 19 | return engine; 20 | } 21 | 22 | public void setEngine(Engine engine) { 23 | this.engine = engine; 24 | } 25 | 26 | @Override 27 | public String toString() { 28 | return "Car [tires=" + tires + ", engine=" + engine + "]"; 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /src/pl/javadevmatt/builder/classic/entities/Engine.java: -------------------------------------------------------------------------------- 1 | package pl.javadevmatt.builder.classic.entities; 2 | 3 | public class Engine { 4 | 5 | // v8, v6 6 | private String type; 7 | 8 | public String getType() { 9 | return type; 10 | } 11 | 12 | public void setType(String type) { 13 | this.type = type; 14 | } 15 | 16 | @Override 17 | public String toString() { 18 | return "Engine [type=" + type + "]"; 19 | } 20 | 21 | 22 | } 23 | -------------------------------------------------------------------------------- /src/pl/javadevmatt/builder/classic/entities/Tires.java: -------------------------------------------------------------------------------- 1 | package pl.javadevmatt.builder.classic.entities; 2 | 3 | public class Tires { 4 | 5 | private String type; 6 | 7 | private int durability; 8 | 9 | public String getType() { 10 | return type; 11 | } 12 | 13 | public void setType(String type) { 14 | this.type = type; 15 | } 16 | 17 | public int getDurability() { 18 | return durability; 19 | } 20 | 21 | public void setDurability(int durability) { 22 | this.durability = durability; 23 | } 24 | 25 | @Override 26 | public String toString() { 27 | return "Tires [type=" + type + ", durability=" + durability + "]"; 28 | } 29 | 30 | 31 | } 32 | -------------------------------------------------------------------------------- /src/pl/javadevmatt/builder/simple/Cat.java: -------------------------------------------------------------------------------- 1 | package pl.javadevmatt.builder.simple; 2 | 3 | public class Cat 4 | { 5 | private String name; 6 | private String color; 7 | private int age; 8 | private String owner; 9 | 10 | private Cat(final Builder builder) 11 | { 12 | this.name = builder.name; 13 | this.color = builder.color; 14 | this.age = builder.age; 15 | this.owner = builder.owner; 16 | } 17 | 18 | public String getName() 19 | { 20 | return name; 21 | } 22 | public String getColor() 23 | { 24 | return color; 25 | } 26 | public int getAge() 27 | { 28 | return age; 29 | } 30 | public String getOwner() 31 | { 32 | return owner; 33 | } 34 | 35 | public static class Builder 36 | { 37 | private final String name; // final because the name is required 38 | private String color; 39 | private int age; 40 | private String owner; 41 | 42 | public Builder(final String name) 43 | { 44 | this.name = name; 45 | } 46 | 47 | public Builder color(final String color) 48 | { 49 | this.color = color; 50 | return this; 51 | } 52 | 53 | public Builder age(final int age) 54 | { 55 | this.age = age; 56 | return this; 57 | } 58 | 59 | public Builder owner(final String owner) 60 | { 61 | this.owner = owner; 62 | return this; 63 | } 64 | 65 | public Cat build() 66 | { 67 | return new Cat(this); 68 | } 69 | 70 | } 71 | 72 | @Override 73 | public String toString() { 74 | return "Cat [name=" + name + ", color=" + color + ", age=" + age 75 | + ", owner=" + owner + "]"; 76 | } 77 | 78 | } 79 | -------------------------------------------------------------------------------- /src/pl/javadevmatt/builder/simple/Client.java: -------------------------------------------------------------------------------- 1 | package pl.javadevmatt.builder.simple; 2 | 3 | public class Client { 4 | 5 | public static void main(String[] args) { 6 | 7 | Cat cat = new Cat.Builder("Pusheen") 8 | .age(3) 9 | .color("Black") 10 | .owner("Matt") 11 | .build(); 12 | System.out.println(cat); 13 | 14 | ProblemCat problemCat = new ProblemCat("Pusheen", "Black", 3, "Matt"); 15 | System.out.println(problemCat); 16 | 17 | ProblemCat problemCat2 = new ProblemCat("Pusheen"); 18 | problemCat2.setColor("Black"); 19 | problemCat2.setAge(3); 20 | problemCat2.setOwner("Matt"); 21 | System.out.println(problemCat2); 22 | 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /src/pl/javadevmatt/builder/simple/ProblemCat.java: -------------------------------------------------------------------------------- 1 | package pl.javadevmatt.builder.simple; 2 | 3 | public class ProblemCat { 4 | 5 | private String name; 6 | private String color; 7 | private int age; 8 | private String owner; 9 | 10 | public ProblemCat(String name){ 11 | this.name = name; 12 | } 13 | 14 | public ProblemCat(String name, String color, int age, String owner) { 15 | this.name = name; 16 | this.color = color; 17 | this.age = age; 18 | this.owner = owner; 19 | } 20 | 21 | public String getName() { 22 | return name; 23 | } 24 | 25 | public void setName(String name) { 26 | this.name = name; 27 | } 28 | 29 | public String getColor() { 30 | return color; 31 | } 32 | 33 | public void setColor(String color) { 34 | this.color = color; 35 | } 36 | 37 | public int getAge() { 38 | return age; 39 | } 40 | 41 | public void setAge(int age) { 42 | this.age = age; 43 | } 44 | 45 | public String getOwner() { 46 | return owner; 47 | } 48 | 49 | public void setOwner(String owner) { 50 | this.owner = owner; 51 | } 52 | 53 | @Override 54 | public String toString() { 55 | return "Cat [name=" + name + ", color=" + color + ", age=" + age 56 | + ", owner=" + owner + "]"; 57 | } 58 | 59 | } 60 | -------------------------------------------------------------------------------- /src/pl/javadevmatt/observer/Client.java: -------------------------------------------------------------------------------- 1 | package pl.javadevmatt.observer; 2 | 3 | 4 | public class Client{ 5 | 6 | public static void main(String[] args) { 7 | 8 | YTChannel ytChannel = new YTChannel(); 9 | 10 | YTUser ytUser = new YTUser("Matt"); 11 | ytChannel.register(ytUser); 12 | 13 | /** 14 | * 15 | * 16 | * 17 | * 18 | */ 19 | 20 | ytChannel.publishNewVideo(); 21 | System.out.println("------------------"); 22 | 23 | YTUser ytUser2 = new YTUser("Patt"); 24 | ytChannel.register(ytUser2); 25 | ytChannel.publishNewVideo(); 26 | System.out.println("------------------"); 27 | 28 | ytChannel.unregister(ytUser); 29 | ytChannel.publishNewVideo(); 30 | System.out.println("------------------"); 31 | } 32 | 33 | } 34 | -------------------------------------------------------------------------------- /src/pl/javadevmatt/observer/YTChannel.java: -------------------------------------------------------------------------------- 1 | package pl.javadevmatt.observer; 2 | 3 | import java.util.ArrayList; 4 | 5 | import pl.javadevmatt.observer.interfaces.Observer; 6 | import pl.javadevmatt.observer.interfaces.Subject; 7 | 8 | public class YTChannel implements Subject{ 9 | 10 | private ArrayList observerList; 11 | 12 | public YTChannel() { 13 | observerList = new ArrayList<>(); 14 | } 15 | 16 | public void publishNewVideo(){ 17 | System.out.println("YTChannel: Publisching new video."); 18 | notifyObservers(); 19 | } 20 | 21 | @Override 22 | public void register(Observer o) { 23 | observerList.add(o); 24 | } 25 | 26 | @Override 27 | public void unregister(Observer o) { 28 | observerList.remove(o); 29 | } 30 | 31 | @Override 32 | public void notifyObservers() { 33 | for (Observer o : observerList){ 34 | o.update(); 35 | } 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /src/pl/javadevmatt/observer/YTUser.java: -------------------------------------------------------------------------------- 1 | package pl.javadevmatt.observer; 2 | 3 | import pl.javadevmatt.observer.interfaces.Observer; 4 | 5 | public class YTUser implements Observer{ 6 | 7 | private String name; 8 | private int videosToWatch; 9 | 10 | public YTUser(String name) { 11 | this.name = name; 12 | videosToWatch = 0; 13 | } 14 | 15 | @Override 16 | public void update() { 17 | videosToWatch++; 18 | System.out.println("Hey " + name + "! There is a new video! You have " + videosToWatch + " videos to watch."); 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /src/pl/javadevmatt/observer/interfaces/Observer.java: -------------------------------------------------------------------------------- 1 | package pl.javadevmatt.observer.interfaces; 2 | 3 | public interface Observer { 4 | 5 | void update(); 6 | 7 | } 8 | -------------------------------------------------------------------------------- /src/pl/javadevmatt/observer/interfaces/Subject.java: -------------------------------------------------------------------------------- 1 | package pl.javadevmatt.observer.interfaces; 2 | 3 | public interface Subject { 4 | 5 | void register(Observer o); 6 | void unregister(Observer o); 7 | void notifyObservers(); 8 | 9 | } 10 | --------------------------------------------------------------------------------