-
Notifications
You must be signed in to change notification settings - Fork 0
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
public abstract class Animal { | ||
|
||
RunBehavior runBehavior; | ||
SwimBehavior swimBehavior; | ||
|
||
public void performSwim(){ | ||
swimBehavior.swim(); | ||
} | ||
|
||
public void performRun(int distance){ | ||
runBehavior.run(distance); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
public class AnimalFactory { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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()); | ||
} | ||
} |
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; | ||
} | ||
} |
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"); | ||
} | ||
} | ||
} |
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; | ||
} | ||
} |
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"); | ||
} | ||
} | ||
} |
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(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
public interface RunBehavior { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Можно было пойти еще дальше и сделать один интерфейс чисто для действия с методом |
||
public void run(int distance); | ||
} |
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"); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
public interface SwimBehavior { | ||
public void swim(); | ||
} |
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"); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Делать доступ по умолчанию - так себе идея. Лучше приватные поля, и передавать стратегии в конструктор