前言
java有23中设计模式。设计模式在Java编程里面用处非常的多,如果把开发一个项目比喻成盖房子,在盖房子的过程中不同的位置需要不同的设计方式,如果设计的好,房子会更耐用更美观。在开发过程中有时就要引入设计模式,让代码可重用,保证代码可靠性,提高开发效率等。
介绍
工厂方法模式:顾名思义,类似于一个工厂对不同的产品进行加工,然后分配给需要该产品的商家。
步骤
第一步:创建抽象产品类People
package com.Plant;
/**
* 定义一个抽象工厂类
*/
public abstract class People {
protected String position;//职位
private String name;//姓名
private int age;//年龄
public String getName() { return name; }
public int getAge() { return age; }
public void setName(String name) {this.name = name; }
public void setAge(int age) { this.age = age; }
public People() { }
public People(String position, String name, int age) {
this.position = position;
this.name = name;
this.age = age;
}
public void sleep(){
System.out.println("一位"+this.age+"岁的"+this.position+"他的姓名为"+this.name+"正在睡觉!");
}
public abstract void job();//定义一个抽象方法
}
第二步: 创建具体产品类
创建一个Student类继承People类,并且实现People的抽象方法。
package com.Plant;
/**
* 学生类
*/
public class Student extends People {
public Student() { this.position="学生"; }
public Student(String name, int age) {
super("学生", name, age);
}
public void show(){
System.out.println("啊?"+this.getName()+"考了100分。");
}
@Override
public void job() {
System.out.println(this.getName()+"今年"+this.getAge()+"岁"+"他的工作是学习。");
}
}
创建一个Teacher类继承People类,并且实现People的抽象方法。
package com.Plant;
/**
* 老师类
*/
public class Teacher extends People {
public Teacher() { this.position="老师"; }
public Teacher(String name, int age) {
super("老师", name, age);
}
public void tell(Student student){
System.out.println(this.getName()+"正在跟他的学生"+student.getName()+"讲道理,为什呢,因为"+student.getName()+"比较皮!");
}
@Override
public void job() {
System.out.println(this.getName()+"今年"+this.getAge()+"岁"+"他的工作是教书。");
}
}
创建一个Program类继承People类,并且实现People的抽象方法。
package com.Plant;
/**
* 程序员类
*/
public class Programmer extends People {
public Programmer() { this.position="程序员"; }
public Programmer(String name, int age) {
super("程序员", name, age);
}
public void WorkOverTime(){//加班的方法(你懂的!!!)
System.out.println(this.getAge()+"岁"+"苦逼"+this.position+this.getName()+"正在疯狂加班!!!!");
}
@Override
public void job() {
System.out.println(this.getName()+"今年"+this.getAge()+"岁"+"他的工作是打代码。");
}
}
第三步:创建工厂类Plant
package com.Plant;
/**
* 工厂类:根据特定的职位返回对应的的类
*/
public class Plant {
public static People getInsent(String position){//定义一个static方法 根据你传入的参数 返回特定的对象
if(position.equals("学生"))
return new Student(); //因为Student类是继承People 可以直接返回Student的匿名对象
if(position.equals("老师"))
return new Teacher();
if(position.equals("程序员"))
return new Programmer();
return null;
}
}
第四步:通过调用创建工厂类来创建不同的实例
package com.Plant;
public class Main {
public static void main(String[] args) {
//程序员
People people=Plant.getInsent("程序员");
people.setAge(20); //赋值
people.setName("老仨");
people.job(); //不能直接people.WorkOverTime()方法
Programmer programmer=null; //因为People类中没有WorkOverTime方法
if(people instanceof Programmer)
programmer=(Programmer)people; //因此需要 向下转型
programmer.WorkOverTime();
System.out.println("");
//学生
people=Plant.getInsent("学生");
people.setAge(20);
people.setName("熊X");
people.job();
people.sleep();
Student student=null;
if(people instanceof Student)
student=(Student)people;
student.show();
System.out.println("");
//老师
people=Plant.getInsent("老师");
people.setName("杨老师");
people.setAge(28);
people.job();
Teacher teacher=null;
if (people instanceof Teacher)
teacher=(Teacher)people;
teacher.tell(student);
}
}
运行结果:
总结:由上面代码可看出,在工厂方法设计模式中。一直反复的用到面向对象的编程思想,即:1.封装:使代码模块化,安全性更高。2.继承:使代码可重用,减少一部分代码量。3.多态:一个接口,多种状态,让代码变得更灵活。
每日鸡汤:如果你每天感觉很累,那就对了,因为舒服是留给死人的!
Over !