代码演示:
package com.mage.oop07.part;
public class Car{
int speed;
public Car(){
}
public void run(){
}
public void setSpeed(int speed){
this.speed=speed;
}
public int getSpeed() {
return speed;
}
}
public class Audi extends Car{
private String type;
Audi(){
}
public void run(){
System.out.println("奥迪以每秒"+speed+"的速度");
}
public String getType(){
return this.type;
}
public void setType(String type){
this.type=type;
}
}
public class AuTuo extends Car{
private String type;
AuTuo(){
}
public void run(){
System.out.println("奥拓以每秒"+speed+"的速度前行");
}
public String getType(){
return type;
}
public void setType(String type){
this.type=type;
}
}
public class CarFactory{
public static Car getInstance(String carType){
Car car=null;
if("奥迪".equals(carType)){
car=new Audi();
}else if("奥拓".equals(carType)){
car=new AuTuo();
}
return car;
}
}
package com.mage.oop07.part.test;
public class Test{
public static void main(String[] args){
Car c=CarFactory.getInstance("奥拓");
c.setSpeed(20);
c.run();
}
}
执行结果:
奥拓以每秒20的速度前行
代码演示:
public class Test{
public static void main(String[] args){
DrinkFlow dc=new DrinkCoff();
dc.flow();
System.out.println("================");
DrinkFlow dt=new DrinkTea();
dt.flow();
}
}
public abstract class DrinkFlow{
private void boil(){
System.out.println("烧水");
}
private void pour(){
System.out.println("倒水");
}
abstract void stir();
private void drink(){
System.out.println("干了");
}
public void flow(){
boil();
pour();
stir();
drink();
}
}
public class DrinkCoff extends DrinkFlow{
public void stir(){
System.out.println("搅拌");
}
}
public class DrinkTea extends DrinkFlow{
public void stir(){
System.out.println("冲泡");
}
}
烧水
倒水
搅拌
干了
================
烧水
倒水
冲泡
干了
有一个学生的数组,查看学生数组中学生年龄在12-23岁之间的学生信息。
有一个学生的数组,查看学生数组中学生分数在33-70岁之间的学生信息
public class Student {
private int id;
private String name;
private int age;
private int score;
public Student() {
}
public Student(int id,String name,int age,int score) {
this.id=id;
this.name=name;
this.age=age;
this.score=score;
}
public void setScore(int score) {
this.score=score;
}
public int getScore() {
return score;
}
public void setAge(int age) {
this.age=age;
}
public int getAge() {
return age;
}
public String toString() {
return name+" "+id+" "+age+" "+score;
}
}
public class Demo1{
public static void main(String[] args) {
//声明一个学生数组,存放5个学生信息
Student stu1=new Student(1,"张山",12,60);
Student stu2=new Student(2,"张三",15,66);
Student stu3=new Student(3,"李四",34,70);
Student stu4=new Student(4,"小二",49,80);
Student stu5=new Student(5,"王二",18,90);
//声明一个学生数组 存储学生对象
Student[] stus=new Student[] {stu1,stu2,stu3,stu4,stu5};
FilterScore(stus);
FilterAge(stus);
}
//编写一个方法将学生数组中的学生信息进行过滤,取出符合要求的对象
public static void FilterScore(Student[] stus) {
//判断获取的每一个对象是否符合要求
for(int i=0;i<stus.length; i++) {
if(stus[i].getScore()>=33&&stus[i].getScore()<=70) {
System.out.println(stus[i]);
}
}
}
public static void FilterAge(Student[] stus) {
for(int i=0;i<stus.length;i++) {
if(stus[i].getAge()>=20&&stus[i].getAge()<90) {
System.out.println(stus[i]);
}
}
}
}
统一接口:满足创建的实现类对象可以传入到形式参数中去
public interface Filter1 {
public abstract void filter(Student[] stus);
}
编写对应的实现类
public class FilterScore implements Filter1{
@Override
public void filter(Student[] stus) {
for(int i=0;i<stus.length; i++) {
if(stus[i].getScore()>=33&&stus[i].getScore()<=70) {
System.out.println(stus[i]);
}
}
}
}
public class FilterAge implements Filter1{
@Override
public void filter(Student[] stus) {
for(int i=0;i<stus.length;i++) {
if(stus[i].getAge()>=20&&stus[i].getAge()<90) {
System.out.println(stus[i]);
}
}
}
}
策略模式的代码
public class Demo1{
public static void main(String[] args) {
//声明一个学生数组,存放5个学生信息
Student stu1=new Student(1,"张山",12,60);
Student stu2=new Student(2,"张三",15,66);
Student stu3=new Student(3,"李四",34,70);
Student stu4=new Student(4,"小二",49,80);
Student stu5=new Student(5,"王二",18,90);
//声明一个学生数组 存储学生对象
Student[] stus=new Student[] {stu1,stu2,stu3,stu4,stu5};
FilterScore filter=new FilterScore();
filter(filter,stus);
FilterAge a=new FilterAge();
filter(a,stus);
}
//策略模式的代码重点
public static void filter(Filter1 filter,Student[] stus) {
//Filter filter = new FilterScore()
filter.filter(stus);//多态
}
}
执行结果:
张山 1 12 60
张三 2 15 66
李四 3 34 70
李四 3 34 70
小二 4 49 80