体系结构—简单工厂模式

编写一个简单工厂模式的程序

public interface clothingType {
	public void getType();    //打印选择的服装类型
}

public class t_shirt implements clothingType{
	@Override
	public void getType() {
		System.out.println("您选择的是T恤");  	
	}
}

public class short_sleeve implements clothingType{
	@Override
	public void getType() {
		System.out.println("您选择的是短袖");  	
	}
}

public class clothingFactory {
	public static clothingType CreateType(String type)
      //根据提供的类型,去实例化具体的对象  
    {  
		clothingType ctype = null;  
        switch (type)
        {   
            case"T恤":  
                ctype = new t_shirt();  //如果是T恤,则返回T恤子类  
                break;  
            case"短袖":  
                ctype = new short_sleeve();  //如果是短袖,则返回短袖子类  
                break;  
        }  
        return ctype;  
    }  
}   
	
public class 服装厂 {
	public static void main(String[] args) {
		clothingType type1 = clothingFactory.CreateType("T恤");  
         //传入参数“T恤”,让工厂去实例化对象的T恤类  
        type1.getType();  

        clothingType type2 = clothingFactory.CreateType("短袖");  
       //传入参数“短袖”,让工厂去实例化对象的短袖类 
        type2.getType();  
	}
}

运行结果:


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值