Java
/
Design Patterns
- 1 Basics 9
-
Classes S
-
Objects S
-
Arrays S
-
Variables S
-
Loops S
-
Numbers S
-
Strings S
-
Exceptions S
-
Regexp S
- 2 OOP 9
-
Inheritance
-
Polymorphism
-
Static S
-
Abstract
-
Interfaces
-
Constructors S
-
Packages
-
Nested Classes
-
Final
- 3 Compiler 2
-
Sublime Text S
-
Apache Ant
- 4 Collections 8
-
Lists
-
Comparable S
-
Sets
-
Maps
-
Generics
-
Properties
-
Streams
-
Json
- 5 Threads 4
-
Create Thread S
-
Sleep
-
Lock
-
Scheduler
- 6 Design Patterns 4
-
Singleton
-
Observer
-
Strategy
-
Mediator
- 7 Swing 12
-
Frame
-
Panel
-
Listener
-
Combo Box
-
Label
-
Image
-
Menu
-
Table
-
Layout
-
Drawing
-
Timer
-
Designer
- 8 I/O 7
-
Streams IO
-
Socket
-
Watching Files
-
Mail
-
Logger
-
Clipboard
-
Encrypt S
- 9 Effective 7
-
Constructors S
-
Dependency Injection
-
Composition
-
Interfaces Default
-
Import Static S
-
Enums
-
Lambdas
- 10 Junit 5
-
About Junit S
-
Test Case
-
Suite Test
-
Annotations
-
Exceptions
- 11 Lambdas 7
-
Expressions S
-
Functional Interfaces
-
Streams
-
Common Operations
-
Default Methods
-
Static Methods S
-
Single Responsibility
- 12 JavaFX 6
-
Openjfx
-
Scene Builder
-
First App
-
Jar Archive
-
On Action
-
Change Listener
- 13 Maven 4
-
Demo
-
Spring Boot
-
Junit
-
Guava
- 14 Spring Boot 13
-
Quick start S
-
Rest service S
-
Consuming rest S
-
Templates S
-
Security auth S
-
Command line S
-
Scheduled task S
-
Ajax S
-
Jdbc mysql S
-
Encrypt password S
-
Https S
-
Jwt S
-
Post request S
R
Q
Strategy
Used when you need to dynamically change an object behavior at run time. This pattern separates behavior from super and subclasses. It allows you to eliminate code duplication.
code
/**
* Using just interfaces ...
* we can't change the behavior at runtime
*/
class App {
public static void main(String[] args) {
Master m = new Master();
m.name = "Socrate";
m.speak();
Pupil p = new Pupil();
p.name = "Alcibiades";
p.speak();
// after that ...
// we can't change speak() behavior at runtime
}
}
class Master implements Speak {
public String name;
public void speak() {
System.out.println(name + ": I can raise questions ...");
}
}
class Pupil implements Speak {
public String name;
public void speak() {
System.out.println(name + ": I can answer!");
}
}
interface Speak {
public void speak();
}
// Socrate: I can raise questions ...
// Alcibiades: I can answer!
With Strategy Pattern we use an instance variable (composition technique).
It allows us to change the capabilities of objects at run time!
code
class Alcibiades {
public static void main(String[] args) {
Master m = new Master("Socrate");
m.type = new AskCapable();
m.speak();
Pupil p = new Pupil("Alcibiades");
p.type = new AskIncapable();
p.speak();
// now, we are able ...
// to change the behavior type at runtime
p.type = new AskCapable();
p.speak();
}
}
class Master extends Human {
public Master(String n) {
name = n;
}
}
class Pupil extends Human {
public Pupil(String n) {
name = n;
}
}
/**
* The parent abstract class ...
* doesn't konw what the behavior type is.
* It just knows that it is available to its subclasses
*/
abstract class Human {
String name;
Speak type;
public void speak() {
System.out.print(name + ": ");
type.speak(); // Look Here
}
}
class AskCapable implements Speak {
public void speak() {
System.out.println("I can raise questions ...");
}
}
class AskIncapable implements Speak {
public void speak() {
System.out.println("I can answer!");
}
}
interface Speak {
public void speak();
}
// Socrate: I can raise questions ...
// Alcibiades: I can answer!
// Alcibiades: I can raise questions ...
code
class TheRings {
public static void main(String[] args) {
Wizard w1 = new Wizard("Saruman");
Wizard w2 = new Wizard("Gandalf");
Hobbit h1 = new Hobbit("Frodo");
w1.type = new Fly(); // Saruman can Fly
w2.type = new Ride(); // Gandalf can Ride
h1.type = new Run(); // Frodo can Run
int level = 1;
while (level <= 3) {
System.out.println("\nLevel " + level);
if (level == 2) {
h1.type = new Disappear(); // Frodo can Disappear
}
if (level == 3) {
w2.type = new Fly(); // Gandalf can Fly
w1.type = new Run(); // Saruman must Run :)
}
w1.display(); w1.move();
w2.display(); w2.move();
h1.display(); h1.move();
level = level + 1;
}
}
}
// Items
class Wizard extends Item {
String name;
public Wizard(String n) {
name = n;
}
public void display() {
System.out.print(name);
}
}
class Hobbit extends Item {
String name;
public Hobbit(String n) {
name = n;
}
public void display() {
System.out.print(name);
}
}
abstract class Item {
Move type;
public void move() {
type.move();
}
abstract void display();
}
// Strategies
interface Move {
public void move();
}
class Fly implements Move {
public void move() {
System.out.print(" (Fly) ");
}
}
class Ride implements Move {
public void move() {
System.out.print(" (Ride) ");
}
}
class Run implements Move {
public void move() {
System.out.print(" (Run) ");
}
}
class Disappear implements Move {
public void move() {
System.out.print(" (Disappear) ");
}
}
/*
Level 1
Saruman (Fly) Gandalf (Ride) Frodo (Run)
Level 2
Saruman (Fly) Gandalf (Ride) Frodo (Disappear)
Level 3
Saruman (Run) Gandalf (Fly) Frodo (Disappear)
*/
Strategy v2.0 Alcibiades can't ask
Strategy v2.2 Alcibiades is wiser now, he can ask
➥ Questions