tag:blogger.com,1999:blog-57414756789967850882024-03-19T03:37:12.025-07:00The Power of CodeGolov Pavelhttp://www.blogger.com/profile/10400648871863951082noreply@blogger.comBlogger4125tag:blogger.com,1999:blog-5741475678996785088.post-56859266544760800902018-01-30T04:19:00.000-08:002018-01-30T04:24:45.464-08:00Ручная компиляция исходного кода java<div dir="ltr" style="text-align: left;" trbidi="on">
<div style="text-align: justify;">
Разберемся, как в ручную откомпилировать программу. </div>
<div style="text-align: justify;">
Допустим, имеется следующая структура папок: </div>
<div style="text-align: justify;">
src -> ru -> golov -> main -> Main.java, </div>
<div style="text-align: justify;">
src -> ru -> golov -> io -> Printer.java</div>
<div style="text-align: justify;">
classes -> .</div>
<div style="text-align: justify;">
Файл Main.java:</div>
<div style="text-align: justify;">
<pre style="background-color: white; font-family: "Fira Code";"><span style="color: navy; font-weight: bold;">package </span>ru.golov.main;
<span style="color: navy; font-weight: bold;">import </span>ru.golov.io.*;
<span style="color: navy; font-weight: bold;">public class </span>Main {
<span style="color: navy; font-weight: bold;">public static void </span>main(String[] args) {
Printer printer = <span style="color: navy; font-weight: bold;">new </span>Printer();
printer.print(<span style="color: green; font-weight: bold;">"Hello world!"</span>);
}
}</pre>
<pre style="background-color: white; font-family: "Fira Code";"></pre>
<pre style="background-color: white; font-family: "Fira Code";">Файл Printer.java:</pre>
<pre style="background-color: white; font-family: "Fira Code";"></pre>
<pre style="background-color: white; font-family: "Fira Code";"><pre style="font-family: "Fira Code";"><span style="color: navy; font-weight: bold;">package </span>ru.golov.io;
<span style="color: navy; font-weight: bold;">public class </span>Printer {
<span style="color: navy; font-weight: bold;">public void </span>print(String s) {
System.out.println(s);
}
}</pre>
Выходим в корневой каталог, компилируем программу:
<b>javac -d classes -cp src src/ru/golov/main/Main.java</b>
classpath указывается для того, чтобы компилятор понимал, относительно какой директории делаются импорты.
Флаг -d говорит о том, что скомпилированные class - файлы должны быть расположены в дирректории classes.
</pre>
</div>
<div>
<div style="text-align: justify;">
Для того, чтобы запустить программу:<br />
<b>java -cp ./classes ru.golov.main.Main</b></div>
<div>
<br /></div>
</div>
</div>
Golov Pavelhttp://www.blogger.com/profile/10400648871863951082noreply@blogger.com0tag:blogger.com,1999:blog-5741475678996785088.post-19101421056655188642017-07-26T04:50:00.001-07:002017-07-26T04:50:12.414-07:00Function closures<div dir="ltr" style="text-align: left;" trbidi="on">
Good day! Recently, I studied JavaScript and сame across an interesting topic: <i>function closures</i>. This feature available in many functional programming languages, but I would like to describe it on JavaScript language.<br />
Let's start! Firstly, we need to get acquainted with the concept of global and local variables.<br />
In JavaScript, global variables is a variable or function, which are not inside some function. In JavaScript, all global variables are the properties of the global object (for example, in the browser, the global object is <i>window. </i>Example of global variable:<br />
<pre><code>var a = 15;</code></pre>
The variables, which declared inside the function are called local. Every function has own special object, which contains all variables of function - <i>LexicalEnvironment</i> object. When we create new function, we create new <i>LexicalEnvironment</i> object also. Moreover, every <i>LexicalEnvironment</i> object has a property [[Scope]], which contains a reference to an outer object LexicalEnvironment. As result, if we can't find a variable in the current LexicalEnvironment, we can find it in the outer LexicalEnvironment object. Let's review the example:<br />
<pre><code>function outerFunc() {
var a = 5; //LexicalEnvironment = {..., a: 5};
(function innerFunction() {
//LexicalEnvironment of inner function not contains "a" variable
//Take the variable from outer LexicalEnvironment
console.log(a);
})();
}
outerFunc(); //Output - 5;</code></pre>
Now, let's review the definition of closures. According to Wikipedia, <i>"In programming languages, closures (also lexical closures or function closures) are techniques for implementing lexically scoped name binding in languages with first-class functions." </i>AnothrWords, internal function remember the outer LexicalEnvironment and use the variables from there. Let's look at an example and discuss it:<br />
<pre><code>function getCounter() {
var count = 0;
return function() {
return count++; //Usage of outer LexicalEnvironment
}
}
var counter1 = getCounter();
console.log(counter1()); //0
console.log(counter1()); //1
//...
//Counters are indepentent
var counter2 = getCounter();
console.log(counter2()); //0
console.log(counter2()); //1</code></pre>
In this example we can see, that inner function use outer variable <i>count</i> and increment it. The result returned by the function call getCounter() refers to it's own outer LexicalEnvironment. When we invoke function getCounter(), we create new LexicalEnvironment and new variable count, as result, all counters are independent.<br />
I hope, this article gave you knowledge about the concept of closure! If you have a questions, you can ask it in comments.</div>
Golov Pavelhttp://www.blogger.com/profile/10400648871863951082noreply@blogger.com0tag:blogger.com,1999:blog-5741475678996785088.post-75084625984606995562017-07-19T14:20:00.001-07:002017-08-11T08:35:16.255-07:00Design pattern "Strategy"<div dir="ltr" style="text-align: left;" trbidi="on">
Hello everyone! Today I would like to tell you about design pattern strategy. In my opinion, this pattern is very interesting and usefull in day-to-day tasks!<br />
<br />
Strategy is behavioral pattern. It means, that this pattern determinate algorythms and ways to realization of interaction different objects and classes. You can use this pattern, when the behavior of your program depends on the situation. For example, you have several ways to implement some feature and your choice depends on the situation - it's a good reason to use strategy pattern!<br />
<br />
Now, let's review the components of this pattern. I would like to show all examples in the java language, but I try to describe it, if you are not familiar with it.<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg_V0L0adZqwpIAcR2KsJH4Llv5IEYSFnd_fzrvguIo1DsLAk_1Gv6qOLAQl_Nc1Eb29KZP620S7bWmVoboB9RbcFl-Wwu67H4kNM5AsHTWKcG8u8WGedTokllrUogEx5BVUSa-BZ-gSi81/s1600/Strategy_diagram.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="302" data-original-width="402" height="300" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg_V0L0adZqwpIAcR2KsJH4Llv5IEYSFnd_fzrvguIo1DsLAk_1Gv6qOLAQl_Nc1Eb29KZP620S7bWmVoboB9RbcFl-Wwu67H4kNM5AsHTWKcG8u8WGedTokllrUogEx5BVUSa-BZ-gSi81/s400/Strategy_diagram.png" width="400" /></a></div>
<ol style="text-align: left;">
<li>interface Strategey - interface, contains only one method - execute(), which execute some action;</li>
<li>class Context - class, contains some Strategy, which you can change depends on the situation, and method, which execute some strategy;</li>
<li>set of classes, which implements Strategy (ConcreteStrategy) - realizations of interface Strategy, or, in other words, the set of classes, which determ the behavior of program.</li>
</ol>
<div>
Now, I would like to show you example of the using this pattern. Imagine, that we would like to write compressor, but there are many methods to compress file (for example RAR, ZIP etc), and we whant to use different methods depends on the situation. Let's rewiev the code:<br />
<br />
<pre class="java"><code>public interface Strategy {
void execute(File file);
}</code></pre>
<pre><code class="jsp">public class Context {
private Strategy strategy;
public void setStrategy(Strategy strategy) {
this.strategy = strategy;
}
public void execute(File file) {
strategy.execute(file);
}
}</code></pre>
<pre><code class="java">public class RarCompressionStrategy implements Strategy{
@Override
public void execute(File file) {
// There should be rar-compression algorithm
}
}</code></pre>
<pre><code class="java">public class ZipCompressionStrategy implements Strategy {
@Override
public void execute(File file) {
// There should be zip-compression algorithm
}
}</code></pre>
</div>
<pre><code>public class Main {
public static void main(String[] args) throws IOException {
//Creating the context object
Context context = new Context();
// Give the user the choice of algorithm.
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Please, select the compression algorithm");
String answer = reader.readLine();
if (answer.toLowerCase().equals("rar")) {
context.setStrategy(new RarCompressionStrategy());
} else if (answer.toLowerCase().equals("zip")) {
context.setStrategy(new ZipCompressionStrategy());
} else {
// if the user entered incorrect data
throw new IllegalArgumentException();
}
//Executing some strategy
context.execute(new File("some file"));
}
}</code></pre>
<div>
<br />
In this case, user chose the method of compressing. I hope, this example was interesting for you!</div>
</div>
Golov Pavelhttp://www.blogger.com/profile/10400648871863951082noreply@blogger.com0tag:blogger.com,1999:blog-5741475678996785088.post-13617286695157491822017-07-18T14:15:00.000-07:002017-07-18T14:35:54.031-07:00Welcome!<div dir="ltr" style="text-align: left;" trbidi="on">
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
Hello everyone! Recently, the idea came to me create my own blog, where I can share my thoughts about programming, give some advices or discribe some interesting for me topics.<br />
<br />
The aim of my blog – simplify and make complex topics easy-understanding for everyone! I will try to write new articles regular and stably. There are the topics that will be considered in the near future:<br />
<br />
<ul style="text-align: left;">
<li>Advises for beginner – programmers;</li>
<li>Functional programming;</li>
<li>Django for beginners;</li>
<li>Design patterns in examples;</li>
<li>Review of interesting programmers educational projects.</li>
</ul>
<br />
I think, my articles will be interesting for you! See you soon!</div>
Golov Pavelhttp://www.blogger.com/profile/10400648871863951082noreply@blogger.com0