"Make more objects."

Source: Internet
Author: User
Tags requires

This leads to an object-oriented program design when a general rule, I was first heard in Grady Booch there: "If the design is too complex, to make more objects." Although it may sound ambiguous and ridiculously simple, it is the most useful guideline I know (you will notice later that "making more objects" is often equated with "adding another level of detour"). In general, if you find a place filled with a lot of complicated code, you need to consider what kind of things can make it seem refreshing. Sorting the system in this way tends to get a better structure and make the program more flexible.
First consider the place where the Trash object was first created, which is a switch statement in main ():

    for (int i = 0, i < i++)
      switch (int) (Math.random () * 3)) {case
        0:
          bin.addelement (new
            aluminum (Mat H.random ()));
          break;
        Case 1:
          bin.addelement (New
            Paper (Math.random () *));
          break;
        Case 2:
          bin.addelement (New
            Glass (Math.random () *));
      }


The code is clearly "too complex" and is one of the places where code must be changed when new types join. If you're always adding a new type, the better solution is to create a separate method that takes all the necessary information and creates a handle that points to an object of the correct type--that has been traced back to a trash object. In the design Patterns, it is loosely called "Create Paradigm". The special paradigm to be applied here is a variant of the factory method. Here, the factory method belongs to a static (static) member of the Trash. But a more common scenario is that it belongs to an overloaded method in a derived class.
The basic principle of the factory method is that we pass the basic information needed to create the object to it, and then return and wait for the handle (which has been traced back to the underlying type) to appear as the return value. From this point on, you can treat the object in a more metaphysical way. Therefore, we simply do not need to know exactly what type of object is being created. In fact, the factory method hides itself, and we can't see it. This is done to prevent inadvertent misuse. If you want to use objects without polymorphism, you must explicitly use RTTI and the specified styling.
But there is still a small problem, especially in the underlying class using more complex methods (not the ones shown here) and overloading (overwriting) the derivative in the context. What if the requested information in the derived class requires more or different parameters? "Create more Objects" solves this problem. To implement the factory method, the Trash class uses a new method named Factory. To hide the creation data, we use a new class named info to contain all the information that the factory method needs to create the appropriate trash object. Here's an easy way to implement info:

Class Info {
  int type;
  Must change this to add another type:
  static final int max_num = 4;
  Double data;
  Info (int typenum, double dat) {
    type = typenum% Max_num;
    data = dat;
  }
}


The only task of the Info object is to hold the information for the factory () method. Now, if there is a special case, factory () need more or different information to create a new type of trash object, then no need to change factory (). By adding new data and builders, we can modify the info class or use subclasses to handle more typical object-oriented forms.
The factory () method for this simple example is as follows:

  Static Trash factory (Info i) {
    switch (i.type) {
      default://To quiet the compiler case
      0: return
        new Alumi Num (i.data);
      Case 1: Return to
        new Paper (i.data);
      Case 2: Return to
        new Glass (i.data);
      Two lines: Case
      3: Return 
        new Cardboard (i.data);
    }
  


In this case, the exact type of the object is easily judged. But we can assume some more complex scenarios, factory () will adopt a complex algorithm. Anyway, the key now is that it's hidden somewhere, and we know where to go when we add new types.
The creation of new objects in main () now becomes very simple and refreshing:

    for (int i = 0; i < i++)
      bin.addelement (trash.factory (int
            ) (Math.random () * Info.max_num),
            math.random () * 100));


We created an Info object here to pass the data to factory (), which creates some kind of trash object in the memory heap and returns the handle added to the vector bin. Of course, if you change the number and type of parameters, you still need to modify the statement. But if the info object is created automatically, it can also avoid that hassle. For example, a vector of parameters can be passed to the builder of the Info object (or passed directly to a factory () call). This requires that the parameters (arguments) be analyzed and checked during the run, but it does provide a very high degree of flexibility.
You can see from this code that factory is responsible for solving the "lead change" problem: If you add a new type to the system (which has changed), the only code that needs to be modified is inside the factory, so factory is isolating the impact of that change.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.