工厂模式
(1)简单工厂模式
结构图:
public interface Phone { // AbstractProduct void playgames(); }
public class HWPhone implements Phone{ // Product1 @Override public void playgames() { System.out.println("HW手机玩游戏"); } }
public class MIPhone implements Phone { // Product2 @Override public void playgames() { System.out.println("MI手机玩游戏"); } }
public class SimplePhoneFactory { // SimpleFactory public Phone createHWPhone(){ // 生产HW手机 return new HWPhone(); } public Phone createMIPhone(){ // 生产MI手机 return new MIPhone(); } }
public class Test { public static void main(String[] args) { SimplePhoneFactory simpleFactory = new SimplePhoneFactory(); Phone hwphone = simpleFactory.createHWPhone(); hwphone.playgames(); Phone miphone = simpleFactory.createMIPhone(); miphone.playgames(); } }
输出:
HW手机玩游戏
MI手机玩游戏
(2)工厂模式
简单工厂中,一个工厂生产不同品牌的手机,可以对工厂进行抽象,分成各自的工厂。
结构图:
public interface Phone { // AbstractProduct void playgames(); }
public class HWPhone implements Phone{ // Product1 @Override public void playgames() { System.out.println("HW手机玩游戏"); } }
public class MIPhone implements Phone { // Product2 @Override public void playgames() { System.out.println("MI手机玩游戏"); } }
public interface PhoneFactory { // AbstractFactory Phone produce(); }
public class HWFactory implements PhoneFactory { // Factory1 @Override public Phone produce() { return new HWPhone(); } }
public class MIFactory implements PhoneFactory { // Factory2 @Override public Phone produce() { return new MIPhone(); } }
public class Test { public static void main(String[] args) { PhoneFactory hwphoneFactory = new HWFactory(); HWPhone hwPhone = (HWPhone) hwphoneFactory.produce(); hwPhone.playgames(); PhoneFactory miphoneFactory = new MIFactory(); MIPhone miPhone = (MIPhone)miphoneFactory.produce(); miPhone.playgames(); } }
输出:
HW手机玩游戏
MI手机玩游戏
(3)抽象工厂
工厂模式只能生产一种产品,抽象工厂模式提供了一种方式,可以将同一品牌的各种产品单独的工厂封装起来。
结构图:
public interface Phone { // AbstractProductA void playgames(); }
public class HWPhone implements Phone{ // ProductA1 @Override public void playgames() { System.out.println("HW手机玩游戏"); } }
public class MIPhone implements Phone { // ProductA2 @Override public void playgames() { System.out.println("MI手机玩游戏"); } }
public interface Charger { // AbstractProductB void charge(); }
public class HWCharger implements Charger{ // ProductB1 @Override public void charge() { System.out.println("HW手机充电器"); } }
public class MICharger implements Charger { // ProductB2 @Override public void charge() { System.out.println("MI手机充电器"); } }
public interface PhoneFactory { // AbstractFactory Phone producePhone(); Charger produceCharger(); }
public class HWFactory implements PhoneFactory { // Factory1 @Override public Phone producePhone() { return new HWPhone(); } @Override public Charger produceCharger() { return new HWCharger(); } }
public class MIFactory implements PhoneFactory { // Factory2 @Override public Phone producePhone() { return new MIPhone(); } @Override public Charger produceCharger() { return new MICharger(); } }
public class Test { public static void main(String[] args) { PhoneFactory hwphoneFactory = new HWFactory(); HWPhone hwPhone = (HWPhone) hwphoneFactory.producePhone(); HWCharger hwCharger = (HWCharger)hwphoneFactory.produceCharger(); hwPhone.playgames(); hwCharger.charge(); PhoneFactory miphoneFactory = new MIFactory(); MIPhone miPhone = (MIPhone)miphoneFactory.producePhone(); MICharger miCharger = (MICharger)miphoneFactory.produceCharger(); miPhone.playgames(); miCharger.charge(); } }
输出:
HW手机玩游戏
HW手机充电器
MI手机玩游戏
MI手机充电器