Monday, 3 July 2017

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. 

These symptoms do not mean that the program does not function, but instead this shows weaknesses in design that may be slowing development or increasing risk of bugs or failures in the future.

The bad smells...

  1. Repetition: Copied, pasted, slightly modified
  2. Opacity: Bad naming
  3. Needless Complexity: Components add no direct benefit
  4. Rigidity: Change in code will need a cascade of changes
  5. Fragility: Slightest error the system does not usually handle
  6. Immobility: Design hard to re-use (extensiblility)
  7. Viscosity: Shortcuts can be made in the code
  8. Loose/Tight Coupling: Class dependency on each other



Software Engineering: Facade Pattern

Structural Pattern - Facade Pattern

Intent
Provides a unified interface to a set of interfaces in a subsystem. Facade defines a higher-level interface that makes the subsystem easier to use. 

Why is this used?
A facade decouples a client from a complex subsystem. Whilst the Facade subsystem and uses delegation to perform the work of the facade.


UML diagram representation

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

Source code example

https://www.tutorialspoint.com/design_pattern/facade_pattern.htm
More to follow...

Software Engineering: Adaptor Pattern

Structural Pattern - Adaptor Pattern


Intent
Converts the interface of a class into another interface the clients expect. Adaptor lets classes work together that could not otherwise because of incompatible interfaces.  

Why is this used?
Adaptor lets classes work together that could not otherwise because of incompatible interfaces. 


UML diagram representation

Adapter Pattern UML Diagram
https://www.tutorialspoint.com/design_pattern/adapter_pattern.htm

Source code example

https://www.tutorialspoint.com/design_pattern/adapter_pattern.htm
More to follow...

Software Engineering: Decorator Pattern

Structural Pattern - Decorator Pattern

Intent
Attach additional responsibilities to an object dynamically.  

Why is this used?
Decorators provide a flexible alternative to sub-classing for extending functionality.


UML diagram representation


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

Source code example

https://www.tutorialspoint.com/design_pattern/decorator_pattern.htm
More to follow...

Software Engineering: Visitor Pattern

Behavioral Pattern - Visitor Pattern


Intent
Represent an operation to be performed on the elements of an object structure. Visitor lets you define a new operation without changing the classes of the elements on which it operates.

Why is this used?
Handy for recovering lost type information. Allow you to add operations to a Composite structure without changing the structure itself. Also, adding new operations is relatively easy. Furthermore, the code operations performed by the Visitor is centralised.


UML diagram representation


https://www.tutorialspoint.com/design_pattern/images/visitor_pattern_uml_diagram.jpg
Visual representation


http://www.vincehuston.org/images/visitor_taxi.gif

Source code example

https://www.tutorialspoint.com/design_pattern/visitor_pattern.htm
More to follow...

Software Engineering: Mediator Pattern

Behavioral Pattern - Mediator Pattern

Intent
Define an object that encapsulates how a set of objects interact. Mediator promotes loose coupling by keeping objects from referring to each other explicitly, and it lets you vary their interaction independently. 

Why is this used?
This increases the res-usability of objects supported by the Mediator by decoupling them from the system. In addition, this simplifies the maintenance of the system by centralising control logic. Also, this simplifies and reduces the variety of messages sent between objects in the system.


UML diagram representation


https://i.stack.imgur.com/T4KCw.png

Visual representation


http://www.jamesportis.com/wp-content/uploads/2015/11/mediator-software-design-pattern.gif

Source code example

https://dzone.com/articles/design-patterns-mediator
More to follow...

Software Engineering: Builder Pattern

Creational Pattern - Builder Pattern


Intent
Separate the construction of a complex object from its representation so that the same construction process can create different representations.

Why is this used?
Encapsulates the way a complex object is constructed. This also allows objects to be constructed in a multi-step and varying process (as opposed to one-step factories). Furthermore, this hides the internal representation of the product from the client, while the product implementation can be swapped in and out because the client only sees the abstract interface.


UML diagram representation


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


Example of Builder
https://sourcemaking.com/design_patterns/builder

Source code example

https://sourcemaking.com/design_patterns/builder/java/2
More to follow...

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.  ...