OOP
手続き型(OP) ⇒ オブジェクト指向(OO) ⇒ インタフェース指向(OI) ⇒ アスペクト指向(AO) ⇒ サービス指向(SO)
オブジェクト指向に関する概念
Abstraction(抽象化)
Encapsulation(カプセル化)
Inheritance(継承): is-a関係
Composition(集約): has-a関係
Modularity(モジュール化)
Polymorphism(多態性)
OPP:Object Process Programming
OOP:Object Oriented Programming
AOP:Aspect Oriented Programming
SOA:Service Oriented Architecture
OPP
一連の流れに注目
関数で設計するのはOPPに限らない、OOPかもしれない
class X{
function create(){
if(A){
...
}
if(B){
...
}
}
}
★Inheritance vs. Composition
Inheritance means an IS-A relationship
Composition means a HAS-A relationship
Inheritance
@AllArgsConstructor
class Insect {
private int size;
private String color;
public void move() {
System.out.println("Move");
}
public void attack() {
move();
System.out.println("Attack");
}
}
class Bee extends Insect {
public Bee(int size, String color) {
super(size, color);
}
@Override
public void move() {
System.out.println("Fly");
}
@Override
public void attack() {
move();
super.attack();
}
}
Bee b = new Bee(1, "black");
b.attack();
Composition
interface Attack {
void move();
void attack();
}
@AllArgsConstructor
class AttackImpl implements Attack {
private String move;
private String attack;
@Override
public void move() {
System.out.println(move);
}
@Override
public void attack() {
move();
System.out.println(attack);
}
}
@AllArgsConstructor
class Insect {
private int size;
private String color;
}
class Bee extends Insect implements Attack {
private Attack attack;
public Bee(int size, String color, Attack attack) {
super(size, color);
this.attack = attack;
}
@Override
public void move() {
attack.move();
}
@Override
public void attack() {
attack.attack();
}
}
Bee b = new Bee(1, "black", new AttackImpl("fly", "attack"));
b.attack();
GRASP(General Responsibility Assignment Software Pattern)
1.Low Coupling 低耦合
2.High Cohesion 高内聚
3.Information Expert 信息专家
4.Creator 创建者
5.Controller 控制器
6.Polymorphism 多态
7.Pure Fabrication 纯虚构
8.Indirection 间接
9.Protected Variation 受保护变化
OOP
多態性・カプセル化・抽象化に注目
クラスで設計するのはOOPに限らない、OPPかもしれない
interface X{
create();
}
class A implements X{
function create(){
...
}
}
class B implements X{
function create(){
...
}
}