一、 结构性模式
1.享元模式(Flyweight)
二、创建型模式
1.工厂方法模式(FactoryMethod)
interface Fruit {
void eat();
}
class Apple implements Fruit {
@Override
public void eat() {
System.out.println("吃苹果");
}
}
class Banana implements Fruit{
@Override
public void eat() {
System.out.println("吃香蕉");
}
}
public class Test {
public static void main(String[] args) {
Fruit apple=new Apple();
apple.eat();
Fruit banana=new Banana();
banana.eat();
}
}
interface Fruit{
void eat();
}
class Apple implements Fruit{
@Override
public void eat() {
System.out.println("吃苹果");
}
}
class Banana implements Fruit{
@Override
public void eat() {
System.out.println("吃香蕉");
}
}
class FruitFactory {
public static Fruit getInstance(String className){
if ("apple".equals(className)){
return new Apple();
}else if ("banana".equals(className)){
return new Banana();
}
return null;
}
}
public class Test {
public static void main(String[] args) {
Fruit apple= FruitFactory.getInstance("apple");
apple.eat();
Fruit banana= FruitFactory.getInstance("banana");
banana.eat();
}
}