设计模式之(一)——工厂方法模式

设计模式(Design pattern)是一套被反复使用、多数人知晓的、经过分类编目的、代码设计经验的总结。
使用设计模式是为了可重用代码、让代码更容易被他人理解、保证代码可靠性。

设计模式主要分为三大类:
1、创建型模式,共五种:工厂方法模式、抽象工厂模式、单例模式、建造者模式、原型模式。
2、结构型模式,共七种:适配器模式、装饰器模式、代理模式、外观模式、桥接模式、组合模式、享元模式。
3、行为型模式,共十一种:策略模式、模板方法模式、观察者模式、迭代子模式、责任链模式、命令模式、备忘录模式、状态模式、访问者模式、中介者模式、解释器模式。
(其实还有两类:并发型模式和线程池模式。)

这里我简单介绍工厂方法模式(java)

工厂方法模式:

就是使用工厂的形式来统一生产类来适应不同的要求。
比如:bike , car 都有running 的用途。
其实你可以用工厂的形式生成不同的类实现running.

1、下面看下简单工厂方法模式:

interface Transportations{
    public void running();
}

class Bike implements Transportations{
    @override
    public void running(){
        System.out.println("bike is running");
    }
}

class Car implements Transportations{
    @override
    public void running(){
        System.out.println("car is running");
    }
}

class Factory{
    public Transportations create(String type){
        if("bike".equals(type)){
            return new Bike ();
        }else if("car".equals(type)){
            return new Car();
        }else{
            System.out.println("类型不对哦!");  
            return null;  
        }
    }
}

测试时:
public class Test{
    public static void main(String [] args){
        Factory f = new Factory();
        //bike 是bike对象
        Transportations bike = f.create("bike"); 
        // 输出 bike is running
        bike.running();
    }
}

其实上述的简单工厂模式可以进行改进的,因为当你传的参数不对时你其实是不知道的,也不知道该传什么样的参数才对,怎样才能获得正确的类。
所以可以修改 Factory 里创建类的方法,使在创建类的时候很容易就能看出来类的类型。可以减少不必要的麻烦。
可以修改下 Factory 创建类的方式即可。
即 :

interface Transportations{
    public void running();
}

class Bike implements Transportations{
    @override
    public void running(){
        System.out.println("bike is running");
    }
}

class Car implements Transportations{
    @override
    public void running(){
        System.out.println("car is running");
    }
}

class Factory{
    public Transportations createBike(){
        return new Bike();
    }

    public Transportations createCar(){
        return new Car();
    }
}

测试时:
public class Test{
    public static void main(String [] args){
        Factory f = new Factory();
        //bike 很明显是 bike对象
        Transportations bike = f.createBike(); 
        bike.running();
    }
}

如果你还想更简单,可以这样,把Factory中的方法变成静态的,可以在创建类的时候可以直接创建。
即静态工厂方法:

静态工厂方法:

interface Transportations{
    public void running();
}

class Bike implements Transportations{
    @override
    public void running(){
        System.out.println("bike is running");
    }
}

class Car implements Transportations{
    @override
    public void running(){
        System.out.println("car is running");
    }
}

class Factory{
    public static Transportations createBike(){
        return new Bike();
    }

    public static Transportations createCar(){
        return new Car();
    }
}

测试时:
public class Test{
    public static void main(String [] args){
        //创建 bike
        Transportations bike = Factory.createBike(); 
        bike.running();
    }
}

工厂方法模式非常简单,相信能看到这的基本上都学会了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值