标签:img 模式 null range arm 父类 mic product welcome
//测试
public class CilentTest {
public static void main(String arg[]){
Customer p1=new Children();
System.out.println(p1.calculate(100.00));
Customer p3=new Vip();
System.out.println(p3.calculate(100.00));
}
}
//
interface Customer {
public double calculate(double price);
}
//vip票
class Vip implements Customer {
private String name="VIP";
private double jifeng=0;
public double calculate(double price){
jifeng=jifeng+0.1*price;
System.out.println("Welcome VIP,Now your jifen is "+jifeng);
System.out.print(name+"‘s price is ");
return 0.8*price;
}
}
//儿童票
class Children implements Customer{
private String name="children";
public double calculate(double price){
System.out.print(name+"‘s price is ");
return 0.5*price;
}
}
//学生票
class Student implements Customer{
private String name="student";
public double calculate(double price){
System.out.print(name+"‘s price is ");
return 0.8*price;
}
}
定义一个创建产品对象的工厂接口,将产品对象的实际创建工作推迟到具体子工厂类当中。这满足创建型模式中所要求的“创建与使用相分离”的特点。在简单工厂模式中创建实例的方法通常为静态方法,因此简单工厂模式又叫作静态工厂方法模式
//测试
public class CilentTest {
public static void main(String[] args){
Fruit fruit=FruitFarm.getFruit("apple");
fruit.eat();
Fruit fruit1=FruitFarm.getFruit("orange");
fruit1.eat();
}
}
//抽象产品
interface Fruit {
public void eat();
}
//具体产品1:Apple
class Apple implements Fruit{
@Override
public void eat(){
System.out.println("eat Apple!");
}
}
//具体产品2:Orange
class Orange implements Fruit{
@Override
public void eat() {
System.out.println("eat Orange!");
}
}
//SimpleFactory工厂
class FruitFarm {
public static Fruit getFruit(String args){
if(args.equalsIgnoreCase("apple")){
return new Apple();
}else if(args.equalsIgnoreCase("orange")){
return new Orange();
}else{
return null;
}
}
}
标签:img 模式 null range arm 父类 mic product welcome
原文地址:https://www.cnblogs.com/52yu/p/14779195.html