工厂模式

介绍

工厂方法模式:顾名思义,类似于一个工厂对不同的产品进行加工,然后分配给需要该产品的商家。

步骤

第一步:创建抽象产品类People

[java]  view plain  copy
  1. package com.Plant;  
  2.   
  3. /** 
  4.  * 定义一个抽象工厂类 
  5.  */  
  6. public abstract class People {  
  7.     protected String position;//职位  
  8.   
  9.     private String name;//姓名  
  10.     private int age;//年龄  
  11.   
  12.     public String getName() { return name; }  
  13.   
  14.     public int getAge() { return age; }  
  15.   
  16.     public void setName(String name) {this.name = name; }  
  17.   
  18.     public void setAge(int age) { this.age = age; }  
  19.   
  20.     public People() {    }  
  21.   
  22.     public People(String position, String name, int age) {  
  23.         this.position = position;  
  24.         this.name = name;  
  25.         this.age = age;  
  26.     }  
  27.   
  28.     public void sleep(){  
  29.         System.out.println("一位"+this.age+"岁的"+this.position+"他的姓名为"+this.name+"正在睡觉!");  
  30.     }  
  31.   
  32.   
  33.     public abstract void job();//定义一个抽象方法  
  34. }  

第二步: 创建具体产品类

创建一个Student类继承People类,并且实现People的抽象方法。   

[java]  view plain  copy
  1. package com.Plant;  
  2.   
  3. /** 
  4.  * 学生类 
  5.  */  
  6. public class Student extends People {  
  7.     public Student() {  this.position="学生";  }  
  8.   
  9.     public Student(String name, int age) {  
  10.         super("学生", name, age);  
  11.     }  
  12.   
  13.     public void show(){  
  14.         System.out.println("啊?"+this.getName()+"考了100分。");  
  15.     }  
  16.     @Override  
  17.     public void job() {  
  18.         System.out.println(this.getName()+"今年"+this.getAge()+"岁"+"他的工作是学习。");  
  19.     }  
  20. }  

创建一个Teacher类继承People类,并且实现People的抽象方法。   

[java]  view plain  copy
  1. package com.Plant;  
  2.   
  3. /** 
  4.  * 老师类 
  5.  */  
  6. public class Teacher extends People {  
  7.     public Teacher() {  this.position="老师"; }  
  8.     public Teacher(String name, int age) {  
  9.         super("老师", name, age);  
  10.     }  
  11.     public void tell(Student student){  
  12.         System.out.println(this.getName()+"正在跟他的学生"+student.getName()+"讲道理,为什呢,因为"+student.getName()+"比较皮!");  
  13.     }  
  14.     @Override  
  15.     public void job() {  
  16.         System.out.println(this.getName()+"今年"+this.getAge()+"岁"+"他的工作是教书。");  
  17.     }  
  18. }  

创建一个Program类继承People类,并且实现People的抽象方法。

[java]  view plain  copy
  1. package com.Plant;  
  2.   
  3. /** 
  4.  * 程序员类 
  5.  */  
  6. public class Programmer extends People {  
  7.     public Programmer() { this.position="程序员"; }  
  8.     public Programmer(String name, int age) {  
  9.         super("程序员", name, age);  
  10.     }  
  11.     public void WorkOverTime(){//加班的方法(你懂的!!!)  
  12.         System.out.println(this.getAge()+"岁"+"苦逼"+this.position+this.getName()+"正在疯狂加班!!!!");  
  13.     }  
  14.     @Override  
  15.     public void job() {  
  16.         System.out.println(this.getName()+"今年"+this.getAge()+"岁"+"他的工作是打代码。");  
  17.     }  
  18. }  

第三步:创建工厂类Plant

[java]  view plain  copy
  1. package com.Plant;  
  2.   
  3. /** 
  4.  * 工厂类:根据特定的职位返回对应的的类 
  5.  */  
  6. public class Plant {  
  7.     public static People getInsent(String position){//定义一个static方法 根据你传入的参数 返回特定的对象  
  8.         if(position.equals("学生"))  
  9.             return new Student();             //因为Student类是继承People 可以直接返回Student的匿名对象  
  10.         if(position.equals("老师"))  
  11.             return new Teacher();  
  12.         if(position.equals("程序员"))  
  13.             return new Programmer();  
  14.         return null;  
  15.     }  
  16. }  

第四步:通过调用创建工厂类来创建不同的实例

[java]  view plain  copy
  1. package com.Plant;  
  2.   
  3. public class Main {  
  4.   
  5.     public static void main(String[] args) {  
  6.         //程序员  
  7.         People people=Plant.getInsent("程序员");  
  8.         people.setAge(20);                    //赋值  
  9.         people.setName("老仨");                 
  10.         people.job();                         //不能直接people.WorkOverTime()方法  
  11.         Programmer programmer=null;           //因为People类中没有WorkOverTime方法  
  12.         if(people instanceof Programmer)  
  13.             programmer=(Programmer)people;     //因此需要 向下转型  
  14.         programmer.WorkOverTime();  
  15.   
  16.         System.out.println("");  
  17.         //学生  
  18.         people=Plant.getInsent("学生");  
  19.         people.setAge(20);  
  20.         people.setName("熊X");  
  21.         people.job();  
  22.         people.sleep();  
  23.         Student student=null;  
  24.         if(people instanceof Student)  
  25.             student=(Student)people;  
  26.         student.show();  
  27.   
  28.         System.out.println("");  
  29.         //老师  
  30.         people=Plant.getInsent("老师");  
  31.         people.setName("杨老师");  
  32.         people.setAge(28);  
  33.         people.job();  
  34.         Teacher teacher=null;  
  35.         if (people instanceof Teacher)  
  36.             teacher=(Teacher)people;  
  37.         teacher.tell(student);  
  38.     }  
  39. }  

运行结果:


总结:由上面代码可看出,在工厂方法设计模式中。一直反复的用到面向对象的编程思想,即:1.封装:使代码模块化,安全性更高。2.继承:使代码可重用,减少一部分代码量。3.多态:一个接口,多种状态,让代码变得更灵活。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值