1.abstract factory
抽象工厂模式,以抽象工厂为核心。创建多个不相干的产品,一个抽象工厂,一个具体工厂,都包含创建多个产品的方法
public abstract Vehicle createVehicle();
public abstract Weapon createWeapon();
public abstract Food createFood();
}
@Override
public Food createFood() {
return new Apple();
}
@Override
public Vehicle createVehicle() {
return new Car();
}
@Override
public Weapon createWeapon() {
return new AK47();
}
}
public static void main(String[] args) {
AbstractFactory f = new DefaultFactory();
Vehicle v = f.createVehicle();
v.run();
Weapon w = f.createWeapon();
w.shoot();
Food a = f.createFood();
a.printName();
}
}
2.build
生成器模式,创建复杂对象,指导者和建造者(可以分抽象,具体)
- public interface PersonBuilder {
- void buildHead();
- void buildBody();
- void buildFoot();
- Person buildPerson();
- }
- public class ManBuilder implements PersonBuilder {
- Person person;
- public ManBuilder() {
- person = new Man();
- }
- public void buildbody() {
- person.setBody("建造男人的身体");
- }
- public void buildFoot() {
- person.setFoot("建造男人的脚");
- }
- public void buildHead() {
- person.setHead("建造男人的头");
- }
- public Person buildPerson() {
- return person;
- }
- }
- public class WomanBuilder implements PersonBuilder {
- Person person;
- public WomanBuilder() {
- person = new Woman();
- }
- public void buildbody() {
- person.setBody(“建造女人的身体");
- }
- public void buildFoot() {
- person.setFoot(“建造女人的脚");
- }
- public void buildHead() {
- person.setHead(“建造女人的头");
- }
- public Person buildPerson() {
- return person;
- }
- }
- public class PersonDirector {
- public Person constructPerson(PersonBuilder pb) {
- pb.buildHead();
- pb.buildBody();
- pb.buildFoot();
- return pb.buildPerson();
- }
- }
- public class Person {
- private String head;
- private String body;
- private String foot;
- public String getHead() {
- return head;
- }
- public void setHead(String head) {
- this.head = head;
- }
- public String getBody() {
- return body;
- }
- public void setBody(String body) {
- this.body = body;
- }
- public String getFoot() {
- return foot;
- }
- public void setFoot(String foot) {
- this.foot = foot;
- }
- }
- public class Man extends Person {
- public Man(){
- System.out.println(“开始建造男人");
- }
- }
- public class Woman extends Person {
- public Woman(){
- System.out.println(“开始建造女人");
- }
- }
- public class Test{
- public static void main(String[] args) {
- PersonDirector pd = new PersonDirector();
- Person womanPerson = pd.constructPerson(new ManBuilder());
- Person manPerson = pd.constructPerson(new WomanBuilder());
- }
- }
android,
public class UserBean {
public int age;
public String name;
public float bodyWeight;
public float height;
public int likeCount;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public float getBodyWeight() {
return bodyWeight;
}
public void setBodyWeight(float bodyWeight) {
this.bodyWeight = bodyWeight;
}
public float getHeight() {
return height;
}
public void setHeight(float height) {
this.height = height;
}
public int getLikeCount() {
return likeCount;
}
public void setLikeCount(int likeCount) {
this.likeCount = likeCount;
}
public static class Builder {
UserBean userBean;
public Builder() {
this.userBean = new UserBean();
}
public Builder setName(String name) {
this.userBean.setName(name);
return this;
}
public Builder setAge(int age) {
this.userBean.setAge(age);
return this;
}
public Builder setBodyWeight(float bodyWeight) {
this.userBean.setBodyWeight(bodyWeight);
return this;
}
public Builder setHeight(float height) {
this.userBean.setHeight(height);
return this;
}
public UserBean build() {
return this.userBean;
}
}
}
UserBean userBean = new UserBean.Builder().setAge(18)
.setName("Bernie")
.setHeight((float) 175.0)
.setBodyWeight((float) 80.0)
.build();
3.factory method
工厂方法模式,一个抽象工厂,多个具体工厂,一个抽象产品,多个具体产品。具体工厂分别创建对应的具体产品
public interface Moveable {
void run();
}
//具体产品角色
public class Plane implements Moveable {
@Override
public void run() {
System.out.println("plane....");
}
}
public class Broom implements Moveable {
@Override
public void run() {
System.out.println("broom.....");
}
}
//抽象工厂
public abstract class VehicleFactory {
abstract Moveable create();
}
public class PlaneFactory extends VehicleFactory{
public Moveable create() {
return new Plane();
}
}
public Moveable create() {
return new Broom();
}
}
public class Test {
public static void main(String[] args) {
VehicleFactory factory = new BroomFactory();
Moveable m = factory.create();
m.run();
}
}
4.prototype
原型模式,创建一样的,区分浅拷贝(共享内存)和深拷贝(独立内存)
// Prototype pattern -- Structural example
using System;
// "Prototype"
abstract class Prototype
{
// Fields
private string id;
// Constructors
public Prototype( string id )
{
this.id = id;
}
public string Id
{
get{ return id; }
}
// Methods
abstract public Prototype Clone();
}
// "ConcretePrototype1"
class ConcretePrototype1 : Prototype
{
// Constructors
public ConcretePrototype1( string id ) : base ( id ) {}
// Methods
override public Prototype Clone()
{
// Shallow copy
return (Prototype)this.MemberwiseClone();
}
}
// "ConcretePrototype2"
class ConcretePrototype2 : Prototype
{
// Constructors
public ConcretePrototype2( string id ) : base ( id ) {}
// Methods
override public Prototype Clone()
{
// Shallow copy
return (Prototype)this.MemberwiseClone();
}
}
/// <summary>
/// Client test
/// </summary>
class Client
{
public static void Main( string[] args )
{
// Create two instances and clone each
ConcretePrototype1 p1 = new ConcretePrototype1( "I" );
ConcretePrototype1 c1 = (ConcretePrototype1)p1.Clone();
Console.WriteLine( "Cloned: {0}", c1.Id );
ConcretePrototype2 p2 = new ConcretePrototype2( "II" );
ConcretePrototype2 c2 = (ConcretePrototype2)p2.Clone();
Console.WriteLine( "Cloned: {0}", c2.Id );
}
}
5.singleton
单体模式,考虑并发,很少考虑反射。构造方法私有,静态内部类获取
- public class Singleton {
- private Singleton() {}
- private static Singleton single=null;
- public static Singleton getInstance() {
- if (single == null) {
- single = new Singleton();
- }
- return single;
- }
- }
- 比较慢,每次都同步
- public static synchronized Singleton getInstance() {
- if (single == null) {
- single = new Singleton();
- }
- return single;
- }
- public static Singleton getInstance() {
- if (singleton == null) {
- synchronized (Singleton.class) {
- if (singleton == null) {
- singleton = new Singleton();
- }
- }
- }
- return singleton;
- }
比较好
- public class Singleton {
- private static class LazyHolder {
- private static final Singleton INSTANCE = new Singleton();
- }
- private Singleton (){}
- public static final Singleton getInstance() {
- return LazyHolder.INSTANCE;
- }
- }
占着内存,快
- public class Singleton1 {
- private Singleton1() {}
- private static final Singleton1 single = new Singleton1();
- public static Singleton1 getInstance() {
- return single;
- }
- }