1、简单工厂模式
package com.jmj.pattern.factory.simple_factory;
public class SimpleCoffeeFactory {
public Coffee createCoffee(String type) throws Exception {
//声明Coffee类型的变量,根据不同类型创建不同的coffee子类对象
Coffee coffee =null;
if ("american".equalsIgnoreCase(type)){
coffee= new AmericanCoffee();
}else if ("latte".equalsIgnoreCase(type)){
coffee=new LatteCoffee();
}else {
throw new Exception("对不起,您所点的咖啡没有");
}
return coffee;
}
}
package com.jmj.pattern.factory.simple_factory;
public class CoffeeStore {
public Coffee orderCoffee(String type) throws Exception {
SimpleCoffeeFactory simpleCoffeeFactory = new SimpleCoffeeFactory();
Coffee coffee = simpleCoffeeFactory.createCoffee(type);
coffee.addMilk();
coffee.addSuger();
return coffee;
}
}
package com.jmj.pattern.factory.simple_factory;
public class Client {
public static void main(String[] args) throws Exception {
CoffeeStore coffeeStore = new CoffeeStore();
Coffee latte = coffeeStore.orderCoffee("latte");
System.out.println(latte.getName());
}
}
2、工厂方法模式
package com.jmj.pattern.factory.factory_method;
public class CoffeeStore {
private CoffeeFactory coffeeFactory;
public void setCoffeeFactory(CoffeeFactory coffeeFactory) {
this.coffeeFactory = coffeeFactory;
}
public Coffee orderCoffee() throws Exception {
Coffee coffee = coffeeFactory.createCoffee();
coffee.addMilk();
coffee.addSuger();
return coffee;
}
}
3、抽象工厂模式
package com.jmj.pattern.factory.abstract_factory;
public interface DessertFactory {
Coffee createCoffee();
Dessert createDessert();
}
package com.jmj.pattern.factory.abstract_factory;
public class AmericanDessertFactory implements DessertFactory{
@Override
public Coffee createCoffee() {
return new AmericanCoffee();
}
@Override
public Dessert createDessert() {
return new MatchaMousse();
}
}
package com.jmj.pattern.factory.abstract_factory;
public class ItalyDessertFactory implements DessertFactory{
@Override
public Coffee createCoffee() {
return new LatteCoffee();
}
@Override
public Dessert createDessert() {
return new Trimisu();
}
}
优缺点
优点:
当一个产品族中的多个对象被设计成一起工作是,他能保证客户端始终只使用同一个产品族中的对象。
缺点:
当产品族中需要增加要给新的产品是,所有的工厂类都需要进行修改
使用场景:
- 但需要创建的对象是一系列相互关联或相互依赖的产品族时,如电器工厂中的电视机、洗衣机、空调等。
- 系统中有多个产品族,的那每次只是用其中的某一族产品。如有人只喜欢穿某一个品牌的衣服和鞋子。
- 系统中提供了产品的类库,且所有产品的接口相同,客户端不依赖产品实例的创建细节和内部结构。
如:输入法换皮肤,一整套一起换。生成不同操作系统的程序
4、模式扩展
package com.jmj.pattern.factory.config_factory;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
public class CoffeFactory {
//加载配置文件,获取配置文件中配置的全类名,并创建该类的对象进行存储
private static HashMap<String,Coffee> map = new HashMap<>();
//加载配置文件
static {
Properties properties = new Properties();
try {
FileInputStream fileInputStream = new FileInputStream("src/main/resources/bean.properties");
properties.load(fileInputStream);
} catch (IOException e) {
throw new RuntimeException(e);
}
Set<Map.Entry<Object, Object>> entries = properties.entrySet();
for (Map.Entry<Object, Object> entry : entries) {
Object o =null;
try {
o = Class.forName((String) entry.getValue()).newInstance();
} catch (Exception e) {
throw new RuntimeException(e);
}
map.put((String) entry.getKey(), (Coffee) o);
}
}
public static void main(String[] args) {
Coffee american = CoffeFactory.createCoffee("american");
System.out.println(american.getName());
Coffee latte = CoffeFactory.createCoffee("latte");
System.out.println(latte.getName());
}
public static Coffee createCoffee(String name){
return map.get(name);
}
}