instanceof
This commit is contained in:
@@ -1,10 +1,9 @@
|
|||||||
package com.baeldung.enumallt;
|
package com.baeldung.enumallt;
|
||||||
|
|
||||||
public class Anatotitan extends Dinosaur{
|
public class Anatotitan extends Dinosaur {
|
||||||
|
|
||||||
String behavior() {
|
String behavior() {
|
||||||
return "very aggressive";
|
return "very aggressive";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,8 +1,7 @@
|
|||||||
package com.baeldung.enumallt;
|
package com.baeldung.enumallt;
|
||||||
|
|
||||||
public class Dinosaur {
|
public class Dinosaur {
|
||||||
|
|
||||||
|
|
||||||
DinosaurEnum dinosaur;
|
DinosaurEnum dinosaur;
|
||||||
|
|
||||||
public DinosaurEnum getDinosaur() {
|
public DinosaurEnum getDinosaur() {
|
||||||
@@ -12,6 +11,5 @@ public class Dinosaur {
|
|||||||
public void setDinosaur(DinosaurEnum dinosaur) {
|
public void setDinosaur(DinosaurEnum dinosaur) {
|
||||||
this.dinosaur = dinosaur;
|
this.dinosaur = dinosaur;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
package com.baeldung.enumallt;
|
package com.baeldung.enumallt;
|
||||||
|
|
||||||
public enum DinosaurEnum {
|
public enum DinosaurEnum {
|
||||||
Anatotitan {
|
Anatotitan {
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -1,10 +1,9 @@
|
|||||||
package com.baeldung.enumallt;
|
package com.baeldung.enumallt;
|
||||||
|
|
||||||
public class Euraptor extends Dinosaur{
|
public class Euraptor extends Dinosaur {
|
||||||
|
|
||||||
String behavior() {
|
String behavior() {
|
||||||
return "calm";
|
return "calm";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,11 +1,9 @@
|
|||||||
package com.baeldung.examplesetup;
|
package com.baeldung.examplesetup;
|
||||||
|
|
||||||
public class Anatotitan extends Dinosaur{
|
public class Anatotitan extends Dinosaur {
|
||||||
|
|
||||||
|
|
||||||
public String behavior() {
|
public String behavior() {
|
||||||
return "very aggressive";
|
return "very aggressive";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,8 +1,5 @@
|
|||||||
package com.baeldung.examplesetup;
|
package com.baeldung.examplesetup;
|
||||||
|
|
||||||
public class Dinosaur {
|
public class Dinosaur {
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,12 +1,9 @@
|
|||||||
package com.baeldung.examplesetup;
|
package com.baeldung.examplesetup;
|
||||||
|
|
||||||
public class Euraptor extends Dinosaur{
|
public class Euraptor extends Dinosaur {
|
||||||
|
|
||||||
|
|
||||||
public String behavior() {
|
public String behavior() {
|
||||||
return "calm";
|
return "calm";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,12 +1,9 @@
|
|||||||
package com.baeldung.getclassalt;
|
package com.baeldung.getclassalt;
|
||||||
|
|
||||||
public class Anatotitan extends Dinosaur{
|
public class Anatotitan extends Dinosaur {
|
||||||
|
|
||||||
|
|
||||||
public String behavior() {
|
public String behavior() {
|
||||||
return "very aggressive";
|
return "very aggressive";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,10 +1,9 @@
|
|||||||
package com.baeldung.getclassalt;
|
package com.baeldung.getclassalt;
|
||||||
|
|
||||||
public class Dinosaur {
|
public class Dinosaur {
|
||||||
|
|
||||||
/*String behavior() {
|
/*
|
||||||
return "default sound";
|
* String behavior() { return "default sound"; }
|
||||||
}*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,13 +1,9 @@
|
|||||||
package com.baeldung.getclassalt;
|
package com.baeldung.getclassalt;
|
||||||
|
|
||||||
public class Euraptor extends Dinosaur{
|
public class Euraptor extends Dinosaur {
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public String behavior() {
|
public String behavior() {
|
||||||
return "calm";
|
return "calm";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,10 +1,9 @@
|
|||||||
package com.baeldung.mapalt;
|
package com.baeldung.mapalt;
|
||||||
|
|
||||||
public class Anatotitan extends Dinosaur {
|
public class Anatotitan extends Dinosaur {
|
||||||
|
|
||||||
public String behavior() {
|
public String behavior() {
|
||||||
return "very aggressive";
|
return "very aggressive";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,11 +1,9 @@
|
|||||||
package com.baeldung.mapalt;
|
package com.baeldung.mapalt;
|
||||||
|
|
||||||
public class Dinosaur {
|
public class Dinosaur {
|
||||||
|
|
||||||
public String behavior() {
|
public String behavior() {
|
||||||
return "default sound";
|
return "default sound";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,12 +1,9 @@
|
|||||||
package com.baeldung.mapalt;
|
package com.baeldung.mapalt;
|
||||||
|
|
||||||
public class Euraptor extends Dinosaur{
|
public class Euraptor extends Dinosaur {
|
||||||
|
|
||||||
|
|
||||||
public String behavior() {
|
public String behavior() {
|
||||||
return "calm";
|
return "calm";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,10 +1,9 @@
|
|||||||
package com.baeldung.patternmatching;
|
package com.baeldung.patternmatching;
|
||||||
|
|
||||||
public class Anatotitan extends Dinosaur {
|
public class Anatotitan extends Dinosaur {
|
||||||
|
|
||||||
public String behavior() {
|
public String behavior() {
|
||||||
return "very aggressive";
|
return "very aggressive";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,11 +1,9 @@
|
|||||||
package com.baeldung.patternmatching;
|
package com.baeldung.patternmatching;
|
||||||
|
|
||||||
public class Dinosaur {
|
public class Dinosaur {
|
||||||
|
|
||||||
String behavior() {
|
String behavior() {
|
||||||
return "default sound";
|
return "default sound";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,12 +1,9 @@
|
|||||||
package com.baeldung.patternmatching;
|
package com.baeldung.patternmatching;
|
||||||
|
|
||||||
public class Euraptor extends Dinosaur{
|
public class Euraptor extends Dinosaur {
|
||||||
|
|
||||||
|
|
||||||
public String behavior() {
|
public String behavior() {
|
||||||
return "calm";
|
return "calm";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,11 +1,10 @@
|
|||||||
package com.baeldung.polymorphism;
|
package com.baeldung.polymorphism;
|
||||||
|
|
||||||
public class Anatotitan extends Dinosaur{
|
public class Anatotitan extends Dinosaur {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String behavior() {
|
public String behavior() {
|
||||||
return "very aggressive";
|
return "very aggressive";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,10 +1,9 @@
|
|||||||
package com.baeldung.polymorphism;
|
package com.baeldung.polymorphism;
|
||||||
|
|
||||||
public class Dinosaur {
|
public class Dinosaur {
|
||||||
|
|
||||||
public String behavior() {
|
public String behavior() {
|
||||||
return "default sound";
|
return "default sound";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,17 +1,14 @@
|
|||||||
package com.baeldung.visitorspattern;
|
package com.baeldung.visitorspattern;
|
||||||
|
|
||||||
public class Anatotitan implements Dino{
|
public class Anatotitan implements Dino {
|
||||||
|
|
||||||
|
|
||||||
String behavior() {
|
String behavior() {
|
||||||
return "very aggressive";
|
return "very aggressive";
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public
|
public String behavior2(Visitor dinobehave) {
|
||||||
String behavior2(Visitor dinobehave) {
|
|
||||||
return dinobehave.visit(this);
|
return dinobehave.visit(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
package com.baeldung.visitorspattern;
|
package com.baeldung.visitorspattern;
|
||||||
|
|
||||||
public class DinoImpl implements Visitor {
|
public class DinoVisitorImpl implements Visitor {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String visit(Anatotitan anatotitan) {
|
public String visit(Anatotitan anatotitan) {
|
||||||
@@ -9,8 +9,7 @@ public class DinoImpl implements Visitor {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String visit(Euraptor euraptor) {
|
public String visit(Euraptor euraptor) {
|
||||||
return euraptor.behavior();
|
return euraptor.behavior();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -1,18 +1,13 @@
|
|||||||
package com.baeldung.visitorspattern;
|
package com.baeldung.visitorspattern;
|
||||||
|
|
||||||
public class Euraptor implements Dino{
|
public class Euraptor implements Dino {
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
String behavior() {
|
String behavior() {
|
||||||
return "calm";
|
return "calm";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public
|
public String behavior2(Visitor dinobehave) {
|
||||||
String behavior2(Visitor dinobehave) {
|
|
||||||
return dinobehave.visit(this);
|
return dinobehave.visit(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,8 +1,9 @@
|
|||||||
package com.baeldung.visitorspattern;
|
package com.baeldung.visitorspattern;
|
||||||
|
|
||||||
public interface Visitor {
|
public interface Visitor {
|
||||||
|
|
||||||
String visit(Anatotitan anatotitan);
|
String visit(Anatotitan anatotitan);
|
||||||
|
|
||||||
String visit(Euraptor euraptor);
|
String visit(Euraptor euraptor);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,11 +6,10 @@ import org.junit.Test;
|
|||||||
|
|
||||||
import com.baeldung.enumallt.*;
|
import com.baeldung.enumallt.*;
|
||||||
|
|
||||||
|
public class EnumUnitTest {
|
||||||
public class EnumAltTest {
|
|
||||||
@Test
|
@Test
|
||||||
public void testAnatotitan() {
|
public void testAnatotitan() {
|
||||||
|
|
||||||
Dinosaur dinosaur = new Dinosaur();
|
Dinosaur dinosaur = new Dinosaur();
|
||||||
dinosaur.setDinosaur(DinosaurEnum.Euraptor);
|
dinosaur.setDinosaur(DinosaurEnum.Euraptor);
|
||||||
|
|
||||||
@@ -25,5 +24,4 @@ public class EnumAltTest {
|
|||||||
assertEquals("Aggressive ", dinosaur.getDinosaur().behaviour());
|
assertEquals("Aggressive ", dinosaur.getDinosaur().behaviour());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -6,7 +6,7 @@ import org.junit.Test;
|
|||||||
|
|
||||||
import com.baeldung.examplesetup.*;
|
import com.baeldung.examplesetup.*;
|
||||||
|
|
||||||
public class ExampleTest {
|
public class ExampleSetupUnitTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testAnatotitan() {
|
public void testAnatotitan() {
|
||||||
@@ -5,7 +5,7 @@ import org.junit.Test;
|
|||||||
|
|
||||||
import com.baeldung.getclassalt.*;
|
import com.baeldung.getclassalt.*;
|
||||||
|
|
||||||
public class getClassTest {
|
public class GetClassUnitTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testAnatotitan() {
|
public void testAnatotitan() {
|
||||||
@@ -9,21 +9,15 @@ import org.junit.Test;
|
|||||||
|
|
||||||
import com.baeldung.mapalt.*;
|
import com.baeldung.mapalt.*;
|
||||||
|
|
||||||
public class MapTest {
|
public class MapUnitTest {
|
||||||
|
|
||||||
|
Map<Class<? extends Dinosaur>, String> response = new HashMap<Class<? extends Dinosaur>, String>();
|
||||||
|
Dinosaur anatotitan = new Anatotitan();
|
||||||
Map<Class<? extends Dinosaur>, String> response =
|
Dinosaur euraptor = new Euraptor();
|
||||||
new HashMap<Class<? extends Dinosaur>, String>();
|
|
||||||
Dinosaur anatotitan = new Anatotitan();
|
|
||||||
Dinosaur euraptor = new Euraptor();
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testAnatotitan() {
|
public void testAnatotitan() {
|
||||||
response.put(Anatotitan.class, anatotitan.behavior());
|
response.put(Anatotitan.class, anatotitan.behavior());
|
||||||
|
|
||||||
|
|
||||||
assertEquals("very aggressive", response.get(Anatotitan.class));
|
assertEquals("very aggressive", response.get(Anatotitan.class));
|
||||||
}
|
}
|
||||||
@@ -31,11 +25,8 @@ public class MapTest {
|
|||||||
@Test
|
@Test
|
||||||
public void testEuraptor() {
|
public void testEuraptor() {
|
||||||
response.put(Euraptor.class, euraptor.behavior());
|
response.put(Euraptor.class, euraptor.behavior());
|
||||||
|
|
||||||
|
|
||||||
assertEquals("calm", response.get(Euraptor.class));
|
assertEquals("calm", response.get(Euraptor.class));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -6,7 +6,7 @@ import org.junit.Test;
|
|||||||
|
|
||||||
import com.baeldung.patternmatching.*;
|
import com.baeldung.patternmatching.*;
|
||||||
|
|
||||||
public class patternTest {
|
public class PatternMatchingUnitTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testAnatotitan() {
|
public void testAnatotitan() {
|
||||||
@@ -7,21 +7,19 @@ import org.junit.Test;
|
|||||||
|
|
||||||
import com.baeldung.polymorphism.*;
|
import com.baeldung.polymorphism.*;
|
||||||
|
|
||||||
public class polymorphsimTest {
|
public class PolymorphismUnitTest {
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testAnatotitan() {
|
public void testAnatotitan() {
|
||||||
|
|
||||||
assertEquals("very aggressive", dinoBehavior(new Anatotitan()));
|
assertEquals("very aggressive", dinoBehavior(new Anatotitan()));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testEuraptor() {
|
public void testEuraptor() {
|
||||||
assertEquals("calm", dinoBehavior(new Euraptor()));
|
assertEquals("calm", dinoBehavior(new Euraptor()));
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String dinoBehavior(Dinosaur dinosaur) {
|
public static String dinoBehavior(Dinosaur dinosaur) {
|
||||||
return dinosaur.behavior();
|
return dinosaur.behavior();
|
||||||
}
|
}
|
||||||
@@ -6,25 +6,23 @@ import org.junit.Test;
|
|||||||
|
|
||||||
import com.baeldung.visitorspattern.*;
|
import com.baeldung.visitorspattern.*;
|
||||||
|
|
||||||
public class VisitorsPatternTest {
|
public class VisitorsPatternUnitTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testAnatotitan() {
|
public void testAnatotitan() {
|
||||||
|
|
||||||
|
|
||||||
assertEquals("very aggressive", visitorsPatter((Dino) new Anatotitan()));
|
assertEquals("very aggressive", visitorsPatter((Dino) new Anatotitan()));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testEuraptor() {
|
public void testEuraptor() {
|
||||||
|
|
||||||
|
|
||||||
assertEquals("calm", visitorsPatter((Dino) new Euraptor()));
|
assertEquals("calm", visitorsPatter((Dino) new Euraptor()));
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String visitorsPatter(Dino dinosaur) {
|
public static String visitorsPatter(Dino dinosaur) {
|
||||||
Visitor visitor = new DinoImpl();
|
Visitor visitor = new DinoVisitorImpl();
|
||||||
|
|
||||||
return dinosaur.behavior2(visitor);
|
return dinosaur.behavior2(visitor);
|
||||||
}
|
}
|
||||||
|
|
||||||
Reference in New Issue
Block a user