Last active
September 28, 2018 08:00
-
-
Save Pooh3Mobi/825cd1245a808e459d1d054c25cef51d to your computer and use it in GitHub Desktop.
Polymorphism Demo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.oop; | |
import java.text.ParseException; | |
import java.text.SimpleDateFormat; | |
import java.util.Date; | |
// abstract class を継承するやり方のデモコード | |
public class Demo { | |
// このコードはデモ用のコードです。書き方についてはチーム単位や会社単位でコーディング規約があると思いますので、 | |
// 製品コードに書くときはそちらを優先してください。 | |
void demo() { | |
try { | |
Karte dogKarte = makeKarte(new Dog("Taro", "2012/04/11")); | |
Karte catKarte = makeKarte(new Cat("Hana", "2016/08/07")); | |
} catch (ParseException e) { | |
e.printStackTrace(); | |
} | |
} | |
Karte makeKarte(Animal animal) { | |
String name = animal.name(); | |
Date birthday = animal.birthday(); | |
return new Karte(name, birthday); | |
} | |
private static final String DATE_FORMAT = "yyyy/MM/dd"; | |
abstract static class Animal { | |
private final String name; | |
private final Date birthday; | |
Animal(String name, Date birthday) { | |
this.name = name; | |
this.birthday = birthday; | |
} | |
String name() { | |
return name; | |
} | |
Date birthday() { | |
return birthday; | |
} | |
} | |
static class Dog extends Animal { | |
Dog(String name, String birthday) throws ParseException { | |
super( | |
name, | |
new SimpleDateFormat(DATE_FORMAT).parse(birthday) | |
); | |
} | |
} | |
static class Cat extends Animal { | |
Cat(String name, String birthday) throws ParseException { | |
super( | |
name, | |
new SimpleDateFormat(DATE_FORMAT).parse(birthday) | |
); | |
} | |
} | |
static class Karte { | |
public final String name; | |
public final Date birthday; | |
Karte(String name, Date birthday) { | |
this.name = name; | |
this.birthday = birthday; | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.oop; | |
import java.text.ParseException; | |
import java.text.SimpleDateFormat; | |
import java.util.Date; | |
// interfaceを継承するやり方のデモコード | |
public class InterfacedPolymorphismDemo { | |
// このコードはデモ用のコードです。書き方についてはチーム単位や会社単位でコーディング規約があると思いますので、 | |
// 製品コードに書くときはそちらを優先してください。 | |
void demo() { | |
try { | |
Karte dogKarte = makeKarte(new Dog("Taro", "2012/04/11")); | |
Karte catKarte = makeKarte(new Cat("Hana", "2016/08/07")); | |
} catch (ParseException e) { | |
e.printStackTrace(); | |
} | |
} | |
Karte makeKarte(Animal animal) { | |
String name = animal.name(); | |
Date birthday = animal.birthday(); | |
return new Karte(name, birthday); | |
} | |
private static final String DATE_FORMAT = "yyyy/MM/dd"; | |
interface Animal { | |
String name(); | |
Date birthday(); | |
} | |
static class Dog implements Animal { | |
private final String name; | |
private final Date birthday; | |
Dog(String name, String birthday) throws ParseException { | |
this.name = name; | |
this.birthday = new SimpleDateFormat(DATE_FORMAT).parse(birthday); | |
} | |
@Override | |
public String name() { | |
return name; | |
} | |
@Override | |
public Date birthday() { | |
return birthday; | |
} | |
} | |
static class Cat implements Animal { | |
private final String name; | |
private final Date birthday; | |
Cat(String name, String birthday) throws ParseException { | |
this.name = name; | |
this.birthday = new SimpleDateFormat(DATE_FORMAT).parse(birthday); | |
} | |
@Override | |
public String name() { | |
return name; | |
} | |
@Override | |
public Date birthday() { | |
return birthday; | |
} | |
} | |
static class Karte { | |
public final String name; | |
public final Date birthday; | |
Karte(String name, Date birthday) { | |
this.name = name; | |
this.birthday = birthday; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Abstract class はそれを継承して実装する人と、Abstract classを作成した人が常に同じであれば気にする必要はないが、そうではなく、途中で人が入れ替わると、Abstractの実装に強く依存した実装の知識が前提になるので、手が入れにくいコードが出来上がる。
なるべくinterfaceを使って継承を行う方が、人が入れ替わった時もすんなり変更できる。