一、创建型模式(5个)
抽象工厂模式
//用于一个系列或风格的产品生成
public class Client {
public static void main(String[] args) {
System.out.println("来一套A牌茶具");
FactoryTeaset teasetA = new FactoryTeasetA();
Tea teaA = teasetA.getTea();
Teacup teacupA = teasetA.getTeacup();
Teapot teapotA = teasetA.getTeapot();
teaA.show();
teacupA.show();
teapotA.show();
System.out.println("来一套B牌茶具");
FactoryTeaset teasetB = new FactoryTeasetB();
Tea teaB = teasetB.getTea();
Teacup teacupB = teasetB.getTeacup();
Teapot teapotB = teasetB.getTeapot();
teaB.show();
teacupB.show();
teapotB.show();
}
}
public interface FactoryTeaset {
//生产茶壶
Teapot getTeapot();
//生产茶杯
Teacup getTeacup();
//生产茶叶
Tea getTea();
}
public interface Tea {
void show();
}
public interface Teacup {
void show();
}
public interface Teapot {
void show();
}
//A品牌茶具工厂
public class FactoryTeasetA implements FactoryTeaset {
@Override
public Teapot getTeapot() {
return new TeapotA();
}
@Override
public Teacup getTeacup() {
return new TeacupA();
}
@Override
public Tea getTea() {
return new TeaA();
}
}
public class TeaA implements Tea {
@Override
public void show() {
System.out.println("我是A牌茶叶");
}
}
public class TeacupA implements Teacup {
@Override
public void show() {
System.out.println("我是A牌茶杯");
}
}
public class TeapotA implements Teapot {
@Override
public void show() {
System.out.println("我是A牌茶壶");
}
}
//B品牌茶具工厂
public class FactoryTeasetB implements FactoryTeaset {
@Override
public Teapot getTeapot() {
return new TeapotB();
}
@Override
public Teacup getTeacup() {
return new TeacupB();
}
@Override
public Tea getTea() {
return new TeaB();
}
}
public class TeaB implements Tea {
@Override
public void show() {
System.out.println("我是B牌茶叶");
}
}
public class TeacupB implements Teacup {
@Override
public void show() {
System.out.println("我是B牌茶杯");
}
}
public class TeapotB implements Teapot {
@Override
public void show() {
System.out.println("我是B牌茶壶");
}
}
构建者模式
//用于复杂对象的构建,定制化构建
public class Client {
public static void main(String[] args) {
HouseBuilder houseBuilder = new HouseBuilder();
House house = houseBuilder
.setDoors(new House.Door())
.setWindows(new House.Window())
.setWalls(new House.Wall())
.setSwimmingPool(new House.SwimmingPool())
.setRoof(new House.Roof())
.setGarden(new House.Garden())
.setGarage(new House.Garage())
.build();
}
}
@Getter
public class HouseBuilder {
private House.Wall walls;
private House.Door doors;
private House.Window windows;
private House.Roof roof;
private House.Garage garage;
private House.SwimmingPool swimmingPool;
private House.Garden garden;
public HouseBuilder setWalls(House.Wall walls) {
this.walls = walls;
return this;
}
public HouseBuilder setDoors(House.Door doors) {
this.doors = doors;
return this;
}
public HouseBuilder setWindows(House.Window windows) {
this.windows = windows;
return this;
}
public HouseBuilder setRoof(House.Roof roof) {
this.roof = roof;
return this;
}
public HouseBuilder setGarage(House.Garage garage) {
this.garage = garage;
return this;
}
public HouseBuilder setSwimmingPool(House.SwimmingPool swimmingPool) {
this.swimmingPool = swimmingPool;
return this;
}
public HouseBuilder setGarden(House.Garden garden) {
this.garden = garden;
return this;
}
public House build(){
return new House(this);
}
}
@Data
public class House {
private Wall walls;
private Door doors;
private Window windows;
private Roof roof;
private Garage garage;
private SwimmingPool swimmingPool;
private Garden garden;
public House(HouseBuilder builder){
this.walls = builder.getWalls();
this.doors = builder.getDoors();
this.windows = builder.getWindows();
this.roof = builder.getRoof();
this.garage = builder.getGarage();
this.swimmingPool = builder.getSwimmingPool();
this.garden = builder.getGarden();
//如果为null可以进行默认值设置
}
static class SwimmingPool {
}
static class Garden {
}
static class Wall {
}
static class Door {
}
static class Window {
}
static class Roof {
}
static class Garage {
}
}
工厂方法模式
//只生产一种产品,生产不同的产品需要不同的工厂
public class Client {
public static void main(String[] args) {
IFactory factory;
IProduct product;
//华为工厂 生产华为播放器
factory = new FactoryHuawei();
product = factory.getProduct();
product.sing();
//小米工厂 生产小米播放器
factory = new FactoryXiaomi();
product = factory.getProduct();
product.sing();
}
}
public interface IFactory {
IProduct getProduct();
}
public interface IProduct {
/**
* 这个产品可以唱歌
*/
void sing();
}
public class FactoryHuawei implements IFactory {
@Override
public IProduct getProduct() {
return new ProductHuawei();
}
}
public class FactoryXiaomi implements IFactory {
@Override
public IProduct getProduct() {
return new ProductXiaomi();
}
}
public class ProductHuawei implements IProduct {
@Override
public void sing() {
System.out.println("hi,我是华为音乐播放器,我正在唱歌。。。。。");
}
}
public class ProductXiaomi implements IProduct {
@Override
public void sing() {
System.out.println("hi,我是小米音乐播放器,我正在唱歌。。。。。");
}
}
原型模式(克隆模式)
//浅克隆
public class Main {
public static void main(String[] args) throws CloneNotSupportedException {
Address address = new Address();
address.location = "beijing";
User user = new User();
user.id = "id1";
user.name = "name1";
user.address = address;
//-------------------------------------------------
User clone =(User) user.clone();
System.out.println("clone:"+clone.id);
System.out.println("clone:"+clone.name);
System.out.println("clone:"+clone.address.location);
}
}
//需要实现Cloneable 接口才能使用 clone方法
class User implements Cloneable{
String id;
String name;
Address address;
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
class Address{
String location;
}
//深克隆
public class Main {
public static void main(String[] args) throws CloneNotSupportedException {
Address address = new Address();
address.location = "beijing";
User user = new User();
user.id = "id1";
user.name = "name1";
user.address = address;
User clone =(User) user.clone();
System.out.println("clone:"+clone.id);
System.out.println("clone:"+clone.name);
System.out.println("clone:"+clone.address.location);
}
}
//需要实现Cloneable 接口才能使用 clone方法
class User implements Cloneable{
String id;
String name;
Address address;
@Override
protected Object clone() throws CloneNotSupportedException {
User clone = (User) super.clone();
clone.address = (Address) address.clone();
return clone;
}
}
class Address implements Cloneable{
String location;
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
单例模式
public class Singleton {
private static Singleton singleton = new Singleton();
//构造函数私有化
private Singleton(){}
public static Singleton getInstance(){
return singleton;
}
}