1 / 4

Question 5: Dependency Injection

Question 5: Dependency Injection. Description: Allows the injection of objects into a class, rather than relying on the class to create the object itself

Télécharger la présentation

Question 5: Dependency Injection

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Question 5: Dependency Injection Description: Allows the injection of objects into a class, rather than relying on the class to create the object itself It is a pattern in which responsibility for object creation and object linking is removed from the objects themselves and transferred to a factory The pattern seeks to establish a level of abstraction via a public interface, and to remove dependency on components by (for example) supplying a plug-in architecture Martin Fowler described three main styles of dependency injection. The Constructor Injection: A constructor is used to decide how to inject ‘A’ implementation into the ‘B’ class. For this to work, B class needs to declare a constructor that includes everything it needs injected. The Setter Injection: define setter method that will accept the injection The Interface Injection:by defining an interface that I'll use to perform the injection through

  2. Question 7: Model View controller • The problems: • User interface tends to change more frequently than business logic: Therefore if presentation code and logic codes are combined in a single object, one will have to modify an object containing business logic every time there is a change in the user interface. • Application displays the same data in different ways. If the user changes data in one view, the system must be able to update all other views of the data automatically • User interface code tend to be more device – dependent than business logic (migrating application from browser-based application to support PDAs or web enabled cell phone, the user interface codes will need much changes while the business logic might remain the same) • The MVC pattern separates the modelling of the domain, the presentation, and the actions based on user input into three separate classes (separate the business logic from the graphical user interfaces). • Participants • Model: The model has the components that use and rely on the underlying data. • View: The view houses the user interface components. It manages the display of information. • Controller: The controller contains the components that control which view is being displayed. • Implementation : use Observer pattern to keep the views and the associated model synchronized.

  3. Student getName() : string getId() : int getDept() : string listcourses(): void Institution getName() : string getId() : int getDept() : string Employee getName() : string getId() : int getDept() : string computeSalary():double Student listcourses(): void Employee computeSalary():double Question 12: Extract Super Class Motivation: • Duplicate code is a bad ways of implementing system • There are two classes with similar features • Each class does similar implementation in the same way or in different way • This promotes code repetition and duplication. • The choice of solving the problem is extract class. The choice is between inheritance and delegation. It the two class shares interface as well as behaviour then inheritance is best suited. Sample: The classes student and employee in IT University consist of similar methods • Mechanism: • Create blank superclass, and make the original classes to be subclasses to the superclass • Move the common feature (apply pull up method), move common fields, and constructor • bodies to move common elements to the superclass • Compile and test after each pull • Check each client of the superclass. • If they only share the same interface then change the required type to superclass. • If subclass inherits from other class, then use delegate in any language that does not support multiple inheritance (e.g C#, java)

  4. Question 16: Code Generation • Problem on merging hand written code and generate code: • It is complicated to maintain. That is, it is hard to trace hand written code in generated code, which thence leads to complexity and difficulty in maintaining. To address the problems: • Use comments or regions to separate hand written from generated code • Put hand written code in subclass (relies on naming convention, NOT transparent) • Merging (by treating generated code as coming from A and hand written coming from B. • Then use the CVS to merge the two pieces together. • Using partial classes (program dependent only C#) by spanning the classes into several files • Use of CodeDom versus simple source text generation using visitor • The text based code generation: • Language dependent i.e it can only generate code for one specific language • Most cases generate syntax error (e.g misplacement of “;”, ) • It is hard to see what is produced • CodeDOM • Language independent i.e. it can generate code for different languages. • Syntax error free: the source is represented by an abstract syntax tree

More Related