What is an object oriented pattern

Object-oriented design patterns


1 Object-oriented design pattern 2 In Volume 1, Fundamentals, we already got to know design patterns for the development of algorithms. The aim was to construct general patterns for certain problem classes in order to apply them appropriately to a specific problem. These samples are an abstract program framework that only needs to be filled out for the respective problem. We first presented these general solution methods and then demonstrated them using a simple example. In the area of ​​object-oriented programming, we now proceed in a similar way. In software development, there are special patterns for designing and implementing programs for many development steps. These design patterns are tried and tested solutions to specific problems that have already been used successfully in many applications. Similar to algorithmic patterns, object-oriented design patterns are not limited to any particular programming language. However, when learning the general principles, it is of great advantage to implement these patterns on a concrete example. This is exactly the path we will follow in this chapter with the aid of the Java programming language. The goal of object-oriented design patterns for software design is to make knowledge already gained reusable, to increase the flexibility of the software architecture and to write understandable, expandable and maintenance-friendly code. Due to the high level of abstraction of these design patterns, the development of large software packages can also be planned efficiently and accurately. In this chapter we will first repeat the basic principles of object-oriented programming in an introductory example. Afterwards we present central and advanced concepts of object-oriented programming in Java. The object-oriented design patterns are based primarily on the two concepts of inheritance and polymorphism. With these concepts we introduce the central design patterns of object-oriented programming with the help of examples in Java. Springer-Verlag GmbH Germany 2017 S. Dörn, Programming for Engineers and Natural Scientists, examen.press, DOI / _2 15

2 16 2 Object-Oriented Design Patterns 2.1 Introductory Example As an introductory example we consider a task for the classification of products in the form of screws. A screw manufacturer wants to classify its products according to the following scheme: Screws with a diameter of up to 3 mm and a length of up to 20 mm have a price of EUR 0.30. Screws with a diameter between 3 and 5 mm and a length between 20 and 30 mm are priced at EUR 0.40. Screws with a diameter between 5 and 6 mm and a length between 20 and 30 mm are priced at EUR 0.60. Screws with a diameter between 6 and 15 mm and a length between 30 and 50 mm are priced at EUR 0.80. Screws with a diameter between 15 and 20 mm and a length between 30 and 50 mm are priced at EUR 0.90. The task is to create a program that determines the correct price of a screw when the diameter and length are entered. If a screw does not belong to any of the categories described above, the message Unknown screw type should be output. In the following we present two options for solving this problem. The first case is traditional structured programming, which we have used successfully in many cases. With this programming paradigm we can quickly find a solution to a given problem. The main disadvantage of this approach is that the structure of the data must be known and there is no logical connection between data and the operations that can be applied to it. In many cases, the result is rigid, inflexible and difficult to extend code. In the second case, we use an object-oriented approach, in which related data and the program logic working on it are combined into one unit. At first glance, this program code requires a little more work from the programmer, especially since a concept has to be created by several classes. The great advantage of this is that we get a program code that is easily expandable, modular and reusable for other similar problems. Structured programming The simplest and shortest implementation takes place via a method determined price with the help of if-else constructs:

3 2.1 Introductory example 17 Although this program text is written quickly, it has numerous disadvantages, as we will see in a moment. Object-oriented programming In the real world there is a price group for the screw types. We are now reproducing this price group with a class price group. In addition to the price, the attributes are the lower and upper limits for the diameter and length.

4 18 2 Object-oriented design samples The check as to whether a given diameter and a given length belong to a price group is carried out using the check price method. This receives the two parameters of the diameter to be tested and the length to be tested. The method then compares these two parameters with its own attributes. This method only needs to be written once, as individual price group objects have been created with different parameters. We can do without the nested if-else loops. If the transferred size is in the range of the price group, then the method returns true, otherwise false. The getprice method returns the associated price as a double value. Changes such as a tax surcharge can easily be added in this method. In the TestSchraub class we now define the 5 price group objects in the form of a field of the price group type. Then we go through all price groups one after the other and compare whether the diameter to be tested and the length to be tested are available. At the end, the result is output.

5 2.1 Introductory example 19 edition The price is 0.60 euros. The object-oriented implementation is a bit more extensive at first glance, but it has numerous advantages. The two classes are clearer and easier to understand. Changes such as adding or leaving out equal signs in the if query only require 1 change instead of 5 as in the implementation above. This means that new price groups can easily be added without making the program code faulty again. Due to the fact that the program is divided into two sub-classes, the price group class can easily be reused for other products.

6 20 2 Object-oriented design patterns 2.2 Object-oriented analysis The basis of the object-oriented programming of a software system is the modeling of the task using cooperating objects. Objects are special data and program structures that have properties (attributes) and behaviors (methods). In object-oriented analysis, the objects to be modeled have to be found, organized and described. The individual steps can be presented in the following order: 1. Finding the objects In the given problem definition of the software system to be modeled, the objects contained therein are to be found. These objects describe a group of interacting elements in order to model real objects such as cars, customers, orders or articles directly in software. 2. Organization of the objects If there is a large number of objects involved, the objects that belong together are put together in groups. This combination results from the relationships between the individual objects. For example, objects can contain other objects (e.g. order system contains articles, machine contains components). The group togetherness is greater, the more relationships there are between individual objects. 3. Interaction of objects The interaction or association of two objects describes the relationship between two objects. The aggregation describes the composition of an object from other objects. Composition is a special case of aggregation in which there are dependencies between the objects in such a way that a described object only exists through certain sub-objects (e.g. machine consists of parts, container consists of containers, container consists of objects). 4. Description of the attributes of the objects The attributes are the individual properties for defining the current status of the object. The attribute is a data element of a class that is present in all objects (e.g. color of a car, name of an article, length of a container). 5. Description of the behavior of the objects The behavior of an object is defined by methods within a class, each of which operates on an object of this class. The methods define a task in the form of operations on the instance of a class (e.g. algorithm controls machine, calculation calculates the price of an article).

7 2.2 Object-oriented analysis 21 The result of this analysis is the above description as well as the creation of illustrations with the individual classes (boxes) and their relationship to one another (lines and text). Example 2.1 We model a software for the production planning of a company. The software system consists of an input mask in which the customer has to enter the order data and the production planning data. The order data consists of the individual articles and the production planning data from the production machine and its configuration. 1. Finding the objects: input mask, customer, order, article, production planning, production machine, configuration 2. Organization of the objects: The objects are divided into 4 groups: (a) input mask (b) customer (c) order, article (d) Production planning, production machine, configuration 3. Interaction of the objects: Customer, order and production planning are entered via the input mask. Article belongs to the order production machine is assigned to production planning Configuration is selected by production planning 4. Description of the attributes of the objects: Customer: Name, address, customer number Article: Number, name, price, number of orders: Array of articles Configuration: Machine parameters Production machine: Machine identifier, Runtime production planning: production machine, configuration 5. Description of the behavior of the objects: Customer: getcustomer number, getcustomer, ... Order: get order value, number of items orders, ... Item: getarticle number, getarticle number, ... Production planning: getconfiguration, ... Configuration: getparameter, setparameter, ... production machine: getmachine identifier, ...

8 22 2 Object-oriented design patterns Fig. 2.1 Object-oriented analysis of planning software Fig. 2.1 shows an overview diagram of the object-oriented analysis of this planning software. 2.3 Object-oriented concepts Object-oriented programming is very well suited to dividing extensive programs into a group of interacting objects in order to meet a number of important programming principles: Flexibility: Classes are not rigidly adapted to a given problem, so that changes can be made without any problems are. Reusability: Individual modules can be reused in new applications, so that the costs of developing new systems are reduced. Expandability: The system can easily be expanded with new functionality, for example to optimally add new library classes. Changeability: Changes in one place do not affect other parts of the system. Maintainability: The system only consists of a few dependencies, so that it can be further developed cost-effectively over time. Freedom from redundancy: The system does not have multiple uses of identical or similar code. Comprehensibility: The system is logically structured using easily readable code so that changes can be implemented quickly and safely.

9 2.3 Object-oriented concepts 23 The aim of object-oriented programming is to minimize the dependencies of the various modules, to ensure correctness and to create an understandable and well-documented program package. All of the concepts presented in this chapter are designed to put these principles into practice. In the context of object-oriented programming, the term refactoring describes changing the program code without expanding the functionality. Code refactoring is performed to ensure extensibility and maintainability. In the case of larger program packages, it is often very useful to identify suboptimal code parts in certain development steps, to clear them up and to prepare them for possible further extensions.Internal classes In Java it is possible to include a class in another class, for example to only define local type declarations that do not have any need further visibility. We now introduce a few different variants for defining inner classes. Inner classes are often very useful for designing auxiliary data structures that are only needed in special classes. You will also find application in some design patterns of software engineering. Member class A member class of an outer class is similar to an attribute and is defined as follows: class OuterClass {class InnerClass {...}} To create an object of the inner class, an object of the outer class must exist: OuterClass out = new OuterClass ( ); Inner class in = out.new inner class (); To access a variable var of the outer class, which is covered by inner classes, from the inner class, we use the command OuterClass.this. var.

10 24 2 Object-Oriented Design Pattern Example 2.2 We define an outer and an inner class with a method output: Output Inside Outside General explanation First an object out of the outer class OutsideClass is created. An object can then be defined in the inner class InnerClass using the variable out. The output method of the inner class can then be accessed using the object variable in. Static Inner Class With a static inner class, an inner class is defined as a kind of static property in the outer class. The difference to the member class is the keyword static: class outer class {static class inner class {...}}