1、工厂模式
在工厂模式中,我们在创建对象时不会对客户端暴露创建逻辑,并且是通过使用一个共同的接口来指向新创建的对象。
通俗的讲,工厂模型的本质就是用工厂方法代替new操作创建一种实例化对象的方式,以提供一种方便地创建有同种类型接口的产品的复杂对象。
以创建手机为例:
(1)定义接口:
public interface Phone { String brand(); }
(2)定义实现类:
public class Iphone implements Phone { @Override public String brand() { return "this is a Apple phone"; } } public class HuaWei implements Phone { @Override public String brand() { return "this is a Huawei phone"; } }
(3)定义工厂类:
public class factory_model { public Phone createPhone (String phoneName){ if("Apple".equals(phoneName)){ return new Iphone(); }else if("HuaWei".equals(phoneName)){ return new HuaWei(); }else { return null; } } }
(4)使用工厂模式:
public class Main { public static void main(String[] args) { factory_model factory = new factory_model(); Phone huawei = factory.createPhone("HuaWei"); Phone apple = factory.createPhone("Apple"); System.out.println(huawei.brand()); System.out.println(apple.brand()); } }
2、抽象工厂模式
抽象工厂模式在工厂模式上添加了一个创建不同工厂的抽象接口,该接口可叫做超级工厂。
在使用过程中,我们首先通过抽象接口创建出不同的工厂对象,然后根据不同的工厂对象创建不同的对象。
以不同品牌工厂生产手机和电脑为例:
(1)手机类产品的接口定义:
public interface Phone { String call(); }
(2)手机类产品的实现类定义:
public class PhoneApple implements Phone { @Override public String call() { return " call somebody by apple phone"; } } public class PhoneHuaWei implements Phone { @Override public String call() { return " call somebody by huawei phone"; } }
(3)电脑类产品的接口定义:
public interface Computer { String internet(); }
(4)电脑类产品的实现类定义:
public class ComputerApple implements Computer { @Override public String internet() { return "surf the internet by apple computer"; } } public class ComputerHuaWei implements Computer { @Override public String internet() { return "surf the internet by huawei computer"; } }
(5)抽象工厂定义:
public abstract class AbstractFactory { public abstract Phone createPhone(String brand); public abstract Computer createComputer(String brand); }
(6)手机工厂类的定义:
public class PhoneFactory extends AbstractFactory { @Override public Phone createPhone(String brand) { if("Apple".equals(brand)){ return new PhoneApple(); }else if("HuaWei".equals(brand)){ return new PhoneHuaWei(); }else { return null; } } @Override public Computer createComputer(String brand) { return null; } }
(7)电脑工厂类的定义:
public class ComputerFactory extends AbstractFactory { @Override public Phone createPhone(String brand) { return null; } @Override public Computer createComputer(String brand) { if("Apple".equals(brand)){ return new ComputerApple(); }else if("HuaWei".equals(brand)){ return new ComputerHuaWei(); }else { return null; } } }
(8)使用抽象工厂:
public class Main { public static void main(String[] args) { AbstractFactory phoneFactory = new PhoneFactory(); Phone phoneApple = phoneFactory.createPhone("Apple"); Phone phoneHuaWei = phoneFactory.createPhone("HuaWei"); System.out.println(phoneApple.call()); System.out.println(phoneHuaWei.call()); AbstractFactory computerFactory = new ComputerFactory(); Computer computerApple = computerFactory.createComputer("Apple"); Computer computerHuaWei = computerFactory.createComputer("HuaWei"); System.out.println(computerApple.internet()); System.out.println(computerHuaWei.internet()); } }
原文地址:https://www.cnblogs.com/strong-FE/p/12114687.html
时间: 2024-10-08 02:36:13