Java Design Patterns Learning notes Chapter III

Source: Internet
Author: User

Adapter (Adapter) mode

Interface adaptation

  the intent of the adapter pattern is that the services provided by classes that use different interfaces provide the interface to the client that it expects .

<interface> Physicalrocket

Rocketsim Physicalrocket (

Getmass ():d ouble burnarea:double,

Getthrust ():d ouble burnrate:double,

Setsimtime (t:double) fuelmass:double,

totalmass:double)

Getburntime ():d ouble

Getmass (t:double):d ouble

Getthrust (t:double):d ouble

Oozinorocket

Challenge 3.1 complete above, show The design of the Oozinorocket class that will let the Physicalrocket object participate in the simulation behavior as a Rocketsim object. Assume that you cannot modify the Rocketsim and Physicalrocket classes.

Oozinorocket extends Physicalrocket implements Rocketsim

Correct answer:

challenges 3.2 Complete the above completed Getmass and Getthrust method code.

1  Packagecom.oozinoz.firework;2 Importcom.oozinoz.simlation.*;3 4  Public classOozinozrocket5     extendsPhysicalrocketImplementsrocketsim{6     Private DoubleTime ;7      PublicOozinozrocket (8            DoubleBurnarea,DoubleBurnrate,9            DoubleFuelmass,DoubleTotalmass) {Ten          Super(burnarea,burnrate,fuelmass,totalmass); One     } A      Public DoubleGetmass () { -         //challenges -ReturnGetmass (time); the     } -      Public DoubleGetthrust () { -         //challenges -ReturnGetthrust (time); +     } -      Public voidSetsimtime (DoubleTime ) { +          This. Time =Time ; A     } at}

Object Adapter

The Oozinozskyrocket object inherits from skyrocket and now satisfies the ability to have another class, namely the Physicalrocket class. It is necessary to have instances of this class.

 Public classOozinozskyrocketextendsskyrocket{PrivatePhysicalrocket rocket;  PublicOozinozskyrocket (Physicalrocket r) {Super(R.getmass (0), R.getthrust (0), R.getburntime ()); Rocket=R; }     Public DoubleGetmass () {returnRocket.getmass (simtime); }     Public DoubleGetthrust () {returnRocket.getthrust (simtime); }}

The following is a description of the jtable data

The abstract class provides the default implementation for the TableModel interface.

If you need to design the swing user interface, display several rockets in the table. The Rockettablemodel class can be created to match this set of rockets to the interface expected by TableModel

Whenever we need to use an abstract class to provide support for an interface to be adapted, we must use the object adaptation method.

The explanation in the book is this:

I understand this: why is the way to reuse can only be inherited or combined? How can you reuse a third party to implement an interface? At first I was confused, but I thought it would be a good thing to say. Because the third party is functional, it implements a number of methods. That is, there are specific actions. So you have to put this object into this class in a combined way. If you are using the implementation interface (implements Interface) to extend the functionality. Indeed you have expanded several methods. But the interface is just a constraint you have to have these several methods. And you have to implement it yourself. So I feel that the way the interface is here does not apply. The book says it's right!

Here's the more I'm saying that Rocketmodel is not a rocket subclass. When an adapter class must obtain information from multiple objects, the object adapter should usually be used. It is very comprehensive to prove that the object adaptation must be used in a combination way.

Note: The adapter for the class inherits from the existing class and implements the target interface at the same time. The object adapter inherits from the target class and references an existing class.

1  Public classRockettablemodelextendsabstracttablemodel{2      protectedrocket[] rockets;3      protectedstring[] Colunmnames =4           Newstring[]{"Name", "Price", "Apogee"}; 5       PublicRockettablemodel (rocket[] rockets) {6            This. Rockets =rockets;7      }8 9       Public intgetColumnCount () {Ten           //challenges One           return Super. getColumnCount ();  A      } -  -       PublicString getColumnName (inti) { the           //challenges -           returnColunmnames[i]; -      } -  +       Public intGetRowCount () { -           //challenges +           returnGetRowCount (); A      } at  -       PublicObject getValueAt (intRowintCol) { -           //challenges -Rocket r =Rockets[row]; -String name =Getcolumnname[col]; -method = Rocket.class. Getdeclaredmethod (Name,NULL); inObject value = Method.invoke (R,NULL); -           returnvalue; to      } +}

The challenge position is to let me complete the code. I wrote it on my own. Look at the correct answer.

I should do it directly with the data collection I've implemented.

Colunmnames.length rather than Super.getcolumncount ()
rockets.length rather than GetRowCount ()
For the getValueAt method, I want to follow the way of the answer. But it needs to be judged. I want to reflect the get method directly with reflection. Then call rocket's Get method to get the value. But I didn't throw an exception to this piece of code. Not written under the IDE. is directly handwritten.

Java Design Patterns Learning notes Chapter III

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.