1. The scenario is resolved
1.1 Scenario Description
Vacation plan generation Project Design factors of vacation plan: time, tickets, restaurants, accommodation, special activities, etc. A plan is a specific object, to be easy to expand, can be one day, three days, seven days, etc
1.2 OO design
1.3 Demand Change
1.4 Bringing Problems
2. Improve with design patterns
2.1 analysis
2.2 Redesign
2.3 the source code
- Base object (Vacation[Vacation],VacationDay[Vacation day detail])
import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Calendar; import java.util.Date; public class Vacation { private ArrayList<VacationDay> mVacationDayLst; private Date mStDate; private int mDays = 0; private VacationDay mVacationDay; public Vacation(String std) { mVacationDayLst = new ArrayList<VacationDay>(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); try { mStDate = sdf.parse(std); mVacationDay = new VacationDay(mStDate); mVacationDayLst.add(mVacationDay); mDays++; } catch (ParseException e) { e.printStackTrace(); } } public void setStDate(String std) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); try { mStDate = sdf.parse(std); } catch (ParseException e) { e.printStackTrace(); } } public Date getStDate() { return mStDate; } public void addDay() { mVacationDay = new VacationDay(nextDate(mDays)); mVacationDayLst.add(mVacationDay); mDays++; } public boolean setVacationDay(int i) { if ((i > 0) && (i < mVacationDayLst.size())) { mVacationDay = mVacationDayLst.get(i); return true; } mVacationDay = null; return false; } public void setHotel(String mHotels) { mVacationDay.setHotel(mHotels); } public void addTicket(String ticket) { mVacationDay.addTicket(ticket); } public void addEvent(String event) { mVacationDay.addEvent(event); } public void showInfo() { for (int i = 0, len = mVacationDayLst.size(); i < len; i++) { System.out.println("------" + (i + 1) + " day --------"); System.out.println(mVacationDayLst.get(i).showInfo()); } } private Date nextDate(int n) { Calendar cal = Calendar.getInstance(); cal.setTime(mStDate); cal.add(Calendar.DATE, n); return cal.getTime(); } } import java.util.ArrayList; import java.util.Date; public class VacationDay { private Date mDate; private String mHotels; private ArrayList<String> mTickets = null; private ArrayList<String> mEvents = null; public VacationDay(Date date) { mDate = date; mTickets = new ArrayList<String>(); mEvents = new ArrayList<String>(); } public void setDate(Date date) { mDate = date; } public void setHotel(String mHotels) { this.mHotels = mHotels; } public void addTicket(String ticket) { mTickets.add(ticket); } public void addEvent(String event) { mEvents.add(event); } public String showInfo() { StringBuilder stb = new StringBuilder(); stb.append("Date:" + mDate.toString() + "\n"); stb.append("Hotel:" + mHotels + "\n"); stb.append("Tickets:" + mTickets.toString() + "\n"); stb.append("Events" + mEvents.toString() + "\n"); return stb.toString(); }}Copy the code
- Holiday generator (AbsBuilder, Builder3d Builder4d, BuilderSelf)
public abstract class AbsBuilder { public Vacation mVacation; public AbsBuilder(String std) { mVacation = new Vacation(std); } public abstract void buildvacation(); public abstract void buildDay(int i); public abstract void addHotel(String hotel); public abstract void addTicket(String ticket); public abstract void addEvent(String tvent); public Vacation getVacation() { return mVacation; } } public class Builder3d extends AbsBuilder { public Builder3d(String std) { super(std); } @Override public void buildDay(int i) { mVacation.setVacationDay(i); } @Override public void addHotel(String hotel) { mVacation.setHotel(hotel); } @Override public void addTicket(String ticket) { mVacation.addTicket(ticket); } @Override public void addEvent(String event) { mVacation.addEvent(event); } @Override public void buildvacation() { addTicket("Plane Ticket"); addEvent("Fly to Destination"); addEvent("Supper"); addEvent("Dancing"); addHotel("Four Seasons"); mVacation.addDay(); addTicket("Theme Park"); addEvent("Bus to Park"); addEvent("lunch"); addHotel("Four Seasons"); mVacation.addDay(); addTicket("Plane Ticket"); addEvent("City Tour"); addEvent("Fly to Home"); } } public class Builder4d extends AbsBuilder { public Builder4d(String std) { super(std); } @Override public void buildDay(int i) { mVacation.setVacationDay(i); } @Override public void addHotel(String hotel) { mVacation.setHotel(hotel); } @Override public void addTicket(String ticket) { mVacation.addTicket(ticket); } @Override public void addEvent(String event) { mVacation.addEvent(event); } @Override public void buildvacation() { addTicket("Plane Ticket"); addEvent("Fly to Destination"); addEvent("Supper"); addHotel("Hilton"); mVacation.addDay(); addTicket("Zoo Ticket"); addEvent("Bus to Zoo"); addEvent("Feed animals"); addHotel("Hilton"); mVacation.addDay(); addTicket("Beach"); addEvent("Swimming"); addHotel("Home inn"); mVacation.addDay(); addTicket("Plane Ticket"); addEvent("Fly to Home"); } } public class BuilderSelf { public Vacation mVacation; public BuilderSelf(String std) { mVacation = new Vacation(std); } public BuilderSelf addDay() { mVacation.addDay(); return this; } public BuilderSelf buildDay(int i) { mVacation.setVacationDay(i); return this; } public BuilderSelf addHotel(String hotel) { mVacation.setHotel(hotel); return this; } public BuilderSelf addTicket(String ticket) { mVacation.addTicket(ticket); return this; } public BuilderSelf addEvent(String event) { mVacation.addEvent(event); return this; } public Vacation getVacation() { return mVacation; }}Copy the code
- Conductor (Director) and Test class (BuildermsTest)
public class Director { private AbsBuilder builder; public Director(AbsBuilder builder){ this.builder=builder; } public void setBuilder(AbsBuilder builder){ this.builder=builder; } public void construct(){ builder.buildvacation(); builder.getVacation().showInfo(); } } public class BuildermsTest { public static void main(String[] args) { test3d(); test4d(); testself(); } public static void test3d(){ Director mDirector = new Director(new Builder3d("2015-8-30")); //mDirector.setBuilder(new Builder4d("2015-8-30")); mDirector.construct(); } public static void test4d(){ Director mDirector = new Director(new Builder4d("2015-12-29")); mDirector.construct(); } public static void testself() { BuilderSelf builder = new BuilderSelf("2015-9-29"); builder.addTicket("Plane Ticket").addEvent("Fly to Destination") .addEvent("Supper").addHotel("Hilton"); builder.addDay().addTicket("Zoo Ticket").addEvent("Bus to Zoo") .addEvent("Feed animals").addHotel("Home Inn"); builder.addDay(); builder.addTicket("Beach"); builder.addEvent("Swimming"); builder.addHotel("Home inn"); builder.addDay().addTicket("Plane Ticket").addEvent("Fly to Home"); builder.getVacation().showInfo(); }}Copy the code
3. Summary of design patterns
3.1 define
Generator pattern: Encapsulates a complex object construction process and allows step-by-step construction.
The generator pattern is a creative design pattern that abstracts the creation of an object into a generator and controls the sequence of all the generation steps through a director class. The client uses the guide class and passes in the corresponding generator, and the corresponding object can be obtained through the interface of the guide class.
3.2 Analysis
- (1) Products: complex objects to be constructed by specific producers;
- (2) Abstract generator (Bulider) : Abstract generator is an interface that defines methods for each component that creates a Product object and also defines methods for returning the Product object;
- (3) Concrete Products: classes that implement the Builder interface. Concrete generators implement methods defined by the Builder interface;
- (4) Director: Director is a class that needs to contain variables declared by the Builder interface. The director is responsible for providing the user with a concrete generator, that is, the director will ask the concrete generator class to construct the Product object that the user needs. If the requested concrete generator successfully constructs the Product object, the director can tell the concrete producer to return the constructed Product object.
3.3 Two forms of evolution
- Omit the abstract generator class
- Omit the instructor class
4. Design pattern usage scenarios and attention
4.1 Advantages of generator
- Encapsulate the creation of complex objects
- Allows objects to be created in several steps and can change the process (factory mode has only one step)
- A specific object can be generated by specifying a specific generator, hiding the internal structure of the class
- The implementation of an object can be replaced
4.2 Differences between generator patterns and abstract factories
The generator pattern and the Abstract factory pattern are functionally similar, with major differences:
- Generators are used to create large, complex objects
- The generator pattern emphasizes creating objects step by step, and the steps can be changed to generate different objects
- In general, objects are not returned directly in generator mode
5. Refer to the article
HeadFirst Design Mode and related video Design Mode Study Notes (12: Generator mode)