adapter - WordPress.com
Transkript
adapter - WordPress.com
adapter 25 Mayıs 13 Cumartesi 25 Mayıs 13 Cumartesi Problem: How to solve incompatible interfaces, or provide a stable interface to similar components with different interfaces? Solution: Convert the original interface of a component into another interface, through an intermediate adapter object. 25 Mayıs 13 Cumartesi t( es qu re ) target interface 25 Mayıs 13 Cumartesi () t es u eq R ed t la s n a r t adaptee interface Client «interface» Target request() Adapter request() 25 Mayıs 13 Cumartesi Adaptee specificRequest() class adapter Client Target Adaptee request() specificRequest() Adapter request() 25 Mayıs 13 Cumartesi example 25 Mayıs 13 Cumartesi 25 Mayıs 13 Cumartesi Amplifier Tuner on() off() setAm() setFm() setFrequenct() on() off() setCd() setDvd() … CdPlayer Screen on() down() PopCornPopper on() off() pop() 25 Mayıs 13 Cumartesi on() off() eject() pause() … TheatherLights on() off() dim() DvdPlayer on() off() eject() pause() … Projector on() off() tvMode() wideScreen() … HomeTheatherFacade watchMovie() endMovie() listenToCd() … Amplifier Tuner on() off() setAm() setFm() setFrequenct() on() off() setCd() setDvd() … CdPlayer Screen on() down() PopCornPopper on() off() pop() 25 Mayıs 13 Cumartesi on() off() eject() pause() … TheatherLights on() off() dim() DvdPlayer on() off() eject() pause() … Projector on() off() tvMode() wideScreen() … 25 Mayıs 13 Cumartesi 25 Mayıs 13 Cumartesi Two ways to code Menu 25 Mayıs 13 Cumartesi Lahmacun Menu 25 Mayıs 13 Cumartesi Adana Menu 25 Mayıs 13 Cumartesi We want to be able to the printMenu() printLahmacunMenu() printAdanaMenu() printSpicyMenu() isItemSpicy(name) 25 Mayıs 13 Cumartesi Implementing 25 Mayıs 13 Cumartesi The Iterator 25 Mayıs 13 Cumartesi Iterator Pattern <<Interface>> Iterator hasNext() next() AdanaMenuIterator hasNext() next() 25 Mayıs 13 Cumartesi AdanaMenuIterator 25 Mayıs 13 Cumartesi New implementation 25 Mayıs 13 Cumartesi Overview of What We Did LahmacunMenu menuItems createIterator() AdanaMenu menuItems createIterator() Waitress printMenu() <<Interface>> Iterator hasNext() next() AdanaMenuIterator hasNext() next() LahmacunMenuIterator hasNext() next() 25 Mayıs 13 Cumartesi 25 Mayıs 13 Cumartesi 25 Mayıs 13 Cumartesi Composite Pattern Client Component operation() add(Component) remove(Component) getChild(int) Leaf operation() 25 Mayıs 13 Cumartesi Composite add(Component) remove(Component) getChild(int) operation() Composite Pattern Waitress MenuComponent getName() getDescription() …. add(Component) remove(Component) getChild(int) 25 Mayıs 13 Cumartesi MenuItem Menu getName() getDescription() menuComponents getName() … add(Component) remove(Component) getChild(int Let’s see the code 25 Mayıs 13 Cumartesi Menu Item 25 Mayıs 13 Cumartesi Composite Menu 25 Mayıs 13 Cumartesi How to print sub menus 25 Mayıs 13 Cumartesi How are we going to iterate We need a Stack to push the Iterators into. Add createIterator to Menu and CompositeMenu MenuItem returns a NullIterator !!! 25 Mayıs 13 Cumartesi CompositeIterator 25 Mayıs 13 Cumartesi A Null Iterator 25 Mayıs 13 Cumartesi