SlideShare a Scribd company logo
1 of 8
Download to read offline
International Journal of Advanced Information Technology (IJAIT) Vol. 4, No. 6, December 2014
DOI : 10.5121/ijait.2014.4602 19
EXACT IMPLEMENTATION OF DESIGN PATTERNS IN
C# LANGUAGE
Viliam Malcher
Faculty of Management, Department of Information Systems, Comenius University
820 05 Bratislava 25, Odbojárov 10, P.O. Box 95, Slovak Republic, Europe
ABSTRACT
A design pattern is a general solution to a commonly occurring problem in software design. It is a
template to solve a problem that can be used in many different situations. Patterns formalize best practices
that the programmer can use to solve common problems when designing an application or systems. In this
article we have focused our attention on it, how the proposed UML diagrams can be implemented in C#
language and whether it is possible to make the diagram implementation in the program code with the
greatest possible precision.
.
KEYWORDS
Design patterns, UML diagrams, C# language, Implementation of design patterns
1. INTRODUCTION
Written program could be flexible, easily maintainable, and reusable, it means might be elegant.
How we know that a program is elegant as it can be? The answer is that a successful programmer
has two primary tools − a good programming language and design patterns.
Design patterns represent some templates to create a program that we can use for some group of
similar tasks. For example, let us consider a picture. We want to add borders and captions to the
picture. Or let us to consider a user and an admin (administrator) type of rights. What do these
things have in common? At a first view it seems that nothing. However, with picture frame and
labels, the picture can be seen as that, which is "decorated" with frames and labels. Similarly,
admin rights can be seen as the user rights, and some rights more. So, the admin rights we do not
have to program from the basic state, it is enough to create admin rights by specifying of the user
– admin rights will be a decoration of user rights. For these types of tasks is proposed a model
called the Decorator [1].
In this paper we discuss how for proposed UML diagrams [2-4] of design patterns can be
implemented programs in C# language exactly as it could be. For some patterns were set up
differently the UML diagrams [5] if we compare it with the GoF [1]. For the pattern represented
by the UML diagram does not exist sufficiently accurate software implementation, so that we can
meet with different implementations of the patterns sometimes. This can reduce their
understanding. Our aim is to show how this can be improved.
International Journal of Advanced Information Technology (IJAIT) Vol. 4, No. 6, December 2014
20
2. DESIGN PATTERNS AND UML DIAGRAMS
In the original work of the GoF group [1] were introduced the 23 design patterns, which are
considered as basic patterns. Every design pattern has a corresponding UML diagram. However,
even for same patterns were established different UML diagrams [5]. Then we have two
problems. The first problem is to determine the UML diagram for the pattern. Most programs are
based on the proposal, which did GoF. The second problem is how make sufficiently accurately
software implementation of the UML diagrams for the design patterns.
Take as an example the Decorator pattern. The GoF have proposed for this pattern following
diagram [1]:
Figure 1. The UML diagram proposed by the GoF group [1]
In the book [1] was given a fragment of code in C++ language, how it could be programmatically
implemented the UML diagram shown in Figure 1. J. Bishop has proposed a slightly modified the
UML diagram for the implementation of the Decorator [5] shown in Figure 2
Fig. 2. The UML diagram for Decorator proposed in the book [5]
International Journal of Advanced Information Technology (IJAIT) Vol. 4, No. 6, December 2014
21
It also were implemented the program code in C# language based on the diagram (Figure 2).
Some parts of the code are problematic in terms of the exact implementation. In the book [5] it
had not been established software implementing of association, aggregation, composition, and
other relations between the classes. Our task is therefore to implement software for the basic
relationships between two classes in C# and based on them to create a C# code of design patterns.
We do it for the Decorator pattern as an example.
The UML diagrams are not often an exact copy of the program. In the diagram are shown
important elements. Accessibility of all attributes and operations are appropriately marked
(private (-), protected (#), public (+)), attributes are usually private and methods are public. Types
associated with the operations are usually not mentioned. If however the types are important, they
can be inserted after the colon. The same procedure is applied for parameters of the methods.
Designing an application using object-oriented programming provides inheritance,
polymorphism, encapsulation, etc. Apart from these concepts, it is very important to understand
some logical concepts along with technical concepts. In this article, we will be discussing some of
the concepts, which we use in most of the classes.
Association. Association is defined as a structural relationship, which conceptually means that the
two classes are linked to each other. In this relationship one class instance uses the other class
instance or vice-versa, or both may be using each other. But the main point is, that the lifetime of
the instances of two classes are independent of each other and there is no ownership between two
classes.
Consider an example of Student-Teacher relationship. Conceptually speaking, each student can
be associated to multiple teachers and each teacher can associate to multiple students. We have
two classes Student and Teacher having the association relationship. Both classes are on an equal
level, there is no ownership. This means that it is not possible to create an object instance of the
class in the second class. On the other hand, the Student class can call the methods, which are
present in the class Teacher, this reflects the direction of the arrow in the diagram. The class
Teacher will not call the method created in the Student class. We make an implementation of this
relationship in C# language as follows:
using System;
class Program {
class Teacher {
public void MethodT() { }
}
class Student {
public Student(Teacher t) {
t.MethodT();
}
}
static void Main() {
Teacher t = new Teacher();
Student s = new Student(t);
Console.ReadLine();
}
}
Listing 1: The association relation between classes
International Journal of Advanced Information Technology (IJAIT) Vol. 4, No. 6, December 2014
22
The diagram for the association is shown in Figure 3
Figure 3. The UML diagram for the association
The constructors, properties, and void in methods we will not showed in the diagram.
Aggregation. In the case of aggregation each object has its own life cycle. The diagram that
represents the type of aggregation relationship shows a line at the end of which is empty diamond.
Let us consider an example of two classes Person and Address, that are related the aggregation.
We propose for it the following structural code in C#:
using System;
class Program {
class Address { }
class Person {
Address address;
public Person(Address a) {
address = a;
}
}
static void Main() {
Address address = new Address();
Person person = new Person(address);
Console.ReadLine();
}
}
Listing 2: The aggregation relation between classes
The design of diagram for the aggregation reads:
Figure 4. The UML diagram for the aggregation
It should be noted here that the address object is created outside of the class Person.
Composition. In the case of the composition a child object has no its life cycle, and if its parent
object is removed, the child object will also be deleted. Let us take an example of the relationship
between a house and a room. The house can contain multiple rooms, there is no independent
living room and each room can not belong to two different houses. If we delete the house, the
room will be automatically removed. Thus, this object will be canceled if canceled an object of
house type. The implementation of the composition by C# language is as follows
International Journal of Advanced Information Technology (IJAIT) Vol. 4, No. 6, December 2014
23
using System;
using System.Collections;
class Program {
class Room {
string roomType;
public Room(string roomType) {
this.roomType = roomType;
}
public string RoomType {
get { return roomType; }
}
}
class House {
ArrayList roomList = new ArrayList();
public void CreateRoom(string roomType){
Room r = new Room(roomType);
roomList.Add(r.RoomType);
}
}
static void Main() {
House myHouse = new House();
myHouse.CreateRoom("dining Room");
myHouse.CreateRoom("bathroom");
Console.ReadLine();
}
}
Listing 3: The composition relation between classes
The diagram design for the composition reads:
Figure 5. The UML diagram for the composition
If you take as an example of house and room, it would make sense to say that the room is part of
house; the room life is designed lifetime of the house. If the house is destroyed, the rooms are
destroyed along with it.
Dependency. This relationship exists between two classes if a change in the definition of one may
cause change to the other. The relationship is display by a dashed line with an open arrow:
Figure 6. The UML diagram for the dependency
International Journal of Advanced Information Technology (IJAIT) Vol. 4, No. 6, December 2014
24
The implementation of this diagram for the dependency could be as follows:
using System;
class Program {
class B {
public string MethodB() {
return "MethodB is called";
}
}
class A {
public B MethodA() {
return new B();
}
}
static void Main() {
B b;
A a = new A();
b = a.MethodA();
Console.WriteLine(b.MethodB());
Console.ReadKey();
}
}
Listing 4: The example of the dependency
The above implementation in C# language of the basic relationship between two classes now
enables us to create a software of the design patterns. We have constructed the basic building
blocks a they can by used for more complicated diagrams. Let us consider the following diagram
(the Decorator pattern):
Figure 7. The UML diagram of the Decorator with an interface
We have defined the basic rules above for the constructing of the basic diagrams and it means in
the C# language the code reads
International Journal of Advanced Information Technology (IJAIT) Vol. 4, No. 6, December 2014
25
using System;
class Program {
interface IComponent {
string Operation();
}
class Component : IComponent {
public string Operation() {
return "I am walking";
}
}
class Decorator : IComponent {
IComponent component;
public Decorator(IComponent c) {
component = c;
}
public string Operation() {
string s = component.Operation();
s += " and listening my music";
return s;
}
public string AddedBehavior() { }
}
static void Main() {
IComponent component = new Component();
IComponent c = new Decorator(component);
Console.WriteLine(c.Operation());
Console.ReadLine();
}
}
Listing 5: The Decorator pattern
3. CONCLUSIONS
We have shown that if we define the programs for the basic relationships between two classes, it
is possible to achieve more accurate software implementations of the UML diagram. We can also
create diagrams for the designed software, which allow us to keep the software in a graphic form
in the documents. Really, we can more exactly make the program implementation from designed
the UML diagrams [6,7], and after further filled some elements in the diagrams this state could be
still improved.
REFERENCES
[1] E. Gamma., R. Helm, R. Johnson, and J. O. Vlissides, Design Patterns: Elements of Reusable Object-
Oriented Software, M. A. Addison-Wesley, Boston, 1995.
[2] J. Rumbaugh, I. Jacobson, G. Booch, The Unified Modeling Language Reference Manual, Addison-
Wesley Longman, Inc., 1999.
[3] M. Fowler, UML destilled, 3th ed., Addison-Wesley, Pearson Education, Inc., 2004.
[4] J. Arlow, and I. Neustadt, UML and the Unified Process, Pearson Education Limited, 2002.
[5] J. Bishop, C# 3.0 Design Patterns, O’Reilly Media, Inc., 2007.
[6] Ch. G. Lasater, Design Patterns, Wordware Publishing, Inc., 2007.
[7] McC. J. Smith, Elemental Design Patterns, Person Education, Inc., 2012.
International Journal of Advanced Information Technology (IJAIT) Vol. 4, No. 6, December 2014
26
AUTHOR
Viliam Malcher born in Bratislava, Slovak Republic. He graduated from the Faculty of
Mathematics, Physics and Informatics, Comenius University, Bratislava, in 1981. From
1983 he was engaged in the industry as a researcher. After he received PhD degree in
1995 worked in Computer Centre at Comenius University and from 2001 in Analytical
Laboratories as IT worker. In this time he joined the Faculty of Management,
Department of Information Systems at Comenius University. He is now a teacher and an
associate professor. His research interests include programming, mainly on the .NET
platform, object oriented analysis, and quantum computing.

More Related Content

What's hot

Uml Presentation
Uml PresentationUml Presentation
Uml Presentation
mewaseem
 
Csc1100 elements of programming (revised july 2014) 120lh-2-student
Csc1100 elements of  programming (revised july 2014) 120lh-2-studentCsc1100 elements of  programming (revised july 2014) 120lh-2-student
Csc1100 elements of programming (revised july 2014) 120lh-2-student
IIUM
 

What's hot (19)

Interfaces & Packages V2
Interfaces & Packages V2Interfaces & Packages V2
Interfaces & Packages V2
 
Uml lecture
Uml lectureUml lecture
Uml lecture
 
An integration of uml use case diagram and activity diagram with Z language f...
An integration of uml use case diagram and activity diagram with Z language f...An integration of uml use case diagram and activity diagram with Z language f...
An integration of uml use case diagram and activity diagram with Z language f...
 
Towards a semantic for uml activity diagram based on institution theory for i...
Towards a semantic for uml activity diagram based on institution theory for i...Towards a semantic for uml activity diagram based on institution theory for i...
Towards a semantic for uml activity diagram based on institution theory for i...
 
Uml basics
Uml basicsUml basics
Uml basics
 
OO Development 4 - Object Concepts
OO Development 4 - Object ConceptsOO Development 4 - Object Concepts
OO Development 4 - Object Concepts
 
Uml class diagram and packages ppt for dot net
Uml class diagram and packages ppt for dot netUml class diagram and packages ppt for dot net
Uml class diagram and packages ppt for dot net
 
UML Diagram Assignment Help, UML Diagram Homework Help
UML Diagram Assignment Help, UML Diagram Homework HelpUML Diagram Assignment Help, UML Diagram Homework Help
UML Diagram Assignment Help, UML Diagram Homework Help
 
PYFML- A TEXTUAL LANGUAGE FOR FEATURE MODELING
PYFML- A TEXTUAL LANGUAGE FOR FEATURE MODELINGPYFML- A TEXTUAL LANGUAGE FOR FEATURE MODELING
PYFML- A TEXTUAL LANGUAGE FOR FEATURE MODELING
 
Uml Presentation
Uml PresentationUml Presentation
Uml Presentation
 
Unified Modeling Language
Unified Modeling LanguageUnified Modeling Language
Unified Modeling Language
 
Csc1100 elements of programming (revised july 2014) 120lh-2-student
Csc1100 elements of  programming (revised july 2014) 120lh-2-studentCsc1100 elements of  programming (revised july 2014) 120lh-2-student
Csc1100 elements of programming (revised july 2014) 120lh-2-student
 
UML Class Diagram Notation
UML Class Diagram NotationUML Class Diagram Notation
UML Class Diagram Notation
 
Lecture-03 Introduction to UML
Lecture-03 Introduction to UMLLecture-03 Introduction to UML
Lecture-03 Introduction to UML
 
Java OOPS Concept
Java OOPS ConceptJava OOPS Concept
Java OOPS Concept
 
class diagram
class diagramclass diagram
class diagram
 
Uml Diagrams for Web Developers
Uml Diagrams for Web DevelopersUml Diagrams for Web Developers
Uml Diagrams for Web Developers
 
Uml class Diagram
Uml class DiagramUml class Diagram
Uml class Diagram
 
Uml Tutorial
Uml TutorialUml Tutorial
Uml Tutorial
 

Viewers also liked

Salud Reproductivae Enf. Transmision Sexual
Salud Reproductivae Enf. Transmision SexualSalud Reproductivae Enf. Transmision Sexual
Salud Reproductivae Enf. Transmision Sexual
abrahamjair
 
Fines, autonomía y descentralizacion municipal
Fines, autonomía y descentralizacion municipalFines, autonomía y descentralizacion municipal
Fines, autonomía y descentralizacion municipal
franksvdt
 
Ruang 8- Kasus 1 Modul Penurunan Kesadaran
Ruang 8- Kasus 1 Modul Penurunan KesadaranRuang 8- Kasus 1 Modul Penurunan Kesadaran
Ruang 8- Kasus 1 Modul Penurunan Kesadaran
Amelia Manatar
 
mangcoban_k07406taminhchau
mangcoban_k07406taminhchaumangcoban_k07406taminhchau
mangcoban_k07406taminhchau
Vo Oanh
 
El Lissitzky. The experience of totality. MART-Museo di Arte Moderna e Contem...
El Lissitzky. The experience of totality. MART-Museo di Arte Moderna e Contem...El Lissitzky. The experience of totality. MART-Museo di Arte Moderna e Contem...
El Lissitzky. The experience of totality. MART-Museo di Arte Moderna e Contem...
CarmenMarinMartinez
 

Viewers also liked (20)

Integrated report-banner
Integrated report-bannerIntegrated report-banner
Integrated report-banner
 
Tổ ấm Sunview town
Tổ ấm Sunview townTổ ấm Sunview town
Tổ ấm Sunview town
 
Erick Mol livro digital
Erick Mol livro digitalErick Mol livro digital
Erick Mol livro digital
 
Institucionalizando la empresa familiar
Institucionalizando la empresa familiarInstitucionalizando la empresa familiar
Institucionalizando la empresa familiar
 
Ejercicios capitulo 6
Ejercicios capitulo 6Ejercicios capitulo 6
Ejercicios capitulo 6
 
Salud Reproductivae Enf. Transmision Sexual
Salud Reproductivae Enf. Transmision SexualSalud Reproductivae Enf. Transmision Sexual
Salud Reproductivae Enf. Transmision Sexual
 
Fines, autonomía y descentralizacion municipal
Fines, autonomía y descentralizacion municipalFines, autonomía y descentralizacion municipal
Fines, autonomía y descentralizacion municipal
 
Radio broadcasting process
Radio broadcasting processRadio broadcasting process
Radio broadcasting process
 
Ruang 8- Kasus 1 Modul Penurunan Kesadaran
Ruang 8- Kasus 1 Modul Penurunan KesadaranRuang 8- Kasus 1 Modul Penurunan Kesadaran
Ruang 8- Kasus 1 Modul Penurunan Kesadaran
 
Stoa: a Web social na universidade
Stoa: a Web social na universidadeStoa: a Web social na universidade
Stoa: a Web social na universidade
 
mangcoban_k07406taminhchau
mangcoban_k07406taminhchaumangcoban_k07406taminhchau
mangcoban_k07406taminhchau
 
Hyatt Airport Exterior Ii
Hyatt Airport Exterior IiHyatt Airport Exterior Ii
Hyatt Airport Exterior Ii
 
Denuncia penal
Denuncia penal Denuncia penal
Denuncia penal
 
Finance and Valuation Basics
Finance and Valuation BasicsFinance and Valuation Basics
Finance and Valuation Basics
 
El Lissitzky. The experience of totality. MART-Museo di Arte Moderna e Contem...
El Lissitzky. The experience of totality. MART-Museo di Arte Moderna e Contem...El Lissitzky. The experience of totality. MART-Museo di Arte Moderna e Contem...
El Lissitzky. The experience of totality. MART-Museo di Arte Moderna e Contem...
 
Education and Outreach: towards Whom and at What Level? [Djafer Benachour, Un...
Education and Outreach: towards Whom and at What Level? [Djafer Benachour, Un...Education and Outreach: towards Whom and at What Level? [Djafer Benachour, Un...
Education and Outreach: towards Whom and at What Level? [Djafer Benachour, Un...
 
sisvsp2012_sessione3_da valle_faustini_tessitore_valentini
sisvsp2012_sessione3_da valle_faustini_tessitore_valentinisisvsp2012_sessione3_da valle_faustini_tessitore_valentini
sisvsp2012_sessione3_da valle_faustini_tessitore_valentini
 
Building Highly Scalable and Flexible SaaS Solutions
Building Highly Scalable and Flexible SaaS SolutionsBuilding Highly Scalable and Flexible SaaS Solutions
Building Highly Scalable and Flexible SaaS Solutions
 
Alternatif Değerlendirme Teknikleri
Alternatif Değerlendirme TeknikleriAlternatif Değerlendirme Teknikleri
Alternatif Değerlendirme Teknikleri
 
Hug france - Administration Hadoop et retour d’expérience BI avec Impala, lim...
Hug france - Administration Hadoop et retour d’expérience BI avec Impala, lim...Hug france - Administration Hadoop et retour d’expérience BI avec Impala, lim...
Hug france - Administration Hadoop et retour d’expérience BI avec Impala, lim...
 

Similar to Exact implementation of design patterns in

Design patterns in_c_sharp
Design patterns in_c_sharpDesign patterns in_c_sharp
Design patterns in_c_sharp
Cao Tuan
 
Andy Bulka Pattern Automation
Andy Bulka Pattern AutomationAndy Bulka Pattern Automation
Andy Bulka Pattern Automation
tcab22
 
Phase 2 - Task 1Task TypeDiscussion BoardDeliverable Length.docx
Phase 2 - Task 1Task TypeDiscussion BoardDeliverable Length.docxPhase 2 - Task 1Task TypeDiscussion BoardDeliverable Length.docx
Phase 2 - Task 1Task TypeDiscussion BoardDeliverable Length.docx
randymartin91030
 
Object oriented-programming-in-c-sharp
Object oriented-programming-in-c-sharpObject oriented-programming-in-c-sharp
Object oriented-programming-in-c-sharp
Abefo
 
Systems variability modeling a textual model mixing class and feature concepts
Systems variability modeling a textual model mixing class and feature conceptsSystems variability modeling a textual model mixing class and feature concepts
Systems variability modeling a textual model mixing class and feature concepts
ijcsit
 

Similar to Exact implementation of design patterns in (20)

Design patterns in_c_sharp
Design patterns in_c_sharpDesign patterns in_c_sharp
Design patterns in_c_sharp
 
DOOML: A NEW DATABASE & OBJECT-ORIENTED MODELING LANGUAGE FOR DATABASE-DRIVEN...
DOOML: A NEW DATABASE & OBJECT-ORIENTED MODELING LANGUAGE FOR DATABASE-DRIVEN...DOOML: A NEW DATABASE & OBJECT-ORIENTED MODELING LANGUAGE FOR DATABASE-DRIVEN...
DOOML: A NEW DATABASE & OBJECT-ORIENTED MODELING LANGUAGE FOR DATABASE-DRIVEN...
 
DOOML: A New Database & Object-Oriented Modeling Language for Database-Driven...
DOOML: A New Database & Object-Oriented Modeling Language for Database-Driven...DOOML: A New Database & Object-Oriented Modeling Language for Database-Driven...
DOOML: A New Database & Object-Oriented Modeling Language for Database-Driven...
 
Improving Consistency of UML Diagrams and Its Implementation Using Reverse En...
Improving Consistency of UML Diagrams and Its Implementation Using Reverse En...Improving Consistency of UML Diagrams and Its Implementation Using Reverse En...
Improving Consistency of UML Diagrams and Its Implementation Using Reverse En...
 
PATTERNS01 - An Introduction to Design Patterns
PATTERNS01 - An Introduction to Design PatternsPATTERNS01 - An Introduction to Design Patterns
PATTERNS01 - An Introduction to Design Patterns
 
Andy Bulka Pattern Automation
Andy Bulka Pattern AutomationAndy Bulka Pattern Automation
Andy Bulka Pattern Automation
 
Apostila UML
Apostila UMLApostila UML
Apostila UML
 
UML Design.pptx
UML Design.pptxUML Design.pptx
UML Design.pptx
 
Phase 2 - Task 1Task TypeDiscussion BoardDeliverable Length.docx
Phase 2 - Task 1Task TypeDiscussion BoardDeliverable Length.docxPhase 2 - Task 1Task TypeDiscussion BoardDeliverable Length.docx
Phase 2 - Task 1Task TypeDiscussion BoardDeliverable Length.docx
 
Object Oriented Database
Object Oriented DatabaseObject Oriented Database
Object Oriented Database
 
Object oriented-programming-in-c-sharp
Object oriented-programming-in-c-sharpObject oriented-programming-in-c-sharp
Object oriented-programming-in-c-sharp
 
Systems variability modeling a textual model mixing class and feature concepts
Systems variability modeling a textual model mixing class and feature conceptsSystems variability modeling a textual model mixing class and feature concepts
Systems variability modeling a textual model mixing class and feature concepts
 
Diagramming the Class Diagram: Toward a Unified Modeling Methodology
Diagramming the Class Diagram: Toward a Unified Modeling Methodology Diagramming the Class Diagram: Toward a Unified Modeling Methodology
Diagramming the Class Diagram: Toward a Unified Modeling Methodology
 
Model versioning in context of living
Model versioning in context of livingModel versioning in context of living
Model versioning in context of living
 
A NATURAL LANGUAGE REQUIREMENTS ENGINEERING APPROACH FOR MDA
A NATURAL LANGUAGE REQUIREMENTS ENGINEERING APPROACH FOR MDA A NATURAL LANGUAGE REQUIREMENTS ENGINEERING APPROACH FOR MDA
A NATURAL LANGUAGE REQUIREMENTS ENGINEERING APPROACH FOR MDA
 
Uml
UmlUml
Uml
 
A NATURAL LANGUAGE REQUIREMENTS ENGINEERING APPROACH FOR MDA
A NATURAL LANGUAGE REQUIREMENTS ENGINEERING APPROACH FOR MDA A NATURAL LANGUAGE REQUIREMENTS ENGINEERING APPROACH FOR MDA
A NATURAL LANGUAGE REQUIREMENTS ENGINEERING APPROACH FOR MDA
 
A NATURAL LANGUAGE REQUIREMENTS ENGINEERING APPROACH FOR MDA
A NATURAL LANGUAGE REQUIREMENTS ENGINEERING APPROACH FOR MDA A NATURAL LANGUAGE REQUIREMENTS ENGINEERING APPROACH FOR MDA
A NATURAL LANGUAGE REQUIREMENTS ENGINEERING APPROACH FOR MDA
 
A Natural Language Requirements Engineering Approach for MDA
A Natural Language Requirements Engineering Approach for MDAA Natural Language Requirements Engineering Approach for MDA
A Natural Language Requirements Engineering Approach for MDA
 
A natural language requirements engineering approach for mda
A natural language requirements engineering approach for mdaA natural language requirements engineering approach for mda
A natural language requirements engineering approach for mda
 

Recently uploaded

+97470301568>> buy weed in qatar,buy thc oil qatar,buy weed and vape oil in d...
+97470301568>> buy weed in qatar,buy thc oil qatar,buy weed and vape oil in d...+97470301568>> buy weed in qatar,buy thc oil qatar,buy weed and vape oil in d...
+97470301568>> buy weed in qatar,buy thc oil qatar,buy weed and vape oil in d...
Health
 
scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...
scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...
scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...
HenryBriggs2
 
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
notes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.pptnotes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.ppt
MsecMca
 
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak HamilCara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Kandungan 087776558899
 

Recently uploaded (20)

Thermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VThermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - V
 
COST-EFFETIVE and Energy Efficient BUILDINGS ptx
COST-EFFETIVE  and Energy Efficient BUILDINGS ptxCOST-EFFETIVE  and Energy Efficient BUILDINGS ptx
COST-EFFETIVE and Energy Efficient BUILDINGS ptx
 
Hostel management system project report..pdf
Hostel management system project report..pdfHostel management system project report..pdf
Hostel management system project report..pdf
 
A Study of Urban Area Plan for Pabna Municipality
A Study of Urban Area Plan for Pabna MunicipalityA Study of Urban Area Plan for Pabna Municipality
A Study of Urban Area Plan for Pabna Municipality
 
Minimum and Maximum Modes of microprocessor 8086
Minimum and Maximum Modes of microprocessor 8086Minimum and Maximum Modes of microprocessor 8086
Minimum and Maximum Modes of microprocessor 8086
 
kiln thermal load.pptx kiln tgermal load
kiln thermal load.pptx kiln tgermal loadkiln thermal load.pptx kiln tgermal load
kiln thermal load.pptx kiln tgermal load
 
+97470301568>> buy weed in qatar,buy thc oil qatar,buy weed and vape oil in d...
+97470301568>> buy weed in qatar,buy thc oil qatar,buy weed and vape oil in d...+97470301568>> buy weed in qatar,buy thc oil qatar,buy weed and vape oil in d...
+97470301568>> buy weed in qatar,buy thc oil qatar,buy weed and vape oil in d...
 
scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...
scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...
scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...
 
Double Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueDouble Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torque
 
Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...
Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...
Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...
 
HAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKAR
HAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKARHAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKAR
HAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKAR
 
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
 
DC MACHINE-Motoring and generation, Armature circuit equation
DC MACHINE-Motoring and generation, Armature circuit equationDC MACHINE-Motoring and generation, Armature circuit equation
DC MACHINE-Motoring and generation, Armature circuit equation
 
Bridge Jacking Design Sample Calculation.pptx
Bridge Jacking Design Sample Calculation.pptxBridge Jacking Design Sample Calculation.pptx
Bridge Jacking Design Sample Calculation.pptx
 
Air Compressor reciprocating single stage
Air Compressor reciprocating single stageAir Compressor reciprocating single stage
Air Compressor reciprocating single stage
 
Hazard Identification (HAZID) vs. Hazard and Operability (HAZOP): A Comparati...
Hazard Identification (HAZID) vs. Hazard and Operability (HAZOP): A Comparati...Hazard Identification (HAZID) vs. Hazard and Operability (HAZOP): A Comparati...
Hazard Identification (HAZID) vs. Hazard and Operability (HAZOP): A Comparati...
 
2016EF22_0 solar project report rooftop projects
2016EF22_0 solar project report rooftop projects2016EF22_0 solar project report rooftop projects
2016EF22_0 solar project report rooftop projects
 
notes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.pptnotes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.ppt
 
Engineering Drawing focus on projection of planes
Engineering Drawing focus on projection of planesEngineering Drawing focus on projection of planes
Engineering Drawing focus on projection of planes
 
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak HamilCara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
 

Exact implementation of design patterns in

  • 1. International Journal of Advanced Information Technology (IJAIT) Vol. 4, No. 6, December 2014 DOI : 10.5121/ijait.2014.4602 19 EXACT IMPLEMENTATION OF DESIGN PATTERNS IN C# LANGUAGE Viliam Malcher Faculty of Management, Department of Information Systems, Comenius University 820 05 Bratislava 25, Odbojárov 10, P.O. Box 95, Slovak Republic, Europe ABSTRACT A design pattern is a general solution to a commonly occurring problem in software design. It is a template to solve a problem that can be used in many different situations. Patterns formalize best practices that the programmer can use to solve common problems when designing an application or systems. In this article we have focused our attention on it, how the proposed UML diagrams can be implemented in C# language and whether it is possible to make the diagram implementation in the program code with the greatest possible precision. . KEYWORDS Design patterns, UML diagrams, C# language, Implementation of design patterns 1. INTRODUCTION Written program could be flexible, easily maintainable, and reusable, it means might be elegant. How we know that a program is elegant as it can be? The answer is that a successful programmer has two primary tools − a good programming language and design patterns. Design patterns represent some templates to create a program that we can use for some group of similar tasks. For example, let us consider a picture. We want to add borders and captions to the picture. Or let us to consider a user and an admin (administrator) type of rights. What do these things have in common? At a first view it seems that nothing. However, with picture frame and labels, the picture can be seen as that, which is "decorated" with frames and labels. Similarly, admin rights can be seen as the user rights, and some rights more. So, the admin rights we do not have to program from the basic state, it is enough to create admin rights by specifying of the user – admin rights will be a decoration of user rights. For these types of tasks is proposed a model called the Decorator [1]. In this paper we discuss how for proposed UML diagrams [2-4] of design patterns can be implemented programs in C# language exactly as it could be. For some patterns were set up differently the UML diagrams [5] if we compare it with the GoF [1]. For the pattern represented by the UML diagram does not exist sufficiently accurate software implementation, so that we can meet with different implementations of the patterns sometimes. This can reduce their understanding. Our aim is to show how this can be improved.
  • 2. International Journal of Advanced Information Technology (IJAIT) Vol. 4, No. 6, December 2014 20 2. DESIGN PATTERNS AND UML DIAGRAMS In the original work of the GoF group [1] were introduced the 23 design patterns, which are considered as basic patterns. Every design pattern has a corresponding UML diagram. However, even for same patterns were established different UML diagrams [5]. Then we have two problems. The first problem is to determine the UML diagram for the pattern. Most programs are based on the proposal, which did GoF. The second problem is how make sufficiently accurately software implementation of the UML diagrams for the design patterns. Take as an example the Decorator pattern. The GoF have proposed for this pattern following diagram [1]: Figure 1. The UML diagram proposed by the GoF group [1] In the book [1] was given a fragment of code in C++ language, how it could be programmatically implemented the UML diagram shown in Figure 1. J. Bishop has proposed a slightly modified the UML diagram for the implementation of the Decorator [5] shown in Figure 2 Fig. 2. The UML diagram for Decorator proposed in the book [5]
  • 3. International Journal of Advanced Information Technology (IJAIT) Vol. 4, No. 6, December 2014 21 It also were implemented the program code in C# language based on the diagram (Figure 2). Some parts of the code are problematic in terms of the exact implementation. In the book [5] it had not been established software implementing of association, aggregation, composition, and other relations between the classes. Our task is therefore to implement software for the basic relationships between two classes in C# and based on them to create a C# code of design patterns. We do it for the Decorator pattern as an example. The UML diagrams are not often an exact copy of the program. In the diagram are shown important elements. Accessibility of all attributes and operations are appropriately marked (private (-), protected (#), public (+)), attributes are usually private and methods are public. Types associated with the operations are usually not mentioned. If however the types are important, they can be inserted after the colon. The same procedure is applied for parameters of the methods. Designing an application using object-oriented programming provides inheritance, polymorphism, encapsulation, etc. Apart from these concepts, it is very important to understand some logical concepts along with technical concepts. In this article, we will be discussing some of the concepts, which we use in most of the classes. Association. Association is defined as a structural relationship, which conceptually means that the two classes are linked to each other. In this relationship one class instance uses the other class instance or vice-versa, or both may be using each other. But the main point is, that the lifetime of the instances of two classes are independent of each other and there is no ownership between two classes. Consider an example of Student-Teacher relationship. Conceptually speaking, each student can be associated to multiple teachers and each teacher can associate to multiple students. We have two classes Student and Teacher having the association relationship. Both classes are on an equal level, there is no ownership. This means that it is not possible to create an object instance of the class in the second class. On the other hand, the Student class can call the methods, which are present in the class Teacher, this reflects the direction of the arrow in the diagram. The class Teacher will not call the method created in the Student class. We make an implementation of this relationship in C# language as follows: using System; class Program { class Teacher { public void MethodT() { } } class Student { public Student(Teacher t) { t.MethodT(); } } static void Main() { Teacher t = new Teacher(); Student s = new Student(t); Console.ReadLine(); } } Listing 1: The association relation between classes
  • 4. International Journal of Advanced Information Technology (IJAIT) Vol. 4, No. 6, December 2014 22 The diagram for the association is shown in Figure 3 Figure 3. The UML diagram for the association The constructors, properties, and void in methods we will not showed in the diagram. Aggregation. In the case of aggregation each object has its own life cycle. The diagram that represents the type of aggregation relationship shows a line at the end of which is empty diamond. Let us consider an example of two classes Person and Address, that are related the aggregation. We propose for it the following structural code in C#: using System; class Program { class Address { } class Person { Address address; public Person(Address a) { address = a; } } static void Main() { Address address = new Address(); Person person = new Person(address); Console.ReadLine(); } } Listing 2: The aggregation relation between classes The design of diagram for the aggregation reads: Figure 4. The UML diagram for the aggregation It should be noted here that the address object is created outside of the class Person. Composition. In the case of the composition a child object has no its life cycle, and if its parent object is removed, the child object will also be deleted. Let us take an example of the relationship between a house and a room. The house can contain multiple rooms, there is no independent living room and each room can not belong to two different houses. If we delete the house, the room will be automatically removed. Thus, this object will be canceled if canceled an object of house type. The implementation of the composition by C# language is as follows
  • 5. International Journal of Advanced Information Technology (IJAIT) Vol. 4, No. 6, December 2014 23 using System; using System.Collections; class Program { class Room { string roomType; public Room(string roomType) { this.roomType = roomType; } public string RoomType { get { return roomType; } } } class House { ArrayList roomList = new ArrayList(); public void CreateRoom(string roomType){ Room r = new Room(roomType); roomList.Add(r.RoomType); } } static void Main() { House myHouse = new House(); myHouse.CreateRoom("dining Room"); myHouse.CreateRoom("bathroom"); Console.ReadLine(); } } Listing 3: The composition relation between classes The diagram design for the composition reads: Figure 5. The UML diagram for the composition If you take as an example of house and room, it would make sense to say that the room is part of house; the room life is designed lifetime of the house. If the house is destroyed, the rooms are destroyed along with it. Dependency. This relationship exists between two classes if a change in the definition of one may cause change to the other. The relationship is display by a dashed line with an open arrow: Figure 6. The UML diagram for the dependency
  • 6. International Journal of Advanced Information Technology (IJAIT) Vol. 4, No. 6, December 2014 24 The implementation of this diagram for the dependency could be as follows: using System; class Program { class B { public string MethodB() { return "MethodB is called"; } } class A { public B MethodA() { return new B(); } } static void Main() { B b; A a = new A(); b = a.MethodA(); Console.WriteLine(b.MethodB()); Console.ReadKey(); } } Listing 4: The example of the dependency The above implementation in C# language of the basic relationship between two classes now enables us to create a software of the design patterns. We have constructed the basic building blocks a they can by used for more complicated diagrams. Let us consider the following diagram (the Decorator pattern): Figure 7. The UML diagram of the Decorator with an interface We have defined the basic rules above for the constructing of the basic diagrams and it means in the C# language the code reads
  • 7. International Journal of Advanced Information Technology (IJAIT) Vol. 4, No. 6, December 2014 25 using System; class Program { interface IComponent { string Operation(); } class Component : IComponent { public string Operation() { return "I am walking"; } } class Decorator : IComponent { IComponent component; public Decorator(IComponent c) { component = c; } public string Operation() { string s = component.Operation(); s += " and listening my music"; return s; } public string AddedBehavior() { } } static void Main() { IComponent component = new Component(); IComponent c = new Decorator(component); Console.WriteLine(c.Operation()); Console.ReadLine(); } } Listing 5: The Decorator pattern 3. CONCLUSIONS We have shown that if we define the programs for the basic relationships between two classes, it is possible to achieve more accurate software implementations of the UML diagram. We can also create diagrams for the designed software, which allow us to keep the software in a graphic form in the documents. Really, we can more exactly make the program implementation from designed the UML diagrams [6,7], and after further filled some elements in the diagrams this state could be still improved. REFERENCES [1] E. Gamma., R. Helm, R. Johnson, and J. O. Vlissides, Design Patterns: Elements of Reusable Object- Oriented Software, M. A. Addison-Wesley, Boston, 1995. [2] J. Rumbaugh, I. Jacobson, G. Booch, The Unified Modeling Language Reference Manual, Addison- Wesley Longman, Inc., 1999. [3] M. Fowler, UML destilled, 3th ed., Addison-Wesley, Pearson Education, Inc., 2004. [4] J. Arlow, and I. Neustadt, UML and the Unified Process, Pearson Education Limited, 2002. [5] J. Bishop, C# 3.0 Design Patterns, O’Reilly Media, Inc., 2007. [6] Ch. G. Lasater, Design Patterns, Wordware Publishing, Inc., 2007. [7] McC. J. Smith, Elemental Design Patterns, Person Education, Inc., 2012.
  • 8. International Journal of Advanced Information Technology (IJAIT) Vol. 4, No. 6, December 2014 26 AUTHOR Viliam Malcher born in Bratislava, Slovak Republic. He graduated from the Faculty of Mathematics, Physics and Informatics, Comenius University, Bratislava, in 1981. From 1983 he was engaged in the industry as a researcher. After he received PhD degree in 1995 worked in Computer Centre at Comenius University and from 2001 in Analytical Laboratories as IT worker. In this time he joined the Faculty of Management, Department of Information Systems at Comenius University. He is now a teacher and an associate professor. His research interests include programming, mainly on the .NET platform, object oriented analysis, and quantum computing.