工厂模式
简单工厂模式
我们要运行车,但是无需知道车是怎么造出来的
先需要有车的接口
public interface Car {
void run();
}
我们现在可以生产两辆车,奔驰和宝马
public class Benz implements Car{
@Override
public void run(){
System.out.println("Benz is running!");
}
}
public class BMW implements Car {
@Override
public void run(){
System.out.println("BMW is running!");
}
}
我们有一个工厂根据订单生产不同的车
public class Factory {
public static Car getCar(String carName){
switch (carName){
case "Benz":
Car benz=new Benz();
return benz;
case "BMW":
Car bmw=new BMW();
return bmw;
default:
return null;
}
}
}
作为开车的人,我只想知道我想开哪辆车,并不想知道车是怎么造出来的
public static void main(String[] args) {
Benz benz=(Benz) Factory.getCar("Benz");
BMW bmw=(BMW)Factory.getCar("BMW");
benz.run();
bmw.run();
}
模板方法模式
有一个抽象类,三个抽象方法,一个已实现的算法来使用抽象方法,已决定使用顺序
public abstract class AbstractClass {
abstract String abstractMethod1();
abstract boolean abstractMethod2();
abstract void abstractMethod3();
public void templateMethod(){
System.out.println(abstractMethod1());
if(abstractMethod2()){
abstractMethod3();
}
}
}
两个类继承于此抽象类并实现
public class InstanceClass1 extends AbstractClass{
@Override
String abstractMethod1(){
return "I am class1";
}
@Override
boolean abstractMethod2(){
return true;
}
@Override
void abstractMethod3(){
System.out.println("This is the method of class 1");
}
}
public class InstanceClass2 extends AbstractClass{
@Override
String abstractMethod1(){
return "I am class2";
}
@Override
boolean abstractMethod2(){
return false;
}
@Override
void abstractMethod3(){
System.out.println("This is the method of class 2");
}
}
实现:
public class Scence {
public static void main(String[] args) {
AbstractClass class1=new InstanceClass1();
AbstractClass class2=new InstanceClass2();
class1.templateMethod();
class2.templateMethod();
}
}
单例模式
饿汉式单例
public class Singleton {
private Singleton(){}
private static Singleton instance=new Singleton();
public static Singleton getInstance(){
return instance;
}
}
懒汉式单例
public class Singleton {
private Singleton(){}
private static Singleton instance=null;
public static synchronized Singleton getInstance(){
if(instance == null){
instance=new Singleton();
}
return instance;
}
}
命令模式
厂家有两个产品,灯与热水器
public class Heater {
public void HeaterOn(){
System.out.println("加热中");
}
public void HeaterOff(){
System.out.println("加热中");
}
}
需要一个外部设备同时可以控制他俩 难道继承这两个类吗?
非也。
先定义一个Commond接口
public interface Command {
void excute();
}
大灯控制器
public class LightOnCommand implements Command{
Light light;
public LightOnCommand(Light light){
this.light=light;
}
@Override
public void excute(){
light.LightOn();
}
}
public class LightOffCommand implements Command {
Light light;
public LightOffCommand(Light light){
this.light=light;
}
@Override
public void excute(){
light.LightOff();
}
}
加热器控制器
public class HeaterOnCommand implements Command {
Heater heater;
public HeaterOnCommand(Heater heater){
this.heater=heater;
}
@Override
public void excute(){
heater.HeaterOn();
}
}
public class HeaterOffCommand implements Command{
Heater heater;
public HeaterOffCommand(Heater heater){
this.heater=heater;
}
@Override
public void excute(){
heater.HeaterOff();
}
}
调用:
public static void main(String[] args) {
Light light=new Light();
Heater heater=new Heater();
LightOnCommand lightOnCommand=new LightOnCommand(light);
LightOffCommand lightOffCommand=new LightOffCommand(light);
HeaterOnCommand heaterOnCommand=new HeaterOnCommand(heater);
HeaterOffCommand heaterOffCommand=new HeaterOffCommand(heater);
lightOnCommand.excute();
lightOffCommand.excute();
heaterOnCommand.excute();
heaterOffCommand.excute();
}
适配器模式
客户端需要的接口(新接口)
public interface Target {
void request();
}
原有的已经实现了的方法,但接口发生了变化
public class Adaptee {
public void oldRequest(){
System.out.println("old request....");
}
}
利用老旧方法实现新接口
public class Adapter implements Target {
private Adaptee adaptee;
public Adapter(Adaptee adaptee){
this.adaptee=adaptee;
}
@Override
public void request(){
adaptee.oldRequest();
}
}
运行
public static void main(String[] args) {
Adaptee adaptee=new Adaptee();
Target target=new Adapter(adaptee);
target.request();
}
责任链模式
请假流程
假设现在一个公司的请假流程如下:一天及以下由小组组长审批,一天以上三天以下由经理审批,三天以上七天以下由老板审批,七天以上直接劝退。
如果每次请假时都很长的if…else…来判断该去找谁请假,很不容易扩展,我们使用责任链模式来实现。
抽象类
public abstract class Handler {
private int day=0;
private Handler nextHander;
//处理请求的流程
public void handleRequest(int day){
if(isInResponsibilityScope(day)){
sign();
}else{
if(nextHander != null){
nextHander.handleRequest(day);
}else{
System.out.println("请假太久,辞职吧");
}
}
}
//设置下一个处理者
public void SetNext(Handler nextHandler){
this.nextHander=nextHandler;
}
protected abstract boolean isInResponsibilityScope(int day);
//签字
protected abstract void sign();
}
接下来是三个主管类
public class GroupLeader extends Handler {
@Override
protected boolean isInResponsibilityScope(int days){
if (days <= 1){
return true;
}
return false;
}
@Override
protected void sign(){
System.out.println("主管签字同意了");
}
}
public class Manager extends Handler {
@Override
protected boolean isInResponsibilityScope(int days){
if (days > 1 && days <= 3)
{
return true;
}
return false;
}
@Override
protected void sign(){
System.out.println("经理签字同意了");
}
}
public class Boss extends Handler{
@Override
protected boolean isInResponsibilityScope(int days){
if (days > 3 && days <= 7)
{
return true;
}
return false;
}
@Override
protected void sign(){
System.out.println("老板签字同意了");
}
}
接下来是实现的代码
public static void main(String[] args) {
GroupLeader groupLeader=new GroupLeader();
Manager manager=new Manager();
Boss boss=new Boss();
groupLeader.SetNext(manager);
manager.SetNext(boss);
groupLeader.handleRequest(1);
groupLeader.handleRequest(2);
groupLeader.handleRequest(5);
groupLeader.handleRequest(10);
}
装饰器模式
定义接口,给出默认实现
public interface Clothes {
void getInfo();
}
class DefaultClothes implements Clothes{
@Override
public void getInfo(){
System.out.println("默认方法");
}
}
装饰器类
public class ClothesDecoration implements Clothes {
private Clothes clothes;
public ClothesDecoration(Clothes clothes){
this.clothes=clothes;
}
@Override
public void getInfo(){
clothes.getInfo();
}
}
每件衣服都是一个装饰器
public class ShoseDecorator extends ClothesDecoration {
private Clothes clothes;
public ShoseDecorator(Clothes clothes){
super(clothes);
this.clothes=clothes;
}
@Override
public void getInfo(){
System.out.println("穿了双鞋子");
super.getInfo();
}
}
public class CoatDecoration extends ClothesDecoration{
private Clothes clothes;
public CoatDecoration(Clothes clothes){
super(clothes);
this.clothes=clothes;
}
@Override
public void getInfo(){
System.out.println("穿了大仪");
super.getInfo();
}
}
主方法
public static void main(String[] args) {
Clothes clothes = new DefaultClothes();
clothes = new ShoseDecorator(clothes);
clothes = new CoatDecoration(clothes);
clothes.getInfo();
}
参考于此链接
https://blog.csdn.net/daguanjia11/article/category/3259443