简单工厂模式,就是把创建实例的过程包括逻辑判断等封装到工厂的方法中,客户需要实例的时候,直接调用工厂的方法创建该实例。这种工厂通常是静态类,因此通常也叫静态工厂方法。
只有一个工厂,一种产品,多个具体的产品类继承或实现基产品,一个工厂创建一个具体的产品
代码实例:
产品接口类
package designPattern.simpleFactory;
/**
* Created by yuxl on 2019/9/25.
*/
public interface Car {
public void describe();
}
产品实现类AudiCar
package designPattern.simpleFactory;
/**
* Created by yuxl on 2019/9/25.
*/
public class AudiCar implements Car {
@Override
public void describe() {
System.out.println("我是奥迪!");
}
}
产品实现类BmwCar
package designPattern.simpleFactory;
/**
* Created by yuxl on 2019/9/25.
*/
public class BmwCar implements Car {
@Override
public void describe() {
System.out.println("我是宝马!");
}
}
工厂类CarFactory
package designPattern.simpleFactory;
import designPattern.simpleFactory.AudiCar;
import designPattern.simpleFactory.BmwCar;
import designPattern.simpleFactory.Car;
/**
* Created by yuxl on 2019/9/25.
*/
public class CarFactory {
public static Car getCar(String brand){
switch (brand) {
case "audi":
return new AudiCar();
case "bmw":
return new BmwCar();
default:
return new AudiCar();
}
}
}
测试类Test
package designPattern.simpleFactory;
/**
* Created by yuxl on 2019/9/25.
*/
public class Test {
public static void main(String[] args){
Car audi = CarFactory.getCar("audi");
audi.describe();
Car bmw = CarFactory.getCar("bmw");
bmw.describe();
}
}
定义:策略模式定义了算法族,分别封装起来,让他们之间可以相互替换,此模式让算法的变化独立于使用算法的客户。
使用方法:定义一个算法接口,不同的算法实现这个接口,通过组合的方式在要调用这个算法的类中以接口指定变量的方式定义该算法变量,并用set等方法动态的给这个算法接口指定实例
代码实例:
策略接口Traffic
package designPattern.strategy;
/**
* Created by yuxl on 2019/9/25.
*/
public interface Traffic {
public void wayToTravel();
}
具体策略一 步行上班WalkTravel
package designPattern.strategy;
/**
* Created by yuxl on 2019/9/25.
*/
public class WalkTravel implements Traffic{
@Override
public void wayToTravel() {
System.out.println("今天步行去上班!");
}
}
具体策略二 开车上班DriveTravel
package designPattern.strategy;
/**
* Created by yuxl on 2019/9/25.
*/
public class DriveTravel implements Traffic{
@Override
public void wayToTravel() {
System.out.println("今天开车去上班!");
}
}
具体策略三 乘地铁上班SubWayTravel
package designPattern.strategy;
/**
* Created by yuxl on 2019/9/25.
*/
public class SubWayTravel implements Traffic{
@Override
public void wayToTravel() {
System.out.println("今天乘地铁去上班!");
}
}
写一个类TravelContext,在该类中以接口定义变量的形式定义一个私有策略,并提供一个公共方法给该变量赋值,最后写一个公共方法调用Traffic的wayToTravel方法打印出行方式。
package designPattern.strategy;
/**
* Created by yuxl on 2019/9/25.
*/
public class TravelContext {
private Traffic traffic;
public void SetTraffic(Traffic traffic){
this.traffic = traffic;
}
public void printWayToWork(){
this.traffic.wayToTravel();
}
}
测试,通过TravelContext来实现动态决定出行方式。
package designPattern.strategy;
/**
* Created by yuxl on 2019/9/25.
*/
public class Test {
public static void main(String[] args){
TravelContext context = new TravelContext();
context.SetTraffic(new WalkTravel());
context.printWayToWork();
context.SetTraffic(new DriveTravel());
context.printWayToWork();
context.SetTraffic(new SubWayTravel());
context.printWayToWork();
}
}
*在测试程序中 我们发现,测试代码不仅用到了TravelContext类,还用到了WalkTravel、DriveTravel、SubWayTravel等类,使得客户端必须知道具体的出行方式类才能调用接口,那么有没有什么方法来给客户端与具体出行方式类解耦呢?*我们可以借鉴简单工厂方法,将具体实例化哪个Traffic子类的判断逻辑放到TravelContext类中,重新写一个context类FactoryTravelContext
package designPattern.strategy;
/**
* Created by yuxl on 2019/9/25.
*/
public class FactoryTravelContext {
private Traffic traffic;
public void SetTraffic(String way){
switch (way){
case "步行":
this.traffic = new WalkTravel();break;
case "开车":
this.traffic = new DriveTravel();break;
case "地铁":
this.traffic = new SubWayTravel();break;
default:
this.traffic = new DriveTravel();break;
}
}
public void printWayToWork(){
this.traffic.wayToTravel();
}
}
这时测试程序只需要传入出行方式字符串就可以调用接口,测试代码如下:
package designPattern.strategy;
/**
* Created by yuxl on 2019/9/25.
*/
public class FactoryStrategyTest {
public static void main(String[] args){
FactoryTravelContext context = new FactoryTravelContext();
context.SetTraffic("步行");
context.printWayToWork();
context.SetTraffic("开车");
context.printWayToWork();
context.SetTraffic("地铁");
context.printWayToWork();
}
}
这样就实现了客户端与具体出行方案类的解耦。