디자인 패턴 학습 -2 프로토 타입 모델 빌더 패턴

1, 프로토 타입 모드 :

프로토 타입으로 이미 생성 된 인스턴스로이 프로토 타입 객체를 복사하여 동일하거나 유사한 새로운 객체의 프로토 타입을 만들 수 있습니다.
에서 주로 상속 자바 Cloneable를 인터페이스입니다 얕은 클론

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();
    }
}

결과는 다음과 같습니다 :

奖状创建成功!
张三同学:在2016学年第一学期中表现优秀,被评为三好学生。韶关学院
奖状拷贝成功!
李四同学:在2016学年第一学期中表现优秀,被评为三好学生。韶关学院

이 빌더 패턴

빌더 (빌더) 모드의 주요 역할은 다음이다.

  • 제품의 역할 (제품) : 그것은 여러 구성 요소를 포함하고 특정 업체에서 다양한 구성 요소를 만드는 오프 복잡한 객체입니다.
  • 추상 빌더 (Builder는) : 그것은 추상적 인 방법은 일반적으로도 복잡한 제품 프로세스를 포함, 제품 인터페이스의 다양한 하위 구성 요소를 만드는 작업이 포함입니다 것은 getResult를 반환합니다 ().
  • (콘크리트 빌더) 비 빌더 : 인터페이스 빌더 달성은 복합 제품의 다양한 컴포넌트를 생성하는 특정 방법을 수행.
  • 이사 (이사) :이 부분의 건설 및 명령 사람들의 특정 제품을 포함하지 않은 복잡한 객체를 생성하는 오브젝트 빌더 조립 방법을 호출합니다.

제품 카테고리 :

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; 
    } 
} 

빌더 类 :

public abstract class Builder { 
    abstract void buildFrame(); 
    abstract void buildSeat(); 
    abstract void buildTire(); 
    abstract Bike createBike(); 
} 

ConcreteBuilder 카테고리 :

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; 
    } 
} 

사령관 카테고리 :

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(); 
    } 
}

클라이언트 사용 :

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(); 
    } 
} 
게시 57 개 원래 기사 · 원의 찬양 3 · 조회수 6206

추천

출처blog.csdn.net/qq_39830579/article/details/101850568