Wednesday, 28 June 2017

Software Engineering: Composite Pattern

Structural Pattern - Composite Pattern


Intent
Compose objects into tree structures to represent part-whole hierarchies. Composite lets clients treat individual objects and compositions of objects uniformly.

Why is this used?
This allows us to build structures of objects in the form of trees that contain both compositions of objects and individual object nodes.

Using a Composite structure, we can apply the same operations over both composites and individual objects. In other words, in most cases, we can ignore the difference between the composition of objects and individual objects.


UML diagram representation

https://www.codeproject.com/KB/Blogs/186192/1.jpg

Source code example

https://www.tutorialspoint.com/design_pattern/composite_pattern.htm

More to follow...

Software Engineering: Command Pattern

Behavioural Pattern - Command Pattern


Intent
Encapsulate a request as an object, thereby letting you parameterise client with different requests, queue or log requests, and support undo-able operations.

Why is this used?
This is a way for us to have a common interface to the behaviour of many different receivers each with its own set of actions.


UML diagram representation

https://www.codeproject.com/KB/Blogs/186192/1.jpg

Source code example

https://www.tutorialspoint.com/design_pattern/command_pattern.htm


More to follow...

Software Engineering: State Pattern

Behavioural Pattern - State Pattern


Intent
This has a set of behaviours encapsulated in state objects; at any time the context is delegating to one of those states. The client usually knows very little, if anything about the state objects.

Why is this used?
Allows the object to alter its behaviour when its internal state changes. The object will appear to change its class.


UML diagram representation

http://www.devlake.com/design-patterns/state/state2.png

Source code example

https://www.tutorialspoint.com/design_pattern/state_pattern.htm


More to follow...

Monday, 26 June 2017

Software Engineering: Factory Method Pattern

Creational Pattern - Strategy Pattern


Intent
Define an interface for creating an object, but let sub classes decide which class to instantiate. Factory Method lets a class defer instantiation to sub classes.

Why is this used?
This is useful for when you only have one concrete creator, because you are decoupling the implementation of the sub class from its use.

If you add additional sub class or change a sub classes implementation, this will not affect the Creator. As the Creator is not tightly coupled with the sub classes.


UML diagram representation

https://www.tutorialspoint.com/design_pattern/images/factory_pattern_uml_diagram.jpg

Source code example
public class FactoryPatternDemo {

   public static void main(String[] args) {
      ShapeFactory shapeFactory = new ShapeFactory();

      //get an object of Circle and call its draw method.
      Shape shape1 = shapeFactory.getShape("CIRCLE");

      //call draw method of Circle
      shape1.draw();

      //get an object of Rectangle and call its draw method.
      Shape shape2 = shapeFactory.getShape("RECTANGLE");

      //call draw method of Rectangle
      shape2.draw();

      //get an object of Square and call its draw method.
      Shape shape3 = shapeFactory.getShape("SQUARE");

      //call draw method of circle
      shape3.draw();
   }
}
https://www.tutorialspoint.com/design_pattern/factory_pattern.htm

More to follow...

Sunday, 25 June 2017

Software Engineering: Strategy Pattern

Behavioural Pattern - Strategy Pattern


Intent
Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from clients that use it.

Similar to Template Method, but instead of changing the entire class, this enables an algorithm's behaviour to be selected at run time as an object.

How is this applied?
  • For variants of an algorithm. 
  • For example you might define an algorithms reflecting different space/time-trade offs.

UML diagram representation
Strategy example

https://sourcemaking.com/files/v2/content/patterns/Strategy_example1-2x.png

More to follow...

Software Engineering: Template Method Pattern

Behavioural Pattern - Template Method


Intent
Defines the skeleton of an algorithm in an operation, deferring some steps to sub classes. Template Method sub-classes redefine certain steps of an algorithm without changing the algorithms structure.

Why is this used?
This ensures the algorithm's structure stays unchanged, while sub classes provide some part of the implementation.

How is this applied?
  • An abstract class will contain the template methods
  • This will then be used as abstract versions of the operations used in the template method
  • There may well be many sub classes that want to implement the full set of operations in the template method


UML diagram representation 
https://sourcemaking.com/files/v2/content/patterns/Template_method_example-2x.png


More to follow...

Thursday, 22 June 2017

Software Engineering: Singleton Pattern

Object Creation - Singleton

Intent
This design pattern restricts the instantiation of a class to one object. The purpose of this is to control object creation by keeping constructor private. This applies global access to the instance. 

How is this applied?
  • Make the constructor in the class as private, this stops us from creating another object of this outside of its class. 
  • This helps us to keep only one instance of a class at any time.

Examples of usage?
For a database, you may only want one connection per user of an application. 


UML diagram representation
https://en.wikipedia.org/wiki/Singleton_pattern#/media/File:Singleton_UML_class_diagram.svg


More to follow...

Wednesday, 21 June 2017

Software Engineering: Design Patterns

Introduction
For this part of my blog, I will discuss design patterns which were covered at University. Hopefully, I will be able through as many as possible in the forthcoming weeks. This is purely for my consolidation of knowledge and academic research for those who are reading.

So to start with...
What are software design patterns?
Well in software engineering we follow key principles in which makes the software work. These principles are design, development, maintenance, testing and evaluation of the software. Design patterns are a key part to developing robust solutions to common problems, what design patterns follow is simply a solution that is recognised within the industry of software development, that also improves understanding of software design as these designs are reusable, these solutions are considered a great way of solving problems within a given context of software design.

How are design patterns applied?
Often UML class diagrams are used to show the representation of the software design. What if we are using Object-Oriented design patterns as these diagrams will usually show relations and interactions between classes or objects that are involved.


Below is an image of some of the different design patterns I will be covering.
https://image.slidesharecdn.com/patterns-msc-110923034042-phpapp02/95/design-patterns-16-728.jpg?cb=1316749339

Creational patterns: this focuses on object creation.

Behavioural patterns: this identifies common communication between objects and realises these patterns. By doing so, these patterns increase the potential flexibility in carrying out communication.

Structural patterns: this eases the design by identifying a simple way to realise the relationship between entities.

Concurrency patterns: this deals specifically with multi-threaded programming paradigm.


More to follow... Next will be looking at specific design patterns in detail


Software Engineering: Bad Smells! (Smelly code)

Bad Smells! What are bad smells? This refers to any symptom in the source code of a program that possibly indicates a deeper problem.  ...