5
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

いろいろな共通化

Posted at

ゴール

"【Hello world】"と"【Wild Stars】"を出力する

いろいろな共通化

1. 共通化しない

public class Main {
    public static void main(String[] args) {
        HelloWorld hello = new HelloWorld();
        hello.greet();
        WildStars ws = new WildStars();
        ws.greet();
    }
}

public class HelloWorld {
    public void greet() {
        System.out.println("【Hello world】");
    }
}

public class WildStars {
    public void greet() {
        System.out.println("【Wild Stars】");
    }
}

2. 継承

public class Main {
    public static void main(String[] args) {
        HelloWorld hello = new HelloWorld();
        hello.greet();
        WildStars ws = new WildStars();
        ws.greet();
    }
}
public class BaseGreet {
	public String message;

	public void greet() {
		System.out.println("【" + message + "】");
	}
}
public class HelloWorld extends BaseGreet {
	public HelloWorld() {
		this.message = "Hello world";
	}
}
public class WildStars extends BaseGreet {
	public WildStars() {
		this.message = "Wild Stars";
	}
}

3. インタフェース

public class Main {
    public static void main(String[] args) {
        IGreet hello = new HelloWorld();
        hello.greet();
        IGreet ws = new WildStars();
        ws.greet();
    }
}
public interface IGreet {

	void greet();

}
public class HelloWorld implements IGreet {
	@Override
	public void greet() {
		System.out.println("【Hello world】");
	}
}
public class WildStars implements IGreet{
	@Override
	public void greet() {
		System.out.println("【Wild Stars】");
	}
}

4. ユーティリティー関数

public class Main {
    public static void main(String[] args) {
        GreetUtility.greet("Hello world");
        GreetUtility.greet("Wild Stars");
    }
}
public final class GreetUtility {
	public static void greet(String message) {
		System.out.println("【" + message + "】");
	}
}

5. 委譲

public class Main {
    public static void main(String[] args) {
        HelloWorld hello = new HelloWorld();
        hello.greet();
        WildStars ws = new WildStars();
        ws.greet();
    }
}
public final class GreetUtility {
	public static void greet(String message) {
		System.out.println("【" + message + "】");
	}
}
public class HelloWorld {
    public void greet() {
        GreetUtility.greet("Hello world");
    }
}
public class WildStars {
    public void greet() {
    	GreetUtility.greet("Wild stars");
    }
}

6. 関数生成

import java.util.function.Consumer;

public class Main {
    public static void main(String[] args) {
    	Consumer<String> greet = new Consumer<String>() {
			@Override
			public void accept(String message) {
				System.out.println("【" + message + "】");
			}
		};
    	greet.accept("Hello world");
    	greet.accept("Wild Stars");
    }
}
5
9
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?