简单工厂模式
一个抽象产品类,可以派生出多个具体产品类;
一个具体工厂类,可以创建多个具体产品的实例。
抽象产品类定义
public abstract class Sender { void send(){ }; }
具体产品类定义
邮件产品类
public class MailSender extends Sender{ public void send(){ System.out.println("Mail Sender!"); } }
短信产品类
public class SmsSender extends Sender{ public void send(){ System.out.println("Sms Sender!"); } }
简单工厂类
public class SimpleFactory { public static MailSender createMailSender(){ return new MailSender(); } public static SmsSender createSmsSender(){ return new SmsSender(); } }
测试类
public class SimpleFactoryTest { public static void main(String[] args) { Sender mailSender = SimpleFactory.createMailSender(); mailSender.send(); Sender smsSender = SimpleFactory.createSmsSender(); smsSender.send(); } }
工厂方法模式
一个抽象产品类,可以派生出多个具体产品类;
一个抽象工厂类,可以派生出多个具体工厂类;
每个具体工厂类只能创建一个具体产品类的实例。
抽象产品类定义
public abstract class Fruit { void print(){ }; }
具体产品类定义
public class Apple extends Fruit{ public void print(){ System.out.println("apple"); } } public class Pear extends Fruit{ public void print(){ System.out.println("pear"); } }
工厂方法抽象类定义
public abstract class Factory { abstract Fruit create(); }
工厂方法具体工厂类定义:
public class AppleFactory extends Factory{ public Fruit create(){ return new Apple(); } } public class PearFactory extends Factory{ public Fruit create(){ return new Pear(); } }
测试类
public class MethodFactoryTest { public static void main(String[] args) { Factory appleFactory = new AppleFactory(); Fruit apple = appleFactory.create(); apple.print(); Factory pearFactory = new PearFactory(); Fruit pear = pearFactory.create(); pear.print(); } }
抽象工厂模式
多个抽象产品类,每个抽象产品类可以派生出多个具体的产品类;
一个抽象工厂类,可以派生出多个具体工厂类;
每个具体工厂类可以创建多个具体产品类的实例。
抽象产品类定义
public abstract class Fruit { void print(){ }; } public abstract class Car { void print(){ }; }
具体产品类定义
public class Apple extends Fruit{ public void print(){ System.out.println("apple"); } } public class Pear extends Fruit{ public void print(){ System.out.println("pear"); } } public class Bmw extends Car{ public void print(){ System.out.println("bmw"); } } public class Buick extends Car{ public void print(){ System.out.println("buick"); } }
抽象工厂抽象类定义
public abstract class AbstractFactory { abstract Fruit createFruit(); abstract Car createCar(); }
抽象工厂具体工厂类定义
public class BmwAppleFactory extends AbstractFactory{ public Fruit createFruit(){ return new Apple(); } public Car createCar(){ return new Bmw(); } } public class BuickPearFactory extends AbstractFactory{ public Fruit createFruit(){ return new Pear(); } public Car createCar(){ return new Buick(); } }
测试类
public class AbstractFactoryTest { public static void main(String[] args) { AbstractFactory bmwAppleFactory = new BmwAppleFactory(); Car car1 = bmwAppleFactory.createCar(); Fruit fruit1 = bmwAppleFactory.createFruit(); car1.print(); fruit1.print(); AbstractFactory buickPearFactory = new BuickPearFactory(); Car car2 = buickPearFactory.createCar(); Fruit fruit2 = buickPearFactory.createFruit(); car2.print(); fruit2.print(); } }
工厂方法和抽象工厂的区别:
工厂方法模式只有一个抽象产品类,而抽象工厂模式有多个。
工厂方法模式的具体工厂类只能创建一个具体产品的实例,而抽象工厂模式可以创建多个。
时间: 2024-10-05 03:40:29