1, the prototype mode:
With an already created instance as a prototype to create a prototype of the same or similar new objects by copying this prototype object.
Mainly inherited java in the Cloneable interface is shallow clone
public class ProtoTypeCitation
{
public static void main(String[] args) throws CloneNotSupportedException
{
citation obj1=new citation("张三","同学:在2016学年第一学期中表现优秀,被评为三好学生。","韶关学院");
obj1.display();
citation obj2=(citation) obj1.clone();
obj2.setName("李四");
obj2.display();
}
}
//奖状类
class citation implements Cloneable
{
String name;
String info;
String college;
citation(String name,String info,String college)
{
this.name=name;
this.info=info;
this.college=college;
System.out.println("奖状创建成功!");
}
void setName(String name)
{
this.name=name;
}
String getName()
{
return(this.name);
}
void display()
{
System.out.println(name+info+college);
}
public Object clone() throws CloneNotSupportedException
{
System.out.println("奖状拷贝成功!");
return (citation)super.clone();
}
}
Results are as follows:
奖状创建成功!
张三同学:在2016学年第一学期中表现优秀,被评为三好学生。韶关学院
奖状拷贝成功!
李四同学:在2016学年第一学期中表现优秀,被评为三好学生。韶关学院
2, the builder pattern
The main role of the builder (Builder) mode are as follows.
- Product role (Product) : It is a complex object that contains multiple components, and off to create its various components by a particular builder.
- Abstract builder (Builder) : It is an abstract method includes creating various subcomponents of the product interface, typically also contain a complex product process returns getResult ().
- Specific builder (by Concrete Builder) : Interface Builder achieve, accomplish specific method of creating the various components of complex products.
- Director (Director) : it calls part construction and assembly methods builder object of creating complex objects, that did not involve specific products in command who.
Product categories:
public class Bike {
private IFrame frame;
private ISeat seat;
private ITire tire;
public IFrame getFrame() {
return frame;
}
public void setFrame(IFrame frame) {
this.frame = frame;
}
public ISeat getSeat() {
return seat;
}
public void setSeat(ISeat seat) {
this.seat = seat;
}
public ITire getTire() {
return tire;
}
public void setTire(ITire tire) {
this.tire = tire;
}
}
Builder 类:
public abstract class Builder {
abstract void buildFrame();
abstract void buildSeat();
abstract void buildTire();
abstract Bike createBike();
}
ConcreteBuilder categories:
public class MobikeBuilder extends Builder{
private Bike mBike = new Bike();
@Override
void buildFrame() {
mBike.setFrame(new AlloyFrame());
}
@Override
void buildSeat() {
mBike.setSeat(new DermisSeat());
}
@Override
void buildTire() {
mBike.setTire(new SolidTire());
}
@Override
Bike createBike() {
return mBike;
}
}
public class OfoBuilder extends Builder{
private Bike mBike = new Bike();
@Override
void buildFrame() {
mBike.setFrame(new CarbonFrame());
}
@Override
void buildSeat() {
mBike.setSeat(new RubberSeat());
}
@Override
void buildTire() {
mBike.setTire(new InflateTire());
}
@Override
Bike createBike() {
return mBike;
}
}
Commander categories:
public class Director {
private Builder mBuilder = null;
public Director(Builder builder) {
mBuilder = builder;
}
public Bike construct() {
mBuilder.buildFrame();
mBuilder.buildSeat();
mBuilder.buildTire();
return mBuilder.createBike();
}
}
The client uses:
public class Click {
public static void main(String[] args) {
showBike(new OfoBuilder());
showBike(new MobikeBuilder());
}
private void showBike(Builder builder) {
Director director = new Director(builder);
Bike bike = director.construct();
bike.getFrame().frame();
bike.getSeat().seat();
bike.getTire().tire();
}
}