工厂方法demo
工厂方法测试类
package com.zhh.designpattern.factorymethod;
public class FactoryMethodTest
{
public static void main(String[] args) {
IAirConditionFactory haierFactory = new HaierAirConditionFactory();
IAirCondition haierAirCondition = haierFactory.createAirCondition();
haierAirCondition.working();
IAirConditionFactory meiDiFactory = new MeidiAirConditionFactory();
IAirCondition meidiAirConDition = meiDiFactory.createAirCondition();
meidiAirConDition.working();
}
}
测试效果
工厂抽象类(接口)
package com.zhh.designpattern.factorymethod;
public interface IAirConditionFactory {
IAirCondition createAirCondition();
}
产品抽象类
package com.zhh.designpattern.factorymethod;
public interface IAirCondition {
void working();
}
具体工厂类(海尔品牌工厂)
package com.zhh.designpattern.factorymethod;
public class HaierAirConditionFactory implements IAirConditionFactory {
@Override
public IAirCondition createAirCondition() {
return new HaierAirCondition();
}
}
具体工厂类(美的品牌工厂)
package com.zhh.designpattern.factorymethod;
public class MeidiAirConditionFactory implements IAirConditionFactory{
@Override
public IAirCondition createAirCondition(){
return new MeidiAirCondition();
}
}
产品产品类(海尔空调)
package com.zhh.designpattern.factorymethod;
public class HaierAirCondition implements IAirCondition {
public HaierAirCondition(){
System.out.println("海尔空调生产完成.");
}
@Override
public void working() {
System.out.println("海尔空调开始工作..\n-------------------\n");
}
}
产品产品类(美的空调)
package com.zhh.designpattern.factorymethod;
public class MeidiAirCondition implements IAirCondition {
MeidiAirCondition(){
System.out.println("美的空调生产完成.");
}
@Override
public void working() {
System.out.println("美的空调开始工作..\n-------------------\n");
}
}