设计模式(design pattern) 是一套被反复使用、多数人知晓的、经过分类的、代码设计经验的总结
使用设计模式的目的:为了代码可重用性、让代码更容易被他人理解、保证代码可靠性。 设计模式使代码编写真正工程化;设计模式是软件工程的基石脉络,如同大厦的结构一样。
java中的设计模式:常见的23中设计模式,可以分为三种类型
-
创建型模式:单例模式、抽象工厂模式、建造者模式、工厂模式、原型模式。
-
结构型模式:适配器模式、桥接模式、装饰模式、组合模式、外观模式、享元模式、代理模式。
-
行为型模式:模版方法模式、命令模式、迭代器模式、观察者模式、中介者模式、备忘录模式、解释器模式(Interpreter模式)、状态模式、策略模式、职责链模式(责任链模式)、访问者模式。
工厂模式:简单说就是将对象的创建交给工厂去处理,我们不需要知道创建的细节,植需要通过工厂去获得产品就可以了
spring中的beanFactory就是典型的工厂模式的体现
1、简单工程模式,
假设现在我需要一辆汽车,如果没有工厂,我就需要自己new car(),而有了工厂,我只要给我钱,然后工厂就会帮我生产一辆汽车,
car
package com.lujia.pattern.factory.domain;
/**
* @author :lujia
* @date :2018/9/9 17:02
*/
public class Car {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Car(String name) {
this.name = name;
}
}
工厂:
package com.lujia.pattern.factory.simple;
import com.lujia.pattern.factory.domain.Car;
import org.junit.Test;
/**
* 简单工厂
* @author :lujia
* @date :2018/9/9 17:02
*/
public class SimpleFactory {
public Car getCar(String name){
switch (name){
case "BYD":
return new Car("比亚迪");
case "BWM":
return new Car("宝马");
case "V":
return new Car("大众");
default:
System.out.println("不能生产您所需的产品");
return null;
}
}
@Test
public void test(){
SimpleFactory simpleFactory=new SimpleFactory();
Car bydCar = simpleFactory.getCar("BYD");
System.out.println(bydCar.getName());
}
}
2.抽象工厂
把工厂给抽象出来,这样就可以在不同的地方,建不同的工厂,实现定制化的工厂
package com.lujia.pattern.factory.abstractFactory;
import com.lujia.pattern.factory.domain.Car;
/**
* 抽象工厂
* @author :lujia
* @date :2018/9/9 17:39
*/
public abstract class AbstractFactory {
abstract Car getCar(String name);
}
package com.lujia.pattern.factory.abstractFactory;
import com.lujia.pattern.factory.domain.Car;
/**
* 美国建的工厂
* @author :lujia
* @date :2018/9/9 17:44
*/
public class AmericaFactory extends AbstractFactory {
@Override
Car getCar(String name) {
return new Car(name);
}
}
package com.lujia.pattern.factory.abstractFactory;
import com.lujia.pattern.factory.domain.Car;
/**
* 中国建的工厂
* @author :lujia
* @date :2018/9/9 17:40
*/
public class ChinaFactory extends AbstractFactory {
@Override
Car getCar(String name) {
switch (name){
case "BYD":
return new Car("中国产比亚迪");
case "BWM":
return new Car("中国产宝马");
case "V":
return new Car("中国产大众");
default:
System.out.println("不能生产您所需的产品");
return null;
}
}
}
package com.lujia.pattern.factory.abstractFactory;
import org.junit.Test;
/**
* @author :lujia
* @date :2018/9/9 17:46
*/
public class TestAbstractFactory {
@Test
public void test(){
//中国建的工厂,只能生产是三种类型的车
AbstractFactory factory=new ChinaFactory();
System.out.println(factory.getCar("BWM").getName());
System.out.println(factory.getCar("福特"));
//老美建的工厂,老美技术发达,给要什么车有什么车
factory=new AmericaFactory();
System.out.println(factory.getCar("美国产宝马").getName());
System.out.println(factory.getCar("美国福特").getName());
}
}
3.工厂方法
通常由应用程序直接使用 new 创建新的对象,为了将对象的创建和使用相分离,采用工厂模
式,即应用程序将对象的创建及初始化职责交给工厂对象。
一般情况下,应用程序有自己的工厂对象来创建 Bean.如果将应用程序自己的工厂对象交给 Spring 管
理,那么 Spring 管理的就不是普通的 Bean,而是工厂 Bean。在srping中的体现就是FactoryBean,它是一个用来创建bean的特殊bean,
在代码上体现就是工厂是抽象的,产品也是抽象的
abstract Mobile getMobile();
@Override
Mobile getMobile() {
return new Iphone("苹果");
}
@Override
Mobile getMobile() {
return new HuaWei("华为");
}
@Test
public void testMobile(){
AbstractFactory factory=new ChinaFactory();
Mobile mobile = factory.getMobile();
System.out.println(mobile.getName());
factory=new AmericaFactory();
mobile=factory.getMobile();
System.out.println(mobile.getName());
}