60 likes | 169 Vues
In software engineering, establishing a "contract" between programming teams is crucial for seamless integration. This contract, known as an interface in Java, allows different groups to develop their code independently without knowledge of each other's implementations. For instance, in the context of a driverless car, automobile manufacturers and electronic guidance manufacturers adhere to a defined interface, enabling collaboration without direct dependencies. This principle extends to APIs, where one company can leverage complex software packages provided by another, utilizing public interfaces for interaction while keeping their implementations confidential.
E N D
SampleAndroid App Chien-Chung Shen CIS, UD cshen@cis.udel.edu
Java Interface • In software engineering, it is important for disparate groups of programmers to agree to a "contract" that spells out how their software interacts • Each group should be able to write their code without any knowledge of how the other group's code is written • Such a contract is termed interfacein Java
Example of Driverless Car • Automobile manufacturers write software that operates the car • Electronic guidance instrument manufacturers make computer system that uses GPS and traffic info to drive the car • Neither group needs to know howthe other group's software is implemented as long as they adhere to the published interface
An Example Java Interface • public interfaceOperateCar { // constant declarations, if any // methodsignatures intturn(Direction direction, double radius, double startSpeed, double endSpeed); intchangeLanes(Direction direction, double startSpeed, double endSpeed); intsignalTurn(Direction direction, booleansignalOn); intgetRadarFront(double distanceToCar, double speedOfCar); intgetRadarRear(double distanceToCar, double speedOfCar); } • Write a class that implementsthe interface • public class OperateBMW760i implementsOperateCar { // the OperateCar method signatures, with implementation intsignalTurn(Direction direction, booleansignalOn) { // code to turn BMW's LEFT turn indicator lights on // code to turn BMW's LEFT turn indicator lights off ....... } // other members, as needed -- for example, helper classes not // visible to clients of the interface }
Interfaces as APIs • Interface is used as an Application Programming Interface (API) • One company sells a software package that contains complex methods that another company wants to use in its own software product – e.g. • One company sells a package of digital image processing methods to companies making end-user graphics programs • The image processing company writes its classes to implement an interface, which it makes public • The graphics company then invokes the image processing methods using the signatures and return types defined in the interface • While API is made public (to its customers), its implementation is kept as a closely guarded secret
UI Events • Android provides listeners for events, such as click, touch, etc. • InterfaceTextWatcher – watch for text changes in edit text field • InterfaceOnClickListener– handle click event • ImplementTextWatcher and attachit to the edit text field where user is typing the text • Methods of TextWatcher will be invoked (used) as user changes the text