Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lesson 6 homework #5

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions lesson2/src/main/java/Animal.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
public abstract class Animal {

RunBehavior runBehavior;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Делать доступ по умолчанию - так себе идея. Лучше приватные поля, и передавать стратегии в конструктор

SwimBehavior swimBehavior;

public void performSwim(){
swimBehavior.swim();
}

public void performRun(int distance){
runBehavior.run(distance);
}
}
14 changes: 14 additions & 0 deletions lesson2/src/main/java/AnimalFactory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
public class AnimalFactory {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Строго говоря, у вас получилась не совсем factory :)


public static void getAllDogs(){
System.out.println(Dog.getAllDogs());
}

public static void getAllCats(){
System.out.println(Cat.getAllCats());
}

public static void getAllAnimals(){
System.out.println(Dog.getAllDogs() + Cat.getAllCats());
}
}
13 changes: 13 additions & 0 deletions lesson2/src/main/java/Cat.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
public class Cat extends Animal{
private static int counter = 0;

public Cat(){
swimBehavior = new SwimNoWay();
runBehavior = new CatRunning();
counter ++;
}

public static int getAllCats(){
return counter;
}
}
11 changes: 11 additions & 0 deletions lesson2/src/main/java/CatRunning.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class CatRunning implements RunBehavior{

@Override
public void run(int distance) {
if (distance<=200){
System.out.println("i'm running on " + distance + " meters");
}else{
System.out.println("i can't running on this distance");
}
}
}
13 changes: 13 additions & 0 deletions lesson2/src/main/java/Dog.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
public class Dog extends Animal{
private static int counter = 0;

public Dog(){
swimBehavior = new Swim();
runBehavior = new DogRunning();
counter ++;
}

public static int getAllDogs(){
return counter;
}
}
10 changes: 10 additions & 0 deletions lesson2/src/main/java/DogRunning.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
public class DogRunning implements RunBehavior{
@Override
public void run(int distance) {
if (distance<=500){
System.out.println("i'm running on " + distance + " meters");
}else{
System.out.println("i can't eun on this distance");
}
}
}
13 changes: 13 additions & 0 deletions lesson2/src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
public class Main {

public static void main(String[] args) {
Cat stepa = new Cat();
stepa.performRun(100);
Dog lord = new Dog();
lord.performRun(300);

AnimalFactory.getAllDogs();
AnimalFactory.getAllCats();
AnimalFactory.getAllAnimals();
}
}
3 changes: 3 additions & 0 deletions lesson2/src/main/java/RunBehavior.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
public interface RunBehavior {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Можно было пойти еще дальше и сделать один интерфейс чисто для действия с методом action, а от него уже наследоваться с конкретными стратегиями для котоплавания и собакобегания :)

public void run(int distance);
}
6 changes: 6 additions & 0 deletions lesson2/src/main/java/Swim.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
public class Swim implements SwimBehavior{

public void swim(){
System.out.println("i'm swimming");
}
}
3 changes: 3 additions & 0 deletions lesson2/src/main/java/SwimBehavior.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
public interface SwimBehavior {
public void swim();
}
5 changes: 5 additions & 0 deletions lesson2/src/main/java/SwimNoWay.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
public class SwimNoWay implements SwimBehavior{
public void swim(){
System.out.println("i can't swim");
}
}