Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

public interface Animal { void create ( ) ; } public class animal 1 implements Animal { @Override public void create ( ) { System.out.println

public interface Animal {void create();}
public class animal1 implements Animal {
@Override
public void create(){
System.out.println("You've just created a cheetah!");}}
public class animal2 implements Animal {
@Override
public void create(){
System.out.println("You've just created a kangaroo!");}}
public class animal3 implements Animal {
@Override
public void create(){
System.out.println("You've just created a lion!");}}
public class factory {
public Animal get(String type){
if(type == null){
return null;
}if(type.equalsIgnoreCase("Kangaroo")){
return new animal1();
}else if(type.equalsIgnoreCase("Cheetah")){
return new animal2();
}else if(type.equalsIgnoreCase("Lion")){
return new animal3();}
return null;}}
public class Main {
public static void main(String[] args){
factory factory = new factory();
Animal animal1= factory.get("Kangaroo");
animal1.create();
Animal animal2= factory.get("Cheetah");
animal2.create();
Animal animal3= factory.get("Lion");
animal3.create();}}
Task One. Look at the above and do the following
For this task, you are required to refactor the badly written program above in the
Factory folder.
The factory pattern is a method that returns one of several possible classes
that share a common superclass. It is particularly useful when you dont
know beforehand what class you need and the decision can only be made
at run time. The superclass that the Factory pattern uses is usually
implemented as an interface. An interface is a special type of class that
acts as a blueprint of classes to come. It is an abstract type that specifies
the behaviour of future classes. An interface will contain method headings
but the methods in an interface dont contain any logic. The logic for the
methods specified in the interface is written in the classes that implement
the interface. In essence, an interface shows a class what to do, and not
how to do it.
It is not vital for you to be familiar with design patterns for this task, but
you can read more about them here.
For this task be sure to:
Troubleshoot and debug the code first so that it runs correctly.
Fix the indentation and formatting of the code so that it adheres to
the guidelines provided here.
Make sure that all the names of variables, classes, methods, etc.
adhere to the guidelines provided here.
Refactor the code to improve the quality and readability of the code
in other ways highlighted in this task.
Compulsory Task 2 look at the code below
Follow these steps:
For this task, you are required to refactor the badly written program in the
Decorator folder.
The decorator pattern adds something extra to an object that already
exists. It wraps an object in order to add functionality or features to the
object. The purpose of the pattern is to make it easy to add or remove
functions if needed. This is done by wrapping a single object (not class) to
add new behaviour. It has an attribute for that object and has all the same
methods as the wrapped object.
It is not vital for you to be familiar with design patterns for this task, but
you can read more about them here.
For this task be sure to:
Troubleshoot and debug the code first so that it runs correctly.
Fix the indentation and formatting of the code so that it adheres to
the guidelines provided here.
Make sure that all the names of variables, classes, methods, etc.
adhere to the guidelines provided here.
Refactor the code to improve the quality and readability of the code
in other ways highlighted in this task.
public interface Frame {
public void render();}
public class BasicFrame implements Frame{
@Override
public void render(){
System.out.println("If this were a real GUI, it'd display the frame");}}
public class FrameDecorator implements Frame {
Frame decoratedFrame;
public FrameDecorator(Frame wrappee){
decoratedFrame = wrappee;}
public void render(){
decoratedFrame.render();}}
public class ScalingFrameDecorator extends FrameDecorator {
private double scaleFactor =1;
public ScalingFrameDecorator(Frame wrappee, double scaleFactor){
super(wrappee);
this.scaleFactor = scaleFactor;}
public void render(){
super.render();
System.out.println(String.format("Imagine making the frame %.1f times bigger!", scaleFactor));}
public void scale(double factor){scaleFactor = factor;}}
public class ScrollingFrameDecorator extends FrameDecorator{
private int numScrollBars =1;
public ScrollingFrameDecorator(Frame wrappee, int numScrollBars){
super(wrappee);
this.numScrollBars = numScrollBars;}
public void render(){super.render();
System.out.println(String.format("Imagine adding %d scroll bars to the frame!", numScrollBars));}
public void addScrollBar(){numScrollBars++;}}
public class Main {
public static void main(String[] args){
System.out.println("The basic frame");
Frame var1= new BasicFrame();
var1.render();
System.out.pri

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database And Expert Systems Applications 19th International Conference Dexa 2008 Turin Italy September 2008 Proceedings Lncs 5181

Authors: Sourav S. Bhowmick ,Josef Kung ,Roland Wagner

2008th Edition

3540856536, 978-3540856535

More Books

Students also viewed these Databases questions